parse-strings-in-object
Advanced tools
Comparing version 1.2.0 to 2.0.0
@@ -1,37 +0,17 @@ | ||
let before = { | ||
topLevel: true, | ||
topNumber: 1, | ||
foo: { | ||
active: 'true', | ||
number: '0', | ||
anotherNumber: '3.17', | ||
}, | ||
bar: { | ||
active: 'false', | ||
number: '10', | ||
aString: 'yo', | ||
subSub: { | ||
thisIsTrue: 'true', | ||
thisIsFalse: 'false', | ||
thisIsNumber: '0.00006' | ||
} | ||
}, | ||
justAString: 'hello', | ||
ipAddress: '192.168.1.101', | ||
listOfNumbers: ['0', '1', '2'], | ||
listOfObjects: [ | ||
{ | ||
id: 0, | ||
value: 'hello' | ||
}, | ||
{ | ||
id: 1, | ||
value: 'world' | ||
} | ||
] | ||
} | ||
let after = require('../lib/index.js')(before); | ||
console.log('before:', before); | ||
console.log('after:\n', JSON.stringify(after, null, 4)); | ||
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
exports.__esModule = true; | ||
var dist_1 = __importDefault(require("../dist")); | ||
var input = { | ||
aNumber: "1.0", | ||
aString: "hello" | ||
}; | ||
var expectedResult = { | ||
aNumber: 1.0, | ||
aString: "hello" | ||
}; | ||
var result = dist_1["default"](input); | ||
console.log("input:", input); | ||
console.log("result:", result); |
{ | ||
"name": "parse-strings-in-object", | ||
"version": "1.2.0", | ||
"version": "2.0.0", | ||
"description": "Convert string values in object to boolean and numbers", | ||
"keywords": ["json", "parser", "types", "configuration", "utilities", "strings", "objects"], | ||
"keywords": [ | ||
"json", | ||
"parser", | ||
"types", | ||
"configuration", | ||
"utilities", | ||
"strings", | ||
"objects" | ||
], | ||
"repository": "https://github.com/anselanza/parse-strings-in-object", | ||
"main": "lib/index.js", | ||
"main": "dist/index.js", | ||
"scripts": { | ||
"test": "npx mocha test/tests.js --watch" | ||
"clean": "shx rm -rf dist", | ||
"build": "npx tsc && npm run test", | ||
"test": "npx jest" | ||
}, | ||
@@ -14,5 +24,11 @@ "author": "Stephen Buchanan", | ||
"devDependencies": { | ||
"mocha": "^5.0.1", | ||
"chai": "^4.1.2" | ||
"@types/jest": "^24.0.23", | ||
"jest": "^24.9.0", | ||
"prettier": "^1.19.1", | ||
"shx": "^0.3.2", | ||
"ts-jest": "^24.2.0", | ||
"tslint": "^5.20.1", | ||
"tslint-config-prettier": "^1.18.0", | ||
"typescript": "^3.7.3" | ||
} | ||
} |
# 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. | ||
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` | ||
- `'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. | ||
@@ -17,3 +19,5 @@ | ||
## Usage | ||
Install from npm: | ||
``` | ||
@@ -23,10 +27,29 @@ npm install parse-strings-in-object | ||
### In JS | ||
There is only one argument to pass to the module - a valid JavaScript object. | ||
``` | ||
var niceParsedObject = require('parse-strings-in-object')(yourOriginalObject) | ||
const parser = require('parse-strings-in-object'); | ||
const niceParsedObject = parser(yourOriginalObject); | ||
``` | ||
Or, more tersely: | ||
``` | ||
const niceParsedObject = require('parse-strings-in-object')(yourOriginalObject) | ||
``` | ||
### In TypeScript | ||
Import and use: | ||
``` | ||
import parser from 'parse-strings-in-object'; | ||
const niceParsedObject = parser(yourOriginalObject); | ||
``` | ||
## Example | ||
``` | ||
@@ -46,2 +69,3 @@ const before = { | ||
The output will be: | ||
``` | ||
@@ -59,9 +83,13 @@ { | ||
``` | ||
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 | ||
## 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: | ||
``` | ||
@@ -74,8 +102,9 @@ const conf = require('parse-strings-in-object')(require('rc')('myapp', { | ||
``` | ||
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: | ||
JavaScript can coerce types, so this can get you into trouble. For example, you might get configuration or JSON including strings as values: | ||
``` | ||
@@ -85,3 +114,5 @@ 'isMaster': 'true', | ||
``` | ||
So, now: | ||
``` | ||
@@ -95,9 +126,12 @@ console.log(isMaster); // "true": as expected, but actually string | ||
## Development and testing | ||
## 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: | ||
Since the module is written in TypeScript, you need to `npm run build` before using. | ||
Unit tests provided by the [Jest](https://jestjs.io/) framework: | ||
``` | ||
npm run test | ||
``` | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
24636
14
441
131
8
1