npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

parse-strings-in-object

v1.6.0

Published

Convert string values in object to boolean and numbers

Downloads

5,972

Readme

Parse Strings in JS Object

Overview

A very simple module that takes a JavaScript object and returns a new object with string representations of booleans, nulls and numbers converted to their proper types.

So:

  • "true" and "false" becomes true and false
  • "1" and "3.147" become 1 and 3.147
  • "192.168.1.1" is left alone even though it "looks" like a number
  • "null" becomes null

It works recursively, so nested structures are no problem.

Array-like strings (anything between [] brackets inside the string), are converted too:

  • "[test,one,two,three]" becomes ["test","one","two","three"] (an array of strings)
  • "[0,1,2,3]" becomes [0,1,2,3] (an array of numbers)

This module was originally inspired by the experience of using a configuration module (rc) and having to check things like active === false || active === 'false' repeatedly. I have therefore provided an example of this use case below.

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

const before = {
    active: true,
    anInt: 1,
    aFloat: 1.1,
    justAString: "hello",
    ipAddress: "192.168.1.101"
}

let after = require("parse-strings-in-object")(before);
console.log(JSON.stringify(after, null, 4), typeof after.aFloat, 'and also a', typeof after.anInt);

The output will be:

{
    active: true,
    anInt: 1,
    aFloat: 1.1,
    justAString: "hello",
    ipAddress: "192.168.1.101"
}
number
and also a
number

Notice that both ints and floats are converted correctly to the single "number" type, and a number-like string such as an IP address is left alone (stays a string).

Example in rc config

The rc module for configuration loading allows hard-coded defaults (where types are respected) and also overrides ini files, environment variables and command-line params, where only strings are possible. This makes strict comparisons with === prone to bugs.

The module addresses this nicely. Just wrap the returned config object in a parse-strings-in-object require statement. For example:

const conf = require('parse-strings-in-object')(require('rc')('myapp', {
    anOrdinaryString: "test",
    aBoolean: true,
    aNumber: 9000
}));

Now, if you run your app with --aBoolean=false or --aNumber=9001 then you can safely check whether aBoolean === true or aNumber===9000 and get the expected results.

Why is this necessary?

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

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