1.0.3 • Published 2 years ago

@paddls/ts-mapperize v1.0.3

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

TS-Mapperize

ts-mapperize-ci Coverage Status npm version GitHub GitHub repo size GitHub last commit GitHub issues GitHub top language

Informations

:warning: Since version 1.0.2, ts-mapperize has been published under @paddls namespace. We continue to maintain @witty-services namespace.

Get Started

Install

npm install @paddls/ts-mapperize

or

npm install @witty-services/ts-mapperize

Create simple mapper

class A {
  a: string;
}

class B {
  b: string;
}


class MyMapper {

    @Mapper(() => B, [
      {target: 'b', source: 'a'}
    ])
    public mapAToB: MapperFn<A, B>;
  
}

const mapper: MyMapper = new MyMapper();

mapper.mapAToB(new A())
// should return B{ b: '...' }

Working with array

Define all mapping behavior

class A {
  a: string;
}

class B {
  b: string;
}


class MyMapper {

    @ArrayMapper(() => B, [
      {target: 'b', source: 'a'}
    ])
    public mapAToB: ArrayMapperFn<A, B>;
  
}

const mapper: MyMapper = new MyMapper();

mapper.mapAToB([new A()])
// should return [B{ b: '...' }]

Reuse behavior from function

class A {
  a: string;
}

class B {
  b: string;
}


class MyMapper {

    @Mapper(() => B, [
      {target: 'b', source: 'a'}
    ])
    public mapAToB: MapperFn<A, B>;

    @ArrayMapper('mapAToB')
    public mapAToBArray: ArrayMapperFn<A, B>;
  
}

const mapper: MyMapper = new MyMapper();

mapper.mapAToBArray([new A()])
// should return [B{ b: '...' }]

API

MapperParamContext

ArgumentTypeRequiredDescription
sourcestring, keyof\falseselect from the input, the value to be mapped
targetstring, keyof\falseselect the destination of the value inside the output object
customTransformer(() => new(...args: any[]) => CustomTransformer<any, any>)falseuse an existing CustomTransformer class
transform(input: any) => anyfalsecustom function to map value from selected source to target
type() => new(...args: any[]) => anyfalsetype of the child object
paramsMapperParamContext<any, any>[]falselist of mapping information for child object

How to run Unit Tests

To run unit tests and generate coverage with Jest, run :

npm run test