Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

baiji-normalizer

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

baiji-normalizer

Baiji Normalizer

  • 1.0.10
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
103
decreased by-94.03%
Maintainers
1
Weekly downloads
 
Created
Source

Baiji Normalizer

Build Status

A smart value type converter for Baiji

Usage

Installation

npm install baiji-normalizer

Convert value

// Built-in Types
// number, string, date, boolean, object, any


var Normalizer = require('baiji-normalizer');

// Number
Normalizer.convert('1', 'number')       // => 1
Normalizer.convert(1, 'number')         // => 1
Normalizer.convert('', 'number')        // => null
Normalizer.convert(undefined, 'number') // => undefined
Normalizer.convert(null, 'number')      // => null

// String
Normalizer.convert('1', 'string')       // => '1'
Normalizer.convert(1, 'string')         // => '1'
Normalizer.convert('', 'string')        // => ''
Normalizer.convert(undefined, 'string') // => undefined
Normalizer.convert(null, 'string')      // => null

// Date
Normalizer.convert('1', 'date')       // => Thu Jan 01 1970 08:00:00 GMT+0800 (CST)
Normalizer.convert(1, 'date')         // => Thu Jan 01 1970 08:00:00 GMT+0800 (CST)
Normalizer.convert('', 'date')        // => Invalid Date
Normalizer.convert(undefined, 'date') // => undefined
Normalizer.convert(null, 'date')      // => null

// Boolean
Normalizer.convert(undefined, 'boolean')     // => undefined
Normalizer.convert('1', 'boolean')           // => true
Normalizer.convert(1, 'boolean')             // => true
Normalizer.convert(0, 'boolean')             // => false
Normalizer.convert(NaN, 'boolean')           // => false
Normalizer.convert('', 'boolean')            // => false
Normalizer.convert('undefined', 'boolean')   // => false
Normalizer.convert('null', 'boolean')        // => false
Normalizer.convert('0', 'boolean')           // => false
Normalizer.convert(null, 'boolean')          // => false

// Handle array type
Normalizer.convert(1, ['number'])          // => [1]
Normalizer.convert(true, ['boolean'])      // => [true]

Add new or overwrite existing converter

var Normalizer = require('baiji-normalizer');

// add new
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, '\\$&'));
});

// overwrite existing
Normalizer.define('boolean', function(val, opts) {
  return Boolean(val);
});

Other APIs


# Normalizer.undefine(type, converterFn) => undefine a converter
# Normalizer.canConvert(type)            => check if a type can be converted
# Normalizer.getConverter(type)          => get a specific converter
# Normalizer.convertArray(val, opts)          => convert val to array

License

  • The MIT license

Keywords

FAQs

Package last updated on 02 Mar 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc