Skip to content

Commit 453b8ce

Browse files
committed
js-core-1.9.1 - fix json string inline function parsing issues
1 parent e89b342 commit 453b8ce

File tree

5 files changed

+49
-34
lines changed

5 files changed

+49
-34
lines changed

javascript/json-transform-core/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

javascript/json-transform-core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@nlighten/json-transform-core",
33
"description": "Core types and utilities for handling JSON transformers",
4-
"version": "1.9.0",
4+
"version": "1.9.1",
55
"main": "./dist/index.js",
66
"source": "src/index.ts",
77
"exports": "./dist/index.js",

javascript/json-transform-core/src/__tests__/functions/functionsParser.test.ts

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -811,9 +811,9 @@ describe("functions schema detection", () => {
811811

812812
describe("matchAllInlineFunctionsInLine", () => {
813813
test(`sanity`, () => {
814-
expect(functionsParser.matchAllInlineFunctionsInLine(" : \"$$foo(1,2):$$bar(abc,'def'):$$\" ")).toEqual([
814+
expect(functionsParser.matchAllInlineFunctionsInLine(" : \"$$pad(1,2):$$wrap(abc,'def'):$$\" ")).toEqual([
815815
{
816-
name: "foo",
816+
name: "pad",
817817
keyLength: 5,
818818
args: [
819819
{
@@ -830,30 +830,30 @@ describe("matchAllInlineFunctionsInLine", () => {
830830
index: 5,
831831
input: {
832832
index: 16,
833-
length: 19,
834-
value: "$$bar(abc,'def'):$$",
833+
length: 18,
834+
value: "$$wrap(abc,'def'):$$",
835835
},
836836
},
837837
{
838-
keyLength: 5,
839-
name: "bar",
838+
keyLength: 6,
839+
name: "wrap",
840840
args: [
841841
{
842-
index: 22,
842+
index: 23,
843843
length: 3,
844844
value: "abc",
845845
},
846846
{
847-
index: 26,
847+
index: 27,
848848
length: 5,
849849
value: "def",
850850
},
851851
],
852852
index: 16,
853853
input: {
854-
index: 33,
855-
length: 6,
856-
value: '$$" ',
854+
index: 34,
855+
length: 2,
856+
value: "$$",
857857
},
858858
},
859859
]);
@@ -889,4 +889,29 @@ describe("matchAllInlineFunctionsInLine", () => {
889889
},
890890
]);
891891
});
892+
893+
test(`sanity 3`, () => {
894+
expect(functionsParser.matchAllInlineFunctionsInLine(`"$$pad:$$wrap:"`)).toEqual([
895+
{
896+
index: 1,
897+
input: {
898+
index: 7,
899+
length: 7,
900+
value: "$$wrap:",
901+
},
902+
keyLength: 5,
903+
name: "pad",
904+
},
905+
{
906+
index: 7,
907+
input: {
908+
index: 14,
909+
length: 0,
910+
value: "",
911+
},
912+
keyLength: 6,
913+
name: "wrap",
914+
},
915+
]);
916+
});
892917
});

javascript/json-transform-core/src/__tests__/utils/convert.test.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,7 @@ describe("convert", () => {
5353
},
5454
to: "$$substring(0,5):##current.name",
5555
}),
56-
).toEqual({
57-
$$map: "$$flat:$.names",
58-
to: "$$substring(0,5):##current.name",
59-
});
56+
).toEqual("$$map('$$substring(0,5):##current.name'):$$flat:$.names");
6057
});
6158
test("object to inline - test 5", () => {
6259
expect(
@@ -130,19 +127,11 @@ describe("convert", () => {
130127
}),
131128
).toEqual({
132129
"*": "$",
130+
age: "$$math('$$math(\\'$$date(EPOCH):#now\\',-,\\'$$date(EPOCH):$.date_of_birth\\')',//,'$$math(365,*,\\'$$math(24,*,3600)\\')')",
133131
full_name: {
134132
$$join: ["$.first_name", "$.last_name"],
135133
delimiter: " ",
136134
},
137-
age: {
138-
$$math: [
139-
{
140-
$$math: ["$$date(EPOCH):#now", "-", "$$date(EPOCH):$.date_of_birth"],
141-
},
142-
"//",
143-
"$$math(365,*,'$$math(24,*,3600)')",
144-
],
145-
},
146135
});
147136
});
148137

javascript/json-transform-core/src/functions/functionsParser.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -193,14 +193,16 @@ class FunctionsParser {
193193
const matches: ({ index: number } & TokenizedInlineFunction)[] = [];
194194
let match: TokenizedInlineFunction | undefined;
195195
let indexOffset = line.indexOf("$$");
196-
let str = line.substring(indexOffset).trimEnd();
197-
if (line[indexOffset - 1] === '"' && str.at(-1) === '"') {
198-
str = str.slice(0, -1).replace(/\\'/g, "\\\\'");
199-
// try {
200-
// str = JSON.parse('"' + str); //.replace(/\\/g, "\\\\"); // duplicate all backslashes to get the right lengths
201-
// } catch (e: any) {}
196+
line = line.trimEnd();
197+
if (line[indexOffset - 1] === '"' && line.at(-1) === '"') {
198+
line = line.slice(0, -1).replace(/\\'/g, "\\\\'");
202199
}
200+
let str = line.substring(indexOffset);
201+
203202
while ((match = tokenizeInlineFunction(str))) {
203+
if (!functionsParser.get(match.name)) {
204+
break; // not a known function name (so not a function)
205+
}
204206
(match as any).index = indexOffset; // add index to match
205207
match.args?.forEach(arg => {
206208
arg.index += indexOffset;
@@ -221,7 +223,6 @@ class FunctionsParser {
221223
indexOffset = match.input.index;
222224
str = line.substring(match.input.index);
223225
}
224-
console.debug("debug", matches);
225226
return matches;
226227
}
227228
}

0 commit comments

Comments
 (0)