parse-strings-in-object
Advanced tools
Comparing version 1.5.0 to 1.6.0
@@ -8,3 +8,2 @@ "use strict"; | ||
const convert = (value) => { | ||
let result = value; | ||
if (typeof value === "object" && value !== null) { | ||
@@ -34,8 +33,8 @@ if (Array.isArray(value)) { | ||
else { | ||
if (isArrayLikeString(value) === true) { | ||
const valueWithoutBrackets = value.replace("[", "").replace("]", ""); | ||
return convertArray(arrayLikeStringToArray(valueWithoutBrackets)); | ||
try { | ||
return JSON.parse(value); | ||
} | ||
else { | ||
// All else fails, return value as is... | ||
catch (e) { | ||
// All else fails, just return the value as-is... | ||
// console.warn(`Failed to parse "${value}" (${e})`); | ||
return value; | ||
@@ -45,21 +44,5 @@ } | ||
} | ||
return result; | ||
}; | ||
const convertArray = (a) => a.map(el => convert(el)); | ||
const isArrayLikeString = (s) => { | ||
if (typeof s === "string") { | ||
const commaSeparated = s.split(","); | ||
if (commaSeparated.length > 1 || s.includes("[") || s.includes("]")) { | ||
return true; | ||
} | ||
else { | ||
return false; | ||
} | ||
} | ||
else { | ||
return false; | ||
} | ||
}; | ||
const arrayLikeStringToArray = (s, token = ",") => s.split(token).map(element => element.trim()); | ||
module.exports = parseKeys; | ||
//# sourceMappingURL=index.js.map |
@@ -17,3 +17,3 @@ "use strict"; | ||
const result = _1.default(before); | ||
// expect(result).to.equal({ foo: true, bar: false }); | ||
expect(result).toEqual({ foo: true, bar: null }); | ||
expect(result.foo).toBe(true); | ||
@@ -164,3 +164,3 @@ expect(result.bar).toBe(null); | ||
test("array of strings", () => { | ||
const before = { list: "[test,one,two,three]" }; | ||
const before = { list: '["test","one","two","three"]' }; | ||
const result = _1.default(before); | ||
@@ -172,7 +172,7 @@ expect(Array.isArray(result.list)).toBeTruthy(); | ||
test("single-element array (string)", () => { | ||
const before = { list: "[one]" }; | ||
const before = { list: '["one"]' }; | ||
const result = _1.default(before); | ||
expect(Array.isArray(result.list)).toBeTruthy(); | ||
expect(typeof result.list).toBe("object"); | ||
expect(result.list.length).toEqual(1); | ||
// expect(Array.isArray(result.list)).toBeTruthy(); | ||
// expect(typeof result.list).toBe("object"); | ||
// expect(result.list.length).toEqual(1); | ||
expect(result).toEqual({ list: ["one"] }); | ||
@@ -197,3 +197,3 @@ }); | ||
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); | ||
@@ -220,10 +220,47 @@ expect(Array.isArray(result.somePaths)).toBeTruthy(); | ||
test("array of nulls and other values, nested in object", () => { | ||
const before = { list: '[null,null,null, string, 0, 3]' }; | ||
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] | ||
list: [null, null, null, "string", "0", 3] | ||
}); | ||
}); | ||
test("various arrays nested", () => { | ||
const before = { | ||
one: '["one", "two", "three"]', | ||
another: '[0,1,2]', | ||
nestedStuff: `{ | ||
"thisOne": ["single"], | ||
"anotherOne": [ | ||
{ | ||
"foo": "bar", | ||
"hello": 0 | ||
}, | ||
{ | ||
"oh": "yeah", | ||
"more": [null, null, null, 101, 102, 103] | ||
} | ||
] | ||
}` | ||
}; | ||
const result = _1.default(before); | ||
expect(result).toEqual({ | ||
one: ["one", "two", "three"], | ||
another: [0, 1, 2], | ||
nestedStuff: { | ||
thisOne: ["single"], | ||
anotherOne: [ | ||
{ | ||
foo: "bar", | ||
hello: 0 | ||
}, | ||
{ | ||
oh: "yeah", | ||
more: [null, null, null, 101, 102, 103] | ||
} | ||
] | ||
} | ||
}); | ||
}); | ||
}); | ||
//# sourceMappingURL=index.test.js.map |
{ | ||
"name": "parse-strings-in-object", | ||
"version": "1.5.0", | ||
"version": "1.6.0", | ||
"description": "Convert string values in object to boolean and numbers", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -14,3 +14,3 @@ import parser from "./"; | ||
const result = parser(before) as typeof before; | ||
// expect(result).to.equal({ foo: true, bar: false }); | ||
expect(result).toEqual({ foo: true, bar: null }); | ||
expect(result.foo).toBe(true); | ||
@@ -174,3 +174,3 @@ expect(result.bar).toBe(null); | ||
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[] }; | ||
@@ -183,7 +183,7 @@ 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[] }; | ||
expect(Array.isArray(result.list)).toBeTruthy(); | ||
expect(typeof result.list).toBe("object"); | ||
expect(result.list.length).toEqual(1); | ||
// expect(Array.isArray(result.list)).toBeTruthy(); | ||
// expect(typeof result.list).toBe("object"); | ||
// expect(result.list.length).toEqual(1); | ||
expect(result).toEqual({ list: ["one"]}); | ||
@@ -211,3 +211,3 @@ }) | ||
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[] }; | ||
@@ -236,10 +236,48 @@ expect(Array.isArray(result.somePaths)).toBeTruthy(); | ||
test("array of nulls and other values, nested in object", () =>{ | ||
const before = { list: '[null,null,null, string, 0, 3]'}; | ||
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] | ||
list: [null, null, null, "string", "0", 3] | ||
}) | ||
}) | ||
test("various arrays nested", () => { | ||
const before = { | ||
one: '["one", "two", "three"]', | ||
another: '[0,1,2]', | ||
nestedStuff: `{ | ||
"thisOne": ["single"], | ||
"anotherOne": [ | ||
{ | ||
"foo": "bar", | ||
"hello": 0 | ||
}, | ||
{ | ||
"oh": "yeah", | ||
"more": [null, null, null, 101, 102, 103] | ||
} | ||
] | ||
}` | ||
} | ||
const result = parser(before); | ||
expect(result).toEqual({ | ||
one: ["one", "two", "three"], | ||
another: [0,1,2], | ||
nestedStuff: { | ||
thisOne: ["single"], | ||
anotherOne: [ | ||
{ | ||
foo: "bar", | ||
hello: 0 | ||
}, | ||
{ | ||
oh: "yeah", | ||
more: [null, null, null, 101, 102, 103] | ||
} | ||
] | ||
} | ||
}) | ||
}) | ||
}) |
@@ -11,3 +11,2 @@ const parseKeys = <T>(obj: object): T => | ||
const convert = (value: string | any[]): any => { | ||
let result: any = value; | ||
@@ -40,9 +39,9 @@ if (typeof value === "object" && value !== null) { | ||
if (isArrayLikeString(value) === true) { | ||
const valueWithoutBrackets = (value as string).replace("[", "").replace("]", ""); | ||
return convertArray(arrayLikeStringToArray(valueWithoutBrackets)); | ||
} else { | ||
// All else fails, return value as is... | ||
return value; | ||
} | ||
try { | ||
return JSON.parse(value as string); | ||
}catch(e) { | ||
// All else fails, just return the value as-is... | ||
// console.warn(`Failed to parse "${value}" (${e})`); | ||
return value; | ||
} | ||
@@ -52,3 +51,2 @@ } | ||
return result; | ||
}; | ||
@@ -58,18 +56,5 @@ | ||
const isArrayLikeString = (s: any): boolean => { | ||
if (typeof s === "string") { | ||
const commaSeparated = s.split(","); | ||
if (commaSeparated.length > 1 || s.includes("[") || s.includes("]")) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} else { | ||
return false; | ||
} | ||
} | ||
const arrayLikeStringToArray = (s: string, token: string = ",") => | ||
s.split(token).map(element => element.trim()); | ||
// 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
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
36664
671