2.3.6 • Published 3 years ago

typedconverter v2.3.6

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

TypedConverter

Convert object into classes match with TypeScript type annotation

Build Status Coverage Status

Convert Primitive Type

import createConverter from "typedconverter";

const convert = createConverter()
const numb = convert("12345", Number) //return number 12345
const numb = convert("YES", Boolean) //return true
const numb = convert("2019-1-1", Date) //return date 1/1/2019

Specify type on configuration

Expected type can be specified in the configuration, than you can omit expected type on the second parameter of the convert function. Useful when you want to covert several times without specifying expected type.

import createConverter from "typedconverter";

const convert = createConverter({type: Number})
const numb = convert("12345")
const numb1 = convert("-12345")
const numb2 = convert("12345.123")

Convert custom class

TypedConvert uses tinspector to get type metadata, so it aware about TypeScript type annotation.

import createConverter from "typedconverter";
import reflect from "tinspector"

const convert = createConverter()

reflect.parameterProperties()
class AnimalClass {
    constructor(
        public id: number,
        public name: string,
        public deceased: boolean,
        public birthday: Date
    ) { }
}
//return instance of AnimalClass with appropriate properties type
const data = convert({ 
    id: "200", 
    name: "Mimi", 
    deceased: "ON", 
    birthday: "2018-1-1" }, 
    AnimalClass) 

Convert Array

Convert into array by providing array of type in the expected type.

import createConverter from "typedconverter";

const convert = createConverter()
const numb = convert(["1", "2", "-3"], [Number])

Custom Converter

Provided custom converter on the configuration

import createConverter from "typedconverter";

const convert = createConverter({ 
    type: Boolean, 
    converters: [{ key: Boolean, converter: x => "Custom Boolean" }] 
})
const numb = convert("True") //result: "Custom Boolean"
2.3.6

4 years ago

2.3.5

4 years ago

2.3.4

4 years ago

2.3.3

4 years ago

2.3.2

4 years ago

2.3.1

4 years ago

2.3.0

4 years ago

2.2.1

4 years ago

2.2.0

4 years ago

2.1.0

4 years ago

2.0.0

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

1.0.0-beta.7

5 years ago

1.0.0-beta.6

5 years ago

1.0.0-beta.5

5 years ago

1.0.0-beta.4

5 years ago

1.0.0-beta.3

5 years ago

1.0.0-beta.2

5 years ago

1.0.0-beta.1

5 years ago

1.0.0-beta.0

5 years ago

1.0.0-alpha.9

5 years ago

1.0.0-alpha.8

5 years ago

1.0.0-alpha.7

5 years ago

1.0.0-alpha.6

5 years ago

1.0.0-alpha.5

5 years ago

1.0.0-alpha.4

5 years ago

1.0.0-alpha.3

5 years ago

1.0.0-alpha.2

5 years ago

1.0.0-alpha.1

5 years ago

1.0.0-alpha.0

5 years ago