Skip to content

zvxayr/coerce-pattern

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Matches a values into a pattern

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published