parse-strings-in-object
Advanced tools
Comparing version 1.1.6 to 1.2.0
@@ -7,3 +7,3 @@ const parseKeys = (obj) => | ||
if (typeof val === 'object') { | ||
if (typeof val === 'object' && val !== null) { | ||
if (Array.isArray(val)) { | ||
@@ -18,2 +18,4 @@ return {...acc, [key]: val}; | ||
if (val === 'false') val = false; | ||
if (val === 'null') val = null; | ||
// if (val === 'undefined') val = undefined; | ||
if ( | ||
@@ -20,0 +22,0 @@ !isNaN(parseFloat(val)) |
{ | ||
"name": "parse-strings-in-object", | ||
"version": "1.1.6", | ||
"version": "1.2.0", | ||
"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 | ||
## 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. | ||
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. | ||
@@ -10,2 +10,3 @@ So: | ||
* `'192.168.1.1'` is left alone even though it "looks" like a number | ||
* `'null'` becomes `null` | ||
@@ -12,0 +13,0 @@ It works recursively, so nested structures are no problem. |
@@ -16,2 +16,36 @@ const expect = require('chai').expect; | ||
it('should retain actual null values without errors', () => { | ||
const before = { foo: 'true', bar: null }; | ||
const result = parser(before); | ||
// expect(result).to.equal({ foo: true, bar: false }); | ||
expect(result.foo).to.equal(true); | ||
expect(result.bar).to.equal(null); | ||
}); | ||
it('should convert string representations of nulls into real nulls', () => { | ||
const before = { foo: 'true', bar: 'null' }; | ||
const result = parser(before); | ||
// expect(result).to.equal({ foo: true, bar: false }); | ||
expect(result.foo).to.equal(true); | ||
expect(result.bar).to.equal(null); | ||
}); | ||
it('should retain actual undefined values without errors', () => { | ||
const before = { foo: 'true', bar: undefined }; | ||
const result = parser(before); | ||
// expect(result).to.equal({ foo: true, bar: false }); | ||
expect(result.foo).to.equal(true); | ||
expect(result.bar).to.equal(undefined); | ||
expect(result.bar).to.be | ||
}); | ||
// it('should convert string representations of undefineds into real undefineds', () => { | ||
// const before = { foo: 'true', bar: 'undefined' }; | ||
// const result = parser(before); | ||
// // expect(result).to.equal({ foo: true, bar: false }); | ||
// expect(result.foo).to.equal(true); | ||
// expect(result.bar).to.equal(undefined); | ||
// expect(result.bar).to.be | ||
// }); | ||
it('should retain arrays properly', () => { | ||
@@ -24,3 +58,3 @@ const before = { foo: 'true', list: ['one', 'two', 'three']}; | ||
it('should convert strings-as-numbers into real numbers', () => { | ||
it('should convert string representations of numbers into real numbers', () => { | ||
const before = { aNumber: '1', another: '0', andAnother: '42' }; | ||
@@ -82,2 +116,3 @@ const result = parser(before); | ||
aString: 'yo', | ||
somethingNull: null, | ||
subSub: { | ||
@@ -90,3 +125,4 @@ thisIsTrue: 'true', | ||
justAString: 'hello', | ||
ipAddress: '192.168.1.101' | ||
ipAddress: '192.168.1.101', | ||
alsoNull: 'null' | ||
} | ||
@@ -106,2 +142,3 @@ const result = parser(before); | ||
aString: 'yo', | ||
somethingNull: null, | ||
subSub: { | ||
@@ -114,3 +151,4 @@ thisIsTrue: true, | ||
justAString: 'hello', | ||
ipAddress: '192.168.1.101' | ||
ipAddress: '192.168.1.101', | ||
alsoNull: null | ||
} | ||
@@ -117,0 +155,0 @@ expect(result).to.deep.equal(expected); |
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
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
10727
204
96