json-asty 1.2.3 → 1.2.5
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 +9 -9
- package/src/json-asty.cli.js +34 -20
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "json-asty",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.5",
|
|
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",
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
"pegjs-otf": "2.0.3",
|
|
25
25
|
"pegjs-util": "2.0.2",
|
|
26
26
|
"chalk": "4.1.2",
|
|
27
|
-
"yargs": "
|
|
27
|
+
"yargs": "18.0.0",
|
|
28
28
|
"cli-io": "0.9.13"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
@@ -43,14 +43,14 @@
|
|
|
43
43
|
"browserify-derequire": "1.1.1",
|
|
44
44
|
"eslint": "8.57.0",
|
|
45
45
|
"eslint-config-standard": "17.1.0",
|
|
46
|
-
"eslint-plugin-promise": "6.
|
|
47
|
-
"eslint-plugin-import": "2.
|
|
46
|
+
"eslint-plugin-promise": "6.6.0",
|
|
47
|
+
"eslint-plugin-import": "2.32.0",
|
|
48
48
|
"eslint-plugin-node": "11.1.0",
|
|
49
|
-
"@babel/core": "7.
|
|
50
|
-
"@babel/preset-env": "7.
|
|
51
|
-
"@babel/plugin-transform-runtime": "7.
|
|
52
|
-
"mocha": "11.
|
|
53
|
-
"chai": "
|
|
49
|
+
"@babel/core": "7.28.4",
|
|
50
|
+
"@babel/preset-env": "7.28.3",
|
|
51
|
+
"@babel/plugin-transform-runtime": "7.28.3",
|
|
52
|
+
"mocha": "11.7.3",
|
|
53
|
+
"chai": "6.2.0"
|
|
54
54
|
},
|
|
55
55
|
"upd": [ "!chalk", "!eslint", "!grunt-eslint" ],
|
|
56
56
|
"scripts": {
|
package/src/json-asty.cli.js
CHANGED
|
@@ -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
|
-
|
|
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
|
|
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
|
|
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
|
-
|
|
137
|
-
|
|
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
|
|