https://github.com/adam-rocska/ts-codec
TypeScript library that delivers reliable contracts for encoding and decoding values with strong type safety.
https://github.com/adam-rocska/ts-codec
codec coder decoder decoding encoder encoding json transformation typescript typescript-library
Last synced: 3 months ago
JSON representation
TypeScript library that delivers reliable contracts for encoding and decoding values with strong type safety.
- Host: GitHub
- URL: https://github.com/adam-rocska/ts-codec
- Owner: adam-rocska
- License: mit
- Created: 2023-03-27T17:35:14.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2025-02-04T16:10:15.000Z (5 months ago)
- Last Synced: 2025-04-03T10:44:35.054Z (3 months ago)
- Topics: codec, coder, decoder, decoding, encoder, encoding, json, transformation, typescript, typescript-library
- Language: TypeScript
- Homepage:
- Size: 606 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# Codec – Encode & Decode for TypeScript
[](https://www.npmjs.com/package/@adam-rocska/ts-codec)
[](https://github.com/adam-rocska/ts-codec-typescript/blob/master/LICENSE)| Aspect | Badge |
| -------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------- |
| Minified | [](https://bundlephobia.com/package/@adam-rocska/ts-codec) |
| Minified + gzip | [](https://bundlephobia.com/package/@adam-rocska/ts-codec) |
| Dependency Count | [](https://bundlephobia.com/package/@adam-rocska/ts-codec) |
| Tree-shaking Support | [](https://bundlephobia.com/package/@adam-rocska/ts-codec) |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
```typescript
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;
const source: TestRecord = {
firstName: 'Adam',
lastName: 'Rocska',
age: undefined,
traits: [],
};
const str = testCodec.encode(source);
const result = testCodec.decode(str);
expect(result).toMatchObject(source);
```