Skip to content

Commit

Permalink
SB: Added more test cases and escape string function
Browse files Browse the repository at this point in the history
  • Loading branch information
00salmon committed Nov 27, 2019
1 parent b2c73a3 commit 70e2d99
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@ import { JsonataASTNode, ObjectUnaryNode, ArrayUnaryNode } from "./types";
// TODO: Should eventually replace this with types from the `jsonata` package once they're complete.
type AST = JsonataASTNode;

export function escapeString(name:string){
if (
/\s/.test(name)
|| ["null", "false", "true"].includes(name)
|| /^\d/.test(name)
|| !(/^[a-zA-Z()]+$/.test(name))
) {
return "`" + name + "`";
}
return name;
}

export default function serializer(node: AST): string {
if(!node) return undefined;
if (node.type === "binary") {
Expand Down Expand Up @@ -61,10 +73,7 @@ export default function serializer(node: AST): string {
bind = "@" + index + focus
}
let name = node.value;
if (/\s/.test(name) || ["null", "false", "true"].includes(name)) {
// Escaped for whitespace
name = "`" + name + "`";
}
name = escapeString(name);
return name + bind + stages;
} else if (node.type === "filter") {
return "[" + serializer(node.expr) + "]";
Expand Down
25 changes: 25 additions & 0 deletions test/features/Serializer.feature
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,30 @@ Feature: AST Serializer
| loans@$foo#$bar |
| loans@$foo |
| loans#$bar |
| foo-bar |
| foo+bar |
| foo/bar |
| foo*bar |
| `😊` |
| "foo-bar" |
| "foo+bar" |
| "foo/bar" |
| "foo*bar" |
| "foo-bar" |
| "foo+bar" |
| "foo/bar" |
| "foo*bar" |
| "\"" |
| foo_bar |
| `foo_bar` |
| foo1bar |
| foobar1 |
| foobar_1 |
| `1foobar` |
| `foo-bar` |
| `foo+bar` |
| `foo/bar` |
| `foo*bar` |
| `foobar-1` |


0 comments on commit 70e2d99

Please sign in to comment.