parse-strings-in-object
Advanced tools
Comparing version 1.1.5 to 1.1.6
{ | ||
"name": "parse-strings-in-object", | ||
"version": "1.1.5", | ||
"version": "1.1.6", | ||
"description": "Convert string values in object to boolean and numbers", | ||
@@ -5,0 +5,0 @@ "keywords": ["json", "parser", "types", "configuration", "utilities", "strings", "objects"], |
# 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 | ||
``` | ||
## Overview | ||
A very simple module that takes a JavaScript object and returns a new object with *string representations of booleans and numbers* converted to their proper types. | ||
This simple module reads your JS Object recursively and converts string values 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 | ||
It works recursively, so nested structures are no problem. | ||
This module was originally inspired by the experience of using a configuration module ([rc](https://www.npmjs.com/package/rc)) and having to check things like `active === false || active === 'false'` repeatedly. I have therefore provided an example of this use case [below](#example-in-rc-config). | ||
## Usage | ||
@@ -31,2 +27,3 @@ Install from npm: | ||
## Example | ||
@@ -61,2 +58,34 @@ ``` | ||
# Example in rc config | ||
The [rc](https://www.npmjs.com/package/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 | ||
@@ -63,0 +92,0 @@ Feel free to improve the module! All pull requests shall be considered. |
9006
95