type-casting
Lightweight Library for cast and validating data.
Used in https://www.npmjs.com/react-url-sync
Installing
npm install type-casting
Example
const user = // Print { id: 5, name: "abc", surname: "", newsletter: true }console
API
Library is exporting three modules:
- Factory
- Type
- ERROR
Factory methods
- cast(any) - cast to specified type
- validate(any) - validate specified type
Type.Array
const list = Typeoptions default: 1 2// 5, 3, 6list/*{ errors: [ { error: 'NOT_A_NUMBER', index: 1 }, { error: 'REQUIRED_BUT_EMPTY', index: 3 } ]}*/listlist // [1, 2]
Type.Bool
const bool = Typebool // truebool // { 'error': 'NOT_A_BOOLEAN' }bool // truebool // false
Type.Float
const float = Typefloat // 14.5float // { 'error': 'NOT_A_NUMBER' }float // null
Type.Int
const int = Typeint // 0int // 55int // { 'error': 'NOT_A_NUMBER' }
Type.Object
const video = videovideo
or types could be defined as:
Type Type Type Type TypeVideo
Type.String
const string = Typestring // ''string // '55'string // 'aaa'string // { error: ERROR.REQUIRED_BUT_EMPTY }
Type.set
Type
ERROR
NOT_A_NUMBERIS_EMPTYNOT_AN_ARRAYNOT_AN_OBJECTREQUIRED_BUT_EMPTYNOT_A_BOOLEANNOT_VALID_TYPE
Testing
npm run test
Building
npm run build