Skip to content

Commit

Permalink
Improve Elm string representation parsing in a few edge cases
Browse files Browse the repository at this point in the history
Closes #15
  • Loading branch information
kachkaev committed Mar 17, 2019
1 parent d0daae9 commit 95d8a83
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
35 changes: 35 additions & 0 deletions packages/elm-string-representation/src/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ export const testCases: Array<{
input: ['"42"'],
output: "42",
},
{
input: ["True"],
output: true,
},
{
input: ["False"],
output: false,
},
{
input: ["<function>"],
output: "<function>",
},
{
input: ['"Hello World"'],
output: "Hello World",
Expand Down Expand Up @@ -134,6 +146,10 @@ export const testCases: Array<{
b: 42,
},
},
{
input: ["( False, True )", "(False,True)"],
output: [false, true],
},
{
input: ["( 1, 2 )", "(1,2)"],
output: [1, 2],
Expand All @@ -160,6 +176,25 @@ export const testCases: Array<{
b: [1, 2, 3],
},
},
{
input: [
'{ booItem = True, fnItem = <function>, intItem = 1, lstItem = [3,4,5], rcdItem = { nestedName = "fourteen", nestedValue = 15 }, strItem = "two", tLsItem = [(1,11),(1.2,13)], tp3Item = (False,8,"nine"), tplItem = (6,7) }',
],
output: {
booItem: true,
fnItem: "<function>",
intItem: 1,
lstItem: [3, 4, 5],
rcdItem: {
nestedName: "fourteen",
nestedValue: 15,
},
strItem: "two",
tLsItem: [[1, 11], [1.2, 13]],
tp3Item: [false, 8, "nine"],
tplItem: [6, 7],
},
},
{
input: [
"{ x = {",
Expand Down
23 changes: 15 additions & 8 deletions packages/elm-string-representation/src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,21 @@ export default (text: string): any => {
insideString = false;
}
} else {
outputChunks.push(
chunk
.replace(/ = True/g, " = true")
.replace(/ = False/g, " = false")
.replace(/(,|{)\s*(|([$a-zA-Z_0-9 ]*)\s+)= /g, '$1 "$3": ')
.replace(/\(/g, "[")
.replace(/\)/g, "]"),
);
if (chunk === "True") {
outputChunks.push("true");
} else if (chunk === "False") {
outputChunks.push("false");
} else {
outputChunks.push(
chunk
.replace(/\<function\>/g, '"<function>"')
.replace(/([^$a-zA-Z_0-9])True([^$a-zA-Z_0-9])/g, "$1true$2")
.replace(/([^$a-zA-Z_0-9])False([^$a-zA-Z_0-9])/g, "$1false$2")
.replace(/(,|{)\s*(|([$a-zA-Z_0-9 ]*)\s+)= /g, '$1 "$3": ')
.replace(/\(/g, "[")
.replace(/\)/g, "]"),
);
}
insideString = true;
}
});
Expand Down

0 comments on commit 95d8a83

Please sign in to comment.