Skip to content

Commit

Permalink
fix: parse hyphen as reserved word in property key on reearth/core (#525
Browse files Browse the repository at this point in the history
)

fix: hyphen reserved word in property key
  • Loading branch information
pyshx committed Mar 9, 2023
1 parent 3c61caf commit d49058b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
Expand Up @@ -46,4 +46,9 @@ describe("replaceVariables", () => {
`czm_va$reearth_opened_square_bracket_$ria$reearth_closed_square_bracket_$ble + czm_variable[0] + czm_variable['key'] + czm_variable["key"] + czm_variable[variable] + czm_variable[variable].nested + czm_variable[variable]['nested']`,
);
});

test("should replace reserved hyphen", () => {
const [result, _] = replaceVariables("${vari-able}");
expect(result).toBe(`czm_vari$reearth_hyphen_$able`);
});
});
Expand Up @@ -105,10 +105,12 @@ const RESERVED_WORDS: Record<string, string> = {
"}": makeReservedWord("closed_curly_bracket"),
"(": makeReservedWord("opened_parentheses"),
")": makeReservedWord("closed_parentheses"),
"-": makeReservedWord("hyphen"),
};

const replaceReservedWord = (word: string) => {
return word.replaceAll(
const wordFiltered = word.replace(/-/g, RESERVED_WORDS["-"]);
return wordFiltered.replaceAll(
/(.*)(\[|\{|\()(.*)(\]|\}|\))(?!\.|\[)(.+)/g,
(_match, prefix, openedBracket, inner, closedBracket, suffix) => {
return `${prefix}${RESERVED_WORDS[openedBracket]}${inner}${RESERVED_WORDS[closedBracket]}${suffix}`;
Expand Down

0 comments on commit d49058b

Please sign in to comment.