2.0.1 • Published 10 months ago

meta-transformer v2.0.1

Weekly downloads
38
License
MIT
Repository
github
Last release
10 months ago

GitHub npm bundle size npm

What is meta-transformer?

meta-transformer is a lightweight (1k gzipped), tree-shakable, zero dependency library for transforming plain JavaScript objects to class instances. It is isomorphic and can be used with NodeJs or in a browser.

Installation

Install the meta-transformer package from npm. npm install meta-transformer

Usage

class Widget {
    name: string;
    model: number;
    created: Date;
}

const classInstance: Widget = MetaTransformer.toClass<Widget>(Widget, {
    name: "Doodad",
    model: 1234,
    created: new Date()
});

You can also transform arrays in the same way.

const classArray: Widget[] = MetaTransformer.toClass<Widget>(Widget, [
        {
            name: "Doodad",
            color: "Blue",
            model: 1234
        },
        {
            name: "Thing",
            color: "Red",
            model: 9876
        }
    ]
);

Transforming Nested Objects

You can use the @Transform(<class type>) to transform nested complex objects.

class WidgetDetail {
    material: string;
    shape: string;
}

class Widget {
    name: string;
    color: string;
    model: number;

    @Transform(WidgetDetail)
    detail: WidgetDetail;
}

const classInstance: Widget = MetaTransformer.toClass<Widget>(Widget, {
    name: "Doodad",
    color: "Blue",
    model: 1234,
    detail: {
        material: "Plastic",
        shape: "Square"
    }
);

Exclude Properties

You can use the @Exclude() decorator to exclude properties from transformation.

class Widget {
    name: string;
    color: string;

    @Exclude()
    model: number;
}

const classInstance = MetaTransformer.toClass<Widget>(Widget, {
    name: "Doodad",
    color: "Blue",
    model: 1234
});

// The transformed classInstance is {"name":"Doodad","color":"Blue"}

Circular Dependencies

meta-transformer will throw an error if you try to transform objects that have circular dependencies.

2.0.1

10 months ago

2.0.0

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

0.0.33

2 years ago

0.0.31

2 years ago

0.0.32

2 years ago

0.0.30

3 years ago

0.0.29

3 years ago

0.0.27

3 years ago

0.0.28

3 years ago

0.0.26

3 years ago

0.0.25

3 years ago

0.0.24

3 years ago

0.0.23

3 years ago

0.0.22

3 years ago

0.0.21

3 years ago

0.0.20

3 years ago

0.0.19

3 years ago

0.0.18

3 years ago

0.0.17

4 years ago

0.0.16

4 years ago

0.0.15

4 years ago

0.0.14

4 years ago

0.0.13

4 years ago

0.0.12

4 years ago

0.0.10

4 years ago

0.0.11

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.7

4 years ago

0.0.6

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago