Package Exports
- parse-value
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (parse-value) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
ParseValue
Installation
npm install --save parse-valueUsage
// require parseValue
var parseValue = require('parse-value');
// define type and value
var type = 'number',
value = '3';
// parse value to requested type
var number = parseValue(type, value);Documentation
parseValue(type, value):
Parse value to given type
params
- String
type: type to use- 'string' (default), 'number', 'int', 'integer', 'float', 'date', 'bool', 'boolean', 'regex', 'regexp', 'null'
- Object
value: value to parse
return
- Object: parsed value
Examples
String
parseValue('string','test')
// 'test'
parseValue('string', 3)
// '3'Number
parseValue('number','3')
// 3
parseValue('number','3.2')
// 3.2
parseValue('number', 'abc')
// NaN
parseValue('int','3')
// 3
parseValue('int','3.2')
// 3
parseValue('float','3.2')
// 3.2Date
parseValue('date', '2000-01-01')
// Sat Jan 01 2000 01:00:00 GMT+0100 (CET)
parseValue('date', 946684800000)
// Sat Jan 01 2000 01:00:00 GMT+0100 (CET)
parseValue('date', 'abc')
// Invalid DateBool
parseValue('bool', true)
// true
parseValue('bool', 1)
// true
parseValue('bool', 'true')
// true
parseValue('bool', 't')
// true
parseValue('bool', 'yes')
// true
parseValue('bool', 'y')
// true
parseValue('bool', false)
// false
parseValue('bool', 0)
// false
parseValue('bool', 'false')
// false
parseValue('bool', 'f')
// false
parseValue('bool', 'no')
// false
parseValue('bool', 'n')
// false
parseValue('bool', null)
// falseRegex
parseValue('regex', 'a+b')
// /a+b/gm
parseValue('regex', '.*\\.js')
// /.*\.js/gmNull
parseValue('null', 'anyValue')
// null
parseValue('null', 3)
// nullLICENSE: MIT