1.0.4 • Published 1 year ago
decoverto v1.0.4
Decoverto
Convert data into instances of first and third party classes and back using decorators. Enjoy not having to write data-to-class setup code.
Test and tinker on the Decoverto playground
Features
- Convert object literals to and from instances
- Multilevel inheritance
- Reflect property types and stay DRY
- Make your own converters Handy for third-party types
- Builtin support for:
number
,string
,Date
,ArrayBuffer
,DataView
,TypedArray
- Collections such as
Array
,Set
,Map
- Deferred types
- Convert any raw data:
- Works in browser and Node
- Use TypeScript (playground) or JavaScript with Babel (playground)
A model looks like this:
import {model, map, MapShape, property} from 'decoverto';
@model()
class User {
@property()
createdAt: Date;
@property()
givenName: string;
@property(() => String)
referralToken: string | null;
@property(map(() => String, () => Boolean, {shape: MapShape.Object}))
permissions: Map<string, boolean>;
}
Quickstart
Install decoverto
yarn add decoverto
or
npm install decoverto
When using TypeScript, enable
experimentalDecorators
intsconfig.json
Define a model
import {model, map, MapShape, property} from 'decoverto'; @model() class User { @property(() => Date) createdAt: Date; @property(() => String) givenName: string; @property(() => String) referralToken: string | null; @property(map(() => String, () => Boolean, {shape: MapShape.Object})) permissions: Map<string, boolean>; }
Create a Decoverto instance
const decoverto = new Decoverto()
Convert some data
// Convert raw data using the default JSON parser user = decoverto.type(User).rawToInstance(`{ "createdAt": "2021-03-31T14:08:42.009Z", "givenName": "Mark", "referralToken": null, "permissions": { "canManageUsers": true, "canCreateProducts": true } }`); // Convert an object literal user = decoverto.type(User).plainToInstance({ createdAt: new Date(), givenName: 'Mark', referralToken: null, permissions: { canManageUsers: true, canCreateProducts: true, } });
For more information, see links in the features list or the docs directory.
Examples/Playground
Decoverto has playgrounds where you can view examples and tinker with them. It allows you to see what is possible and experiment.
- TypeScript playground This is the main playground.
- JavaScript + Babel playground