coerce-pattern

Coerces values into a pattern


Keywords
cast, coerce, pattern
License
MIT
Install
npm install coerce-pattern@1.0.4

Documentation

coerce-pattern

Coerces values into a pattern

Installation

npm install coerce-pattern --save

API

const Coerce = require('coerce-pattern');

let coerce = new Coerce();

Usage

With promises

coerce.cast(
    { id: Number, name: String, list: [String] },
    { id: '100', name: 'foo', list: [1, 2, 3, 4] }
).then(res => {
    // res == { id: 100, name: 'foo', list: ['1', '2', '3', '4'] }
}).catch(onError);

With callbacks

coerce.cast(
    { id: Number, name: String, list: [String] },
    { id: '100', name: 'foo', list: [1, 2, 3, 4] },
    function(err, res) {
        // res == { id: 100, name: 'foo', list: ['1', '2', '3', '4'] }
    }
)

Adding rules

coerce.addRule(
    (pattern, value) => value === 100 && pattern == String, // return true if rule applies
    (pattern, value) => '200' // return value of this rule
);

coerce.cast(100, String).then(res => {
    // res == '200'
});

License

MIT