0.0.14 • Published 3 years ago

@cmind/class-mapper v0.0.14

Weekly downloads
48
License
MIT
Repository
github
Last release
3 years ago

@cmind/class-mapper

Class mapper consist of several decorators and two functions: box(), unbox(). You use box to convert your class to raw JS object. And unbox() to convert JS object and its nested properties to the instances of specified classes.

Install

npm i --save @cmind/class-mapper

Usage

class Person {
  @number()
  id: number;
  @string()
  name: string;
  @boolean()
  enabled: boolean;

  displayName: string;

  constructor(id: number, name: string, enabled = true) {
    this.id = id;
    this.name = name;
    this.enabled = enabled;
  }
}

class Organization {
  @number()
  id: number;

  @array(Person)
  employees: Person[];
}
const raw = {
  id: '3591',
  employees: [{
    id: 1,
    name: 'John',
    enabled: true
  }, {
    id: 2,
    name: 'Jack',
    enabled: true
  }]
};

const organization = unbox<Organization>(raw, Organization);

console.log(organization instanceof Organization); //true
console.log(organization.id === 3591); //true
console.log(organization.employees[0] instanceof Person); //true

const rawOrganization = box<Organization>(organization);

Decorators:

any();                     // no conversion
number();                  // convert input value to number (+value) 
string();                  // convert input value to string (value.toString())
boolean();                 // convert input value to boolean (!!value)
object(cls: new() => any); // unbox input value to cls
array(cls: new() => any);  // create array and unbox input each element of input value to cls

skipBoxing();              // Skip field on boxing
skipUnboxing();            // Skip field on unboxing 
0.0.14

3 years ago

0.0.12

3 years ago

0.0.13

3 years ago

0.0.11

3 years ago

0.0.10

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.1

3 years ago