-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(web): parse properties(feature) recursively before using in evalu…
…ator at core (#479)
- Loading branch information
Showing
10 changed files
with
194 additions
and
24 deletions.
There are no files selected for viewing
8 changes: 0 additions & 8 deletions
8
web/src/beta/lib/core/mantle/evaluator/simple/expression/utils.ts
This file was deleted.
Oops, something went wrong.
5 changes: 4 additions & 1 deletion
5
web/src/beta/lib/core/mantle/evaluator/simple/expression/variableReplacer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
web/src/beta/lib/core/mantle/evaluator/simple/utils.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { expect, test, describe } from "vitest"; | ||
|
||
import { recursiveJSONParse } from "./utils"; // Import the function from your module/file | ||
|
||
describe("recursiveJSONParse", () => { | ||
test("should parse nested JSON strings within an object", () => { | ||
const obj = { | ||
name: "John", | ||
age: "25", | ||
data: '{"city": "New York", "country": "USA"}', | ||
}; | ||
const expected = { | ||
name: "John", | ||
age: "25", | ||
data: { | ||
city: "New York", | ||
country: "USA", | ||
}, | ||
}; | ||
|
||
const result = recursiveJSONParse(obj); | ||
expect(result).toEqual(expected); | ||
}); | ||
|
||
test("should handle non-object values and null", () => { | ||
const str = "Test"; | ||
const num = 123; | ||
const bool = true; | ||
const arr = ["one", "two"]; | ||
const emptyObj = {}; | ||
const nullValue = null; | ||
|
||
expect(recursiveJSONParse(str)).toBe(str); | ||
expect(recursiveJSONParse(num)).toBe(num); | ||
expect(recursiveJSONParse(bool)).toBe(bool); | ||
expect(recursiveJSONParse(arr)).toEqual(arr); | ||
expect(recursiveJSONParse(emptyObj)).toEqual(emptyObj); | ||
expect(recursiveJSONParse(nullValue)).toBe(nullValue); | ||
}); | ||
|
||
test("should handle invalid JSON strings", () => { | ||
const obj = { | ||
name: "John", | ||
age: "25", | ||
data: '{city: "New York", country: "USA"}', // Invalid JSON string | ||
}; | ||
const expected = { | ||
name: "John", | ||
age: "25", | ||
data: '{city: "New York", country: "USA"}', // Invalid JSON string remains unchanged | ||
}; | ||
|
||
const result = recursiveJSONParse(obj); | ||
expect(result).toEqual(expected); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const usableChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; | ||
|
||
export const generateRandomString = (len: number): string => { | ||
return Array.from(window.crypto.getRandomValues(new Uint8Array(len))) | ||
.map(n => usableChars[n % len]) | ||
.join("") | ||
.toLowerCase(); | ||
}; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export const recursiveJSONParse = (obj: any): any => { | ||
if (typeof obj !== "object" || obj === null) { | ||
return obj; | ||
} | ||
|
||
for (const key in obj) { | ||
if (typeof obj[key] === "string") { | ||
try { | ||
if (obj[key].startsWith("{") && obj[key].endsWith("}")) { | ||
obj[key] = JSON.parse(obj[key]); | ||
} | ||
} catch (error) { | ||
console.error("Invalid JSON:", obj[key]); | ||
} | ||
} else if (typeof obj[key] === "object") { | ||
obj[key] = recursiveJSONParse(obj[key]); | ||
} | ||
} | ||
|
||
return obj; | ||
}; |
8 changes: 0 additions & 8 deletions
8
web/src/classic/core/mantle/evaluator/simple/expression/utils.ts
This file was deleted.
Oops, something went wrong.
3 changes: 2 additions & 1 deletion
3
web/src/classic/core/mantle/evaluator/simple/expression/variableReplacer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
web/src/classic/core/mantle/evaluator/simple/utils.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { expect, test, describe } from "vitest"; | ||
|
||
import { recursiveJSONParse } from "./utils"; // Import the function from your module/file | ||
|
||
describe("recursiveJSONParse", () => { | ||
test("should parse nested JSON strings within an object", () => { | ||
const obj = { | ||
name: "John", | ||
age: "25", | ||
data: '{"city": "New York", "country": "USA"}', | ||
}; | ||
const expected = { | ||
name: "John", | ||
age: "25", | ||
data: { | ||
city: "New York", | ||
country: "USA", | ||
}, | ||
}; | ||
|
||
const result = recursiveJSONParse(obj); | ||
expect(result).toEqual(expected); | ||
}); | ||
|
||
test("should handle non-object values and null", () => { | ||
const str = "Test"; | ||
const num = 123; | ||
const bool = true; | ||
const arr = ["one", "two"]; | ||
const emptyObj = {}; | ||
const nullValue = null; | ||
|
||
expect(recursiveJSONParse(str)).toBe(str); | ||
expect(recursiveJSONParse(num)).toBe(num); | ||
expect(recursiveJSONParse(bool)).toBe(bool); | ||
expect(recursiveJSONParse(arr)).toEqual(arr); | ||
expect(recursiveJSONParse(emptyObj)).toEqual(emptyObj); | ||
expect(recursiveJSONParse(nullValue)).toBe(nullValue); | ||
}); | ||
|
||
test("should handle invalid JSON strings", () => { | ||
const obj = { | ||
name: "John", | ||
age: "25", | ||
data: '{city: "New York", country: "USA"}', // Invalid JSON string | ||
}; | ||
const expected = { | ||
name: "John", | ||
age: "25", | ||
data: '{city: "New York", country: "USA"}', // Invalid JSON string remains unchanged | ||
}; | ||
|
||
const result = recursiveJSONParse(obj); | ||
expect(result).toEqual(expected); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const usableChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; | ||
|
||
export const generateRandomString = (len: number): string => { | ||
return Array.from(window.crypto.getRandomValues(new Uint8Array(len))) | ||
.map(n => usableChars[n % len]) | ||
.join("") | ||
.toLowerCase(); | ||
}; | ||
|
||
export const recursiveJSONParse = (obj: any): any => { | ||
if (typeof obj !== "object" || obj === null) { | ||
return obj; | ||
} | ||
|
||
for (const key in obj) { | ||
if (typeof obj[key] === "string") { | ||
try { | ||
if (obj[key].startsWith("{") && obj[key].endsWith("}")) { | ||
obj[key] = JSON.parse(obj[key]); | ||
} | ||
} catch (error) { | ||
console.error("Invalid JSON:", obj[key]); | ||
} | ||
} else if (typeof obj[key] === "object") { | ||
obj[key] = recursiveJSONParse(obj[key]); | ||
} | ||
} | ||
|
||
return obj; | ||
}; |