0.1.0 • Published 1 year ago

flex-mapper v0.1.0

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

flex-mapper

TypeScript library for mapping objects and classes.

Usage

Installation:

npm i flex-mapper

map function

Maps objects

import { map } from 'flex-mapper';

...

const cat = {
    name: "Tom",
    color: "gray",
    years: 2
}
const result = map(cat, {
    name: 'nickname',
    color: value => value.toUpperCase(),
    years: [value => value * 12, "months"]
});
console.info(result);
// {
//     nickname: "Tom",
//     color: "GRAY",
//     months: 24
// }

map class

Destination class definition. Mapping options are described with decorators: @mapProperty and @mapConvert.

import { mapProperty, mapConvert } from 'flex-mapper';

...

class CatClass {
    @mapProperty("name")
    nickname: number;

    @mapConvert(value => value.toUpperCase())
    color: string;

    @mapProperty("years", value => value * 12)
    months: number;
}

Mapping class object:

import { map } from 'flex-mapper';

...

const cat = {
    name: "Tom",
    color: "gray",
    years: 2
}
const result = map(cat, CatClass);
console.info(result);
// {
//     nickname: "Tom",
//     color: "GRAY",
//     months: 24
// }

This is important to pass the class type as a second parameter. In this way map method reads decorators defined inside the class.

mapProperty decorator

Describes how the property is to be mapped.

Parameter is an interface with properties:

NameTypeDescription
sourcestringSource property name
convertfunction or stringIn function case, parameter is source value the function result is definition value. In string case, possible values are: number, string.

mapConvert decorator

Has one function or string parameter for converting.

0.1.0

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago