Skip to content

Commit

Permalink
Merge pull request #90 from gisce/fix/context_json_parsing
Browse files Browse the repository at this point in the history
Fix parse json context
  • Loading branch information
mguellsegarra authored Sep 4, 2023
2 parents 03f3d68 + 8d55257 commit 2455c35
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 4 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gisce/ooui",
"version": "0.22.0",
"version": "0.22.1",
"main": "./dist/ooui.umd.js",
"module": "./dist/ooui.es.js",
"types": "./dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/contextParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export const parseContext = ({

function tryParseJSON(str: string): any | null {
try {
const parsedJSON = JSON.parse(str);
const parsedJSON = JSON.parse(str.replace(/'/g, '"'));
return parsedJSON;
} catch (error) {
return null;
Expand Down
6 changes: 6 additions & 0 deletions src/spec/contextParser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,10 @@ describe("A Context Parser", () => {
expect(parsedContext!["person"].name).toBe("John Doe");
expect(JSON.stringify(parsedContext)).toBe(JSON.stringify(ctx));
});

it.only("should parse context with simple single quotes in a string", () => {
const string = "{'contract_id': 1, 'contract_ids': [1, 3]}";
const parsedContext = parseContext({ context: string });
expect(parsedContext!["contract_id"]).toBe(1);
});
});

0 comments on commit 2455c35

Please sign in to comment.