parse-strings-in-object
Advanced tools
Comparing version 1.4.0 to 1.5.0
@@ -34,3 +34,4 @@ "use strict"; | ||
if (isArrayLikeString(value) === true) { | ||
return convertArray(arrayLikeStringToArray(value)); | ||
const valueWithoutBrackets = value.replace("[", "").replace("]", ""); | ||
return convertArray(arrayLikeStringToArray(valueWithoutBrackets)); | ||
} | ||
@@ -49,3 +50,3 @@ else { | ||
const commaSeparated = s.split(","); | ||
if (commaSeparated.length > 1) { | ||
if (commaSeparated.length > 1 || s.includes("[") || s.includes("]")) { | ||
return true; | ||
@@ -61,4 +62,4 @@ } | ||
}; | ||
const arrayLikeStringToArray = (s, token = ",") => s.split(token).map(element => element.trim()).filter(element => element !== "" && element !== null); | ||
const arrayLikeStringToArray = (s, token = ",") => s.split(token).map(element => element.trim()); | ||
module.exports = parseKeys; | ||
//# sourceMappingURL=index.js.map |
@@ -163,3 +163,3 @@ "use strict"; | ||
test("array of strings", () => { | ||
const before = { list: "test,one,two,three" }; | ||
const before = { list: "[test,one,two,three]" }; | ||
const result = _1.default(before); | ||
@@ -171,3 +171,3 @@ expect(Array.isArray(result.list)).toBeTruthy(); | ||
test("single-element array (string)", () => { | ||
const before = { list: "one," }; | ||
const before = { list: "[one]" }; | ||
const result = _1.default(before); | ||
@@ -180,3 +180,3 @@ expect(Array.isArray(result.list)).toBeTruthy(); | ||
test("single-element array (number)", () => { | ||
const before = { list: "0.05," }; | ||
const before = { list: "[0.05]" }; | ||
const result = _1.default(before); | ||
@@ -190,3 +190,3 @@ expect(Array.isArray(result.list)).toBeTruthy(); | ||
test("array of numnbers", () => { | ||
const before = { list: "0,1,2,4,8" }; | ||
const before = { list: "[0,1,2,4,8]" }; | ||
const result = _1.default(before); | ||
@@ -198,3 +198,3 @@ expect(Array.isArray(result.list)).toBeTruthy(); | ||
test("array of paths", () => { | ||
const before = { somePaths: "index.ts, ./some-relative-path/some_File.txt, ../../hello.world.txt,one.json,./two.json" }; | ||
const before = { "somePaths": "[index.ts, ./some-relative-path/some_File.txt, ../../hello.world.txt,one.json,./two.json]" }; | ||
const result = _1.default(before); | ||
@@ -212,3 +212,19 @@ expect(Array.isArray(result.somePaths)).toBeTruthy(); | ||
}); | ||
test("array of nulls, nested in object", () => { | ||
const before = { list: "[null,null]" }; | ||
const result = _1.default(before); | ||
expect(result.list).toHaveLength(2); | ||
expect(result).toEqual({ | ||
list: [null, null] | ||
}); | ||
}); | ||
test("array of nulls and other values, nested in object", () => { | ||
const before = { list: '[null,null,null, string, 0, 3]' }; | ||
const result = _1.default(before); | ||
expect(result.list).toHaveLength(6); | ||
expect(result).toEqual({ | ||
list: [null, null, null, "string", 0, 3] | ||
}); | ||
}); | ||
}); | ||
//# sourceMappingURL=index.test.js.map |
{ | ||
"name": "parse-strings-in-object", | ||
"version": "1.4.0", | ||
"version": "1.5.0", | ||
"description": "Convert string values in object to boolean and numbers", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -14,10 +14,6 @@ # Parse Strings in JS Object | ||
Array-like strings (currently, only comma-separated values are intepreted like this), 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) | ||
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) | ||
Single-element arrays need you to provide a trailing comma to cue the parser appropriately: | ||
* `"1.1,"` becomes `[1.1]` (single-element array of numbers) | ||
* `"someString,"` becomes `["someString"]` (single-element array of strings) | ||
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). | ||
@@ -24,0 +20,0 @@ |
@@ -173,3 +173,3 @@ import parser from "./"; | ||
test("array of strings", () => { | ||
const before = { list: "test,one,two,three" }; | ||
const before = { list: "[test,one,two,three]" }; | ||
const result = parser(before) as { list: string[] }; | ||
@@ -182,3 +182,3 @@ expect(Array.isArray(result.list)).toBeTruthy(); | ||
test("single-element array (string)", () => { | ||
const before = { list: "one," }; | ||
const before = { list: "[one]" }; | ||
const result = parser(before) as { list: string[] }; | ||
@@ -192,3 +192,3 @@ expect(Array.isArray(result.list)).toBeTruthy(); | ||
test("single-element array (number)", () => { | ||
const before = { list: "0.05," }; | ||
const before = { list: "[0.05]" }; | ||
const result = parser(before) as { list: number[] }; | ||
@@ -203,3 +203,3 @@ expect(Array.isArray(result.list)).toBeTruthy(); | ||
test("array of numnbers", () => { | ||
const before = { list: "0,1,2,4,8" }; | ||
const before = { list: "[0,1,2,4,8]" }; | ||
const result = parser(before) as { list: number[] }; | ||
@@ -212,3 +212,3 @@ expect(Array.isArray(result.list)).toBeTruthy(); | ||
test("array of paths", () => { | ||
const before = { somePaths: "index.ts, ./some-relative-path/some_File.txt, ../../hello.world.txt,one.json,./two.json" }; | ||
const before = { "somePaths": "[index.ts, ./some-relative-path/some_File.txt, ../../hello.world.txt,one.json,./two.json]" }; | ||
const result = parser(before) as { somePaths: string[] }; | ||
@@ -225,5 +225,22 @@ expect(Array.isArray(result.somePaths)).toBeTruthy(); | ||
]}) | ||
}) | ||
test("array of nulls, nested in object", () =>{ | ||
const before = { list: "[null,null]"}; | ||
const result = parser(before) as { list: any[] }; | ||
expect(result.list).toHaveLength(2); | ||
expect(result).toEqual({ | ||
list: [null, null] | ||
}) | ||
}) | ||
test("array of nulls and other values, nested in object", () =>{ | ||
const before = { list: '[null,null,null, string, 0, 3]'}; | ||
const result = parser(before) as { list: any[] }; | ||
expect(result.list).toHaveLength(6); | ||
expect(result).toEqual({ | ||
list: [null, null, null, "string", 0, 3] | ||
}) | ||
}) | ||
}) |
@@ -40,3 +40,4 @@ const parseKeys = <T>(obj: object): T => | ||
if (isArrayLikeString(value) === true) { | ||
return convertArray(arrayLikeStringToArray(value as string)); | ||
const valueWithoutBrackets = (value as string).replace("[", "").replace("]", ""); | ||
return convertArray(arrayLikeStringToArray(valueWithoutBrackets)); | ||
} else { | ||
@@ -58,3 +59,3 @@ // All else fails, return value as is... | ||
const commaSeparated = s.split(","); | ||
if (commaSeparated.length > 1) { | ||
if (commaSeparated.length > 1 || s.includes("[") || s.includes("]")) { | ||
return true; | ||
@@ -70,4 +71,4 @@ } else { | ||
const arrayLikeStringToArray = (s: string, token: string = ",") => | ||
s.split(token).map(element => element.trim()).filter(element => element !== "" && element !== null); | ||
s.split(token).map(element => element.trim()); | ||
export = parseKeys; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
36033
629
100