coerce-pattern

1.0.4 • Public • Published

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

Dependencies (0)

    Dev Dependencies (0)

      Package Sidebar

      Install

      npm i coerce-pattern

      Weekly Downloads

      1

      Version

      1.0.4

      License

      MIT

      Last publish

      Collaborators

      • zvxayr