Coerces values into a pattern
npm install coerce-pattern --save
const Coerce = require('coerce-pattern');
let coerce = new Coerce();
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);
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'] }
}
)
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'
});
MIT