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

normalize-type

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

normalize-type

Normalizes string JavaScript values to have real type (so "123" becomes number 123 etc).

  • 1.5.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Normalize type

Coverage Downloads Version License

Normalizes string JavaScript values to have real type.

  • a string "123" becomes number 123.
  • "true" becomes boolean true.
  • works recursively on objects and as well so { a: ['123'] } becomes { a: [123] } etc.

Mainly useful for parsing query parameters etc where you always get string values for everything but you'd like to use the values with their real types or validate them etc.

Installation

This package is distributed via npm

npm install normalize-type

Example

const normalizeType = require('./build/index').default;

const parameters = {
  id: '2',
  name: 'Jack Daniels',
  age: '26',
  height: '1.84',
  birthday: new Date(1849, 1, 15, 0, 0, 0, 0),
  accessLevels: ['1', '3'],
};

console.log(normalizeType(parameters));

/*
> npm run build && node example
{
  id: 2,
  name: 'Jack Daniels',
  age: 26,
  height: 1.84,
  birthday: 1849-02-14T22:00:00.000Z,
  accessLevels: [ 1, 3 ]
}
*/

See example.js and run npm run build && node example to try for yourself.

Features verified by 100% test coverage

  • √ should leave strings to be strings (3ms)
  • √ should not modify non-strings (2ms)
  • √ should convert numeric integer strings to numbers
  • √ should convert numeric float strings to numbers
  • √ should preserve number-like invalid number values (1ms)
  • √ should convert "true" and "false" to booleans
  • √ should convert "undefined" to undefined
  • √ should not convert non-lowercase special mappings
  • √ should convert arrays of values (1ms)
  • √ should convert nested arrays of values
  • √ should convert object maps (1ms)
  • √ should convert nested object maps
  • √ should handle mixed content

Keywords

FAQs

Package last updated on 23 Apr 2019

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