@openf6p/typemapper

Typescript Object - object mapping util

Usage no npm install needed!

<script type="module">
  import openf6pTypemapper from 'https://cdn.skypack.dev/@openf6p/typemapper';
</script>

README

Open F6P - TypeMapper

Typescript Object - object mapping util

To install:

$ npm install --save @openf6p/typemapper

Usage:

import { TypeMapper } from "@openf6p/typemapper"

class ClassA {
    public prop1: string
    public prop2: string
    public prop3: string
}

class ClassB {
    public destProp1: string
    public destProp2: number
    public specialProp3: number
}

const mapper = new TypeMapper<ClassA, ClassB>()
    .map(x => x.destProp1, x => x.prop1)
    .map(x => x.destProp2, x => parseInt(x.prop2))
    .map(x => x.specialProp3, x => parseInt(x.prop3) / 100)
    .then(x => {
        // you can mutate your result object here
    })

const objA = new ClassA()
objA.prop1 = 'foo'
objA.prop2 = '123'
objA.prop3 = '456'

const objB: ClassB = mapper.parse(objA)

/**
 * Result:
 * {
 *   destProp1: 'foo',
 *   destProp2: 123,
 *   specialProp3: 4.56
 * }
 */