Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

parse-strings-in-object

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

parse-strings-in-object - npm Package Compare versions

Comparing version 1.5.0 to 1.6.0

27

dist/index.js

@@ -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

55

dist/index.test.js

@@ -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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc