JSPM

parse-value

0.1.2
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 213
  • Score
    100M100P100Q79814F
  • License MIT

Generic value parser for javascript

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-value

Usage

// 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.2

Date

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 Date

Bool

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)
// false

Regex

parseValue('regex', 'a+b')
// /a+b/gm
parseValue('regex', '.*\\.js')
// /.*\.js/gm

Null

parseValue('null', 'anyValue')
// null
parseValue('null', 3)
// null

LICENSE: MIT