json-asty 1.2.3 → 1.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-asty",
3
- "version": "1.2.3",
3
+ "version": "1.2.4",
4
4
  "description": "Lossless JSON-to-AST Parser and AST-to-JSON Generator",
5
5
  "keywords": [ "json", "ast", "parse", "generate" ],
6
6
  "browser": "lib/json-asty.browser.js",
@@ -80,12 +80,25 @@ const cli = new CLIio({
80
80
  const opath2xpath = (opath) => {
81
81
  let xpath = ""
82
82
  const segments = opath.split(".")
83
- for (const segment of segments)
84
- xpath += `object / member [ / * [ pos() == 1 && @value == "${segment}" ] ] /`
83
+ for (const segment of segments) {
84
+ const escapedSegment = segment.replace(/\\/g, "\\\\").replace(/"/g, "\\\"")
85
+ xpath += `object / member [ / * [ pos() == 1 && @value == "${escapedSegment}" ] ] /`
86
+ }
85
87
  xpath += " * [ pos() == 2 ]"
86
88
  return xpath
87
89
  }
88
90
 
91
+ /* helper function: query AST and return single node */
92
+ const queryAST = (ast, path) => {
93
+ const query = opath2xpath(path)
94
+ const nodes = ast.query(query)
95
+ if (nodes.length === 0)
96
+ throw new Error(`path not found (no AST nodes matched): "${path}"`)
97
+ if (nodes.length > 1)
98
+ throw new Error(`path is ambiguous (more than one AST node matched): "${path}"`)
99
+ return nodes[0]
100
+ }
101
+
89
102
  /* dispatch commands */
90
103
  if (!argv.value) {
91
104
  /* ==== GET OPERATION ==== */
@@ -97,13 +110,7 @@ const cli = new CLIio({
97
110
  const ast = JsonAsty.parse(json)
98
111
 
99
112
  /* query JSON AST */
100
- const query = opath2xpath(argv.path)
101
- const nodes = ast.query(query)
102
- if (nodes.length === 0)
103
- throw new Error(`path not found (no AST nodes matched): "${argv.path}"`)
104
- if (nodes.length > 1)
105
- throw new Error(`path is ambigous (more than one AST node matched): "${argv.path}"`)
106
- const node = nodes[0]
113
+ const node = queryAST(ast, argv.path)
107
114
 
108
115
  /* fetch value */
109
116
  const value = node.get("value")
@@ -121,20 +128,27 @@ const cli = new CLIio({
121
128
  const ast = JsonAsty.parse(json)
122
129
 
123
130
  /* query JSON AST */
124
- const query = opath2xpath(argv.path)
125
- const nodes = ast.query(query)
126
- if (nodes.length === 0)
127
- throw new Error(`path not found (no AST nodes matched): "${argv.path}"`)
128
- if (nodes.length > 1)
129
- throw new Error(`path is ambigous (more than one AST node matched): "${argv.path}"`)
130
- const node = nodes[0]
131
+ const node = queryAST(ast, argv.path)
131
132
 
132
133
  /* replace value */
133
134
  let value = argv.value
134
- if (argv.type === "number")
135
- value = parseInt(value)
136
- else if (argv.type === "boolean")
137
- value = Boolean(value)
135
+ if (argv.type === "number") {
136
+ value = parseInt(value, 10)
137
+ if (isNaN(value))
138
+ throw new Error(`invalid number value: "${argv.value}"`)
139
+ }
140
+ else if (argv.type === "boolean") {
141
+ if (value === "true")
142
+ value = true
143
+ else if (value === "false")
144
+ value = false
145
+ else
146
+ throw new Error(`invalid boolean value: "${argv.value}" (expected: "true" or "false")`)
147
+ }
148
+ else if (argv.type === "string")
149
+ value = String(value)
150
+ else
151
+ throw new Error(`invalid type: "${argv.type}" (expected: boolean, number, or string)`)
138
152
  const nodeNew = node.create(argv.type).set({ value })
139
153
  node.parent().del(node).add(nodeNew)
140
154