Providing convenient methods to do fitering to the variables.
node install node-value-filter
const filter = require('node-value-filter');
var a = '"123.456"';
var b = filter(a).trim('"').number().int().get();
// b = 123;
- undefined => default | ''
- null => default | ''
- true => 'true'
- false => 'false'
- number => number.toString()
- object => object.toString()
- string => not changed
- default string (optional)
var a = filter(123.456).string().get();
// a = '123.456' (string)
var b = filter(null).string('-').get();
// b = '-' (string)
- string => string.trim()
- others => not changed
var a = filter([' abcdef ']).string().trim().get();
// a = 'abcdef' (string)
- string => string.replace( /^chars|chars$/gm, '' )
- others => not changed
- chars string | RegExp
var a = filter('"abcdef"').trim('"').get();
// a = abcdef (string)
var b = filter('{abcdef}').trim(/^{|}$/gm).get();
// b = abcdef (string)
- string => string.replace( /^prefix|suffix$/gm, '' )
- others => not changed
- prefix string
- suffix string
var a = filter('{abcdef}').trim('{','}').get();
// a = abcdef (string)
- undefined => default | 0
- null => default | 0
- true => 1
- false => 0
- string => new Number( string ).valueOf() | default | 0
- object => new Number( object.toString() ).valueOf() | default | 0
- number => not changed
- default number (optional)
var a = filter('0xff').number().get();
// a = 255 (number)
var b = filter('abc').number(-1).get();
// b = -1 (number)
- undefined => default | 0
- null => default | 0
- true => 1
- false => 0
- string => parseInt( string, radix | 10 ) | 0
- object => parseInt( object.toString(), radix | 10 ) | 0
- number => Math.trunc( number )
- radix integer (optional)
- default integer (optional)
var a = filter([123.456]).string().int().get();
// a = 123 (number)
var b = filter(['ff']).string().int(16).get();
// b = 255 (number)
var c = filter([]).string().int(-1).get();
// c = -1 (number)
- undefined => default | 0
- null => default | 0
- true => 1
- false => 0
- string => parseFloat( string ) | default | 0
- object => parseFloat( object.toString() ) | default | 0
- number => not changed
- default number
var a = filter(['1e2']).string().float().get();
// a = 100 (number)
var b = filter([]).string().float(0.1).get();
// b = 0.1 (number)
- f function
- context object (optional)
var a = filter(something).custom((input)=>{
// Do something with input
return result;
}).get();
- name string
- f function
- context object (optional)
filter
.bind('custom_name', (input, parameters...)=>{
// Do something with input, parameters...
return result;
})
.bind('other_name', ...);
var a = filter.custom_name(parameters...).get();
var b = filter.other_name(...).get();
Please contact to email
(Please add the prefix "[node-value-filter]" into the email title. Thanks!)