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

parse-strings-in-object

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

parse-strings-in-object

Convert string values in object to boolean and numbers

  • 1.1.4
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.9K
increased by30.5%
Maintainers
1
Weekly downloads
 
Created
Source

Parse Strings in JS Object

JavaScript is notoriously loose with typing, so this can get you into trouble. For example, you might get configuration or JSON including strings as values:

'isMaster': 'true',
myNumber: '0'

So, now:

console.log(isMaster); // "true": as expected, but actually string
console.log(isMaster==true, isMaster===true); // "false false": oops
console.log(myNumber); // "0": as expected, but actually a string
console.log(typeof myNumber, myNumber==0, myNumber===0); // "string true false": hmmm
console.log(!myNumber); // "true": this is getting confusing

This simple module reads your JS Object recursively and converts string values to their proper types.

Usage

Install from npm:

npm install parse-strings-in-object

There is only one argument to pass to the module - a valid JavaScript object.

var niceParsedObject = require('parse-strings-in-object')(yourOriginalObject)

Example

let before = {
    topLevel: true,
    topNumber: 1,
    justAString: 'hello',
    ipAddress: '192.168.1.101'
}

let after = require('parse-strings-in-object')(before);

console.log('before:', before);
console.log('after:', JSON.stringify(after, null, 4));

The output will be:

{
    "topLevel": true,
    "topNumber": 1,
    "justAString": "hello",
    "ipAddress": "192.168.1.101"
}

Development and testing

Feel free to improve the module! All pull requests shall be considered.

After npm install you can run unit tests with Mocha like this:

npm run test

Keywords

FAQs

Package last updated on 05 Mar 2018

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