Skip to content

Commit

Permalink
Merge pull request #80 from gisce/context_parser_improvements
Browse files Browse the repository at this point in the history
Improve context parser in order to parse JSON objects or return objects directly if needed
  • Loading branch information
mguellsegarra authored May 16, 2023
2 parents 28532cc + f60bda8 commit addc217
Show file tree
Hide file tree
Showing 4 changed files with 82 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.19.1",
"version": "0.19.2",
"main": "./dist/ooui.umd.js",
"module": "./dist/ooui.es.js",
"types": "./dist/index.d.ts",
Expand Down
32 changes: 31 additions & 1 deletion src/helpers/contextParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,27 @@ export const parseContext = ({
values,
fields,
}: {
context?: string;
context?: any;
values?: any;
fields?: any;
}) => {
// TODO: remove try/catch when we know for sure that all the incoming contexts formats are expected
try {
if (!context) return undefined;

if (isObject(context)) {
return context;
}

if (typeof context !== "string") {
return context;
}

const parsedContextInJson = tryParseJSON(context);
if (parsedContextInJson !== null) {
return parsedContextInJson;
}

if (context.trim().length === 0) {
return undefined;
}
Expand Down Expand Up @@ -65,3 +78,20 @@ export const parseContext = ({
return undefined;
}
};

function tryParseJSON(str: string): any | null {
try {
const parsedJSON = JSON.parse(str);
return parsedJSON;
} catch (error) {
return null;
}
}

function isObject(variable: any): boolean {
return (
typeof variable === "object" &&
variable !== null &&
typeof variable !== "string"
);
}
48 changes: 48 additions & 0 deletions src/spec/contextParser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,53 @@ describe("A Context Parser", () => {
expect(parsedContext!["active_id"]).toBeDefined();
expect(parsedContext!["active_id"]).toBe(3);
});

it("should properly parse with double quotes", () => {
const str = '{"active_id": 3}';

const parsedContext = parseContext({ context: str });

expect(parsedContext!["active_id"]).toBeDefined();
expect(parsedContext!["active_id"]).toBe(3);
});

it("should properly return a object when context is a object", () => {
const ctx = {
person: {
name: "John Doe",
age: 30,
address: {
street: "123 Main St",
city: "Exampleville",
country: "Exampleland",
},
},
};

const parsedContext = parseContext({ context: ctx });

expect(parsedContext!["person"]).toBeDefined();
expect(parsedContext!["person"].name).toBe("John Doe");
expect(JSON.stringify(parsedContext)).toBe(JSON.stringify(ctx));
});
});

it("should properly return a parsed object when context is a string json", () => {
const ctx = {
person: {
name: "John Doe",
age: 30,
address: {
street: "123 Main St",
city: "Exampleville",
country: "Exampleland",
},
},
};

const parsedContext = parseContext({ context: JSON.stringify(ctx) });
expect(parsedContext!["person"]).toBeDefined();
expect(parsedContext!["person"].name).toBe("John Doe");
expect(JSON.stringify(parsedContext)).toBe(JSON.stringify(ctx));
});
});

0 comments on commit addc217

Please sign in to comment.