@adam-rocska/ts-codec
TypeScript icon, indicating that this package has built-in type declarations

1.2.3 • Public • Published

Codec – Encode & Decode for TypeScript

NPM Version License

Aspect Badge
Minified Minified
Minified + gzip Minified + gzip
Dependency Count Dependency Count
Tree-shaking Support Tree-shaking Support

A TypeScript library that provides a strongly-typed and user-friendly API for encoding and decoding data types.

Inspired by Swift's Codable API, this library aims to offer a straightforward functional API for basic encoding and decoding tasks. While it's a simplified initial release, future iterations will introduce more advanced features.

For detailed API documentation, please refer to this repository's GitHub Pages.

Example

const testCodec = record({
  firstName: string,
  lastName: optional(string),
  age: optional(number),
  traits: array(
    record({
      isVenerable: boolean,
      label: string,
      labelAlternatives: set(string),
      aquiredOn: date,
    })
  ),
});

type TestRecord = SourceType<typeof testCodec>;

const source: TestRecord = {
  firstName: 'Adam',
  lastName: 'Rocska',
  age: undefined,
  traits: [],
};
const str = testCodec.encode(source);
const result = testCodec.decode(str);
expect(result).toMatchObject(source);

Package Sidebar

Install

npm i @adam-rocska/ts-codec

Weekly Downloads

28

Version

1.2.3

License

MIT

Unpacked Size

49 kB

Total Files

7

Last publish

Collaborators

  • adam-rocska