Baiji Normalizer
A smart value type converter for Baiji
Usage
Installation
npm install baiji-normalizer
Convert value
var Normalizer = require('baiji-normalizer');
Normalizer.convert('1', 'number')
Normalizer.convert(1, 'number')
Normalizer.convert('', 'number')
Normalizer.convert(undefined, 'number')
Normalizer.convert(null, 'number')
Normalizer.convert('1', 'string')
Normalizer.convert(1, 'string')
Normalizer.convert('', 'string')
Normalizer.convert(undefined, 'string')
Normalizer.convert(null, 'string')
Normalizer.convert('1', 'date')
Normalizer.convert(1, 'date')
Normalizer.convert('', 'date')
Normalizer.convert(undefined, 'date')
Normalizer.convert(null, 'date')
Normalizer.convert(undefined, 'boolean')
Normalizer.convert('1', 'boolean')
Normalizer.convert(1, 'boolean')
Normalizer.convert(0, 'boolean')
Normalizer.convert(NaN, 'boolean')
Normalizer.convert('', 'boolean')
Normalizer.convert('undefined', 'boolean')
Normalizer.convert('null', 'boolean')
Normalizer.convert('0', 'boolean')
Normalizer.convert(null, 'boolean')
Normalizer.convert(1, ['number'])
Normalizer.convert(true, ['boolean'])
Add new or overwrite existing converter
var Normalizer = require('baiji-normalizer');
Normalizer.define('regexp', function(val, opts) {
if (val === undefined || val === null || val instanceof RegExp) return val;
val = String(val);
return new RegExp(val.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&'));
});
Normalizer.define('boolean', function(val, opts) {
return Boolean(val);
});
Other APIs
License