@twin.org/data-json-path 0.0.3-next.7 → 0.0.3-next.9
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/dist/es/index.js +1 -0
- package/dist/es/index.js.map +1 -1
- package/dist/es/jsonPathHelper.js +100 -0
- package/dist/es/jsonPathHelper.js.map +1 -1
- package/dist/es/models/IJsonPathLocation.js +4 -0
- package/dist/es/models/IJsonPathLocation.js.map +1 -0
- package/dist/es/models/IJsonPathResult.js +0 -2
- package/dist/es/models/IJsonPathResult.js.map +1 -1
- package/dist/types/index.d.ts +1 -0
- package/dist/types/jsonPathHelper.d.ts +14 -0
- package/dist/types/models/IJsonPathLocation.d.ts +4 -0
- package/dist/types/models/IJsonPathResult.d.ts +2 -1
- package/docs/changelog.md +14 -0
- package/docs/reference/classes/JsonPathHelper.md +58 -0
- package/docs/reference/index.md +4 -0
- package/docs/reference/interfaces/IJsonPathResult.md +1 -1
- package/docs/reference/type-aliases/IJsonPathLocation.md +5 -0
- package/package.json +1 -1
package/dist/es/index.js
CHANGED
package/dist/es/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,cAAc,qBAAqB,CAAC;AACpC,cAAc,6BAA6B,CAAC","sourcesContent":["// Copyright 2025 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nexport * from \"./jsonPathHelper.js\";\nexport * from \"./models/IJsonPathResult.js\";\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,cAAc,qBAAqB,CAAC;AACpC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,6BAA6B,CAAC","sourcesContent":["// Copyright 2025 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nexport * from \"./jsonPathHelper.js\";\nexport * from \"./models/IJsonPathLocation.js\";\nexport * from \"./models/IJsonPathResult.js\";\n"]}
|
|
@@ -114,5 +114,105 @@ export class JsonPathHelper {
|
|
|
114
114
|
}, BaseError.fromError(error));
|
|
115
115
|
}
|
|
116
116
|
}
|
|
117
|
+
/**
|
|
118
|
+
* Set a value on the target object using a JSONPath location.
|
|
119
|
+
* @param root The target root object.
|
|
120
|
+
* @param location The JSONPath location tokens.
|
|
121
|
+
* @param value The value to set.
|
|
122
|
+
*/
|
|
123
|
+
static setAtLocation(root, location, value) {
|
|
124
|
+
if (!Is.arrayValue(location) || location.length === 0) {
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
let current = root;
|
|
128
|
+
for (let i = 0; i < location.length - 1; i++) {
|
|
129
|
+
const token = location[i];
|
|
130
|
+
const nextToken = location[i + 1];
|
|
131
|
+
if (!Is.array(current) && !Is.object(current)) {
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
if (Is.number(token)) {
|
|
135
|
+
if (!Is.array(current)) {
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
const existing = current[token];
|
|
139
|
+
if (!Is.array(existing) && !Is.object(existing)) {
|
|
140
|
+
current[token] = Is.number(nextToken) ? [] : {};
|
|
141
|
+
}
|
|
142
|
+
current = current[token];
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
if (!Is.object(current)) {
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
const existing = current[token];
|
|
149
|
+
if (!Is.array(existing) && !Is.object(existing)) {
|
|
150
|
+
current[token] = Is.number(nextToken) ? [] : {};
|
|
151
|
+
}
|
|
152
|
+
current = current[token];
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
const lastToken = location[location.length - 1];
|
|
156
|
+
if (!Is.array(current) && !Is.object(current)) {
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
if (Is.number(lastToken)) {
|
|
160
|
+
if (!Is.array(current)) {
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
current[lastToken] = value;
|
|
164
|
+
}
|
|
165
|
+
else {
|
|
166
|
+
if (!Is.object(current)) {
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
current[lastToken] = value;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Delete a value on the target object using a JSONPath location.
|
|
174
|
+
* @param root The target root object.
|
|
175
|
+
* @param location The JSONPath location tokens.
|
|
176
|
+
*/
|
|
177
|
+
static deleteAtLocation(root, location) {
|
|
178
|
+
if (!Is.arrayValue(location) || location.length === 0) {
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
let current = root;
|
|
182
|
+
for (let i = 0; i < location.length - 1; i++) {
|
|
183
|
+
const token = location[i];
|
|
184
|
+
if (!Is.array(current) && !Is.object(current)) {
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
if (Is.number(token)) {
|
|
188
|
+
if (!Is.array(current)) {
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
current = current[token];
|
|
192
|
+
}
|
|
193
|
+
else {
|
|
194
|
+
if (!Is.object(current)) {
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
current = current[token];
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
if (!Is.array(current) && !Is.object(current)) {
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
const lastToken = location[location.length - 1];
|
|
204
|
+
if (Is.number(lastToken)) {
|
|
205
|
+
if (!Is.array(current)) {
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
current[lastToken] = undefined;
|
|
209
|
+
}
|
|
210
|
+
else {
|
|
211
|
+
if (!Is.object(current)) {
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
delete current[lastToken];
|
|
215
|
+
}
|
|
216
|
+
}
|
|
117
217
|
}
|
|
118
218
|
//# sourceMappingURL=jsonPathHelper.js.map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;;;;;OAKG;IACI,MAAM,CAAC,QAAQ,CAAC,IAAY;QAClC,IAAI,CAAC,EAAE,CAAC,WAAW,CAAC,IAAI,CAAC,EAAE,CAAC;YAC3B,OAAO,KAAK,CAAC;QACd,CAAC;QAED,IAAI,CAAC;YACJ,yCAAyC;YACzC,QAAQ,CAAC,KAAK,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;YACzB,OAAO,IAAI,CAAC;QACb,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YAChB,MAAM,IAAI,YAAY,CACrB,cAAc,CAAC,UAAU,EACzB,gBAAgB,EAChB;gBACC,IAAI;aACJ,EACD,SAAS,CAAC,SAAS,CAAC,KAAK,CAAC,CAC1B,CAAC;QACH,CAAC;IACF,CAAC","sourcesContent":["// Copyright 2025 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { BaseError, GeneralError, Guards, Is } from \"@twin.org/core\";\nimport { nameof } from \"@twin.org/nameof\";\nimport { jsonpath, type JSONValue } from \"json-p3\";\nimport type { IJsonPathResult } from \"./models/IJsonPathResult.js\";\n\n/**\n * Helper class for JSONPath operations.\n * Provides abstraction over the json-p3 library.\n */\nexport class JsonPathHelper {\n\t/**\n\t * The name of the class name of the JsonPathHelper class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<JsonPathHelper>();\n\n\t/**\n\t * Execute a JSONPath query and return results with values and locations.\n\t * @param path The JSONPath query string (e.g., \"$.store.book[*].title\").\n\t * @param data The data to query.\n\t * @returns Array of results containing values and their locations.\n\t * @throws GeneralError if the path is invalid or data cannot be queried.\n\t */\n\tpublic static query(path: string, data: unknown): IJsonPathResult[] {\n\t\tGuards.stringValue(JsonPathHelper.CLASS_NAME, nameof(path), path);\n\n\t\ttry {\n\t\t\tconst results = jsonpath.query(path, data as JSONValue);\n\t\t\t// Convert JSONPathNodeList to array of IJsonPathResult\n\t\t\tconst resultArray: IJsonPathResult[] = [];\n\t\t\tfor (const node of results) {\n\t\t\t\tresultArray.push({\n\t\t\t\t\tvalue: node.value,\n\t\t\t\t\tlocation: node.location,\n\t\t\t\t\tpath: node.getPath({ form: \"canonical\" })\n\t\t\t\t});\n\t\t\t}\n\t\t\treturn resultArray;\n\t\t} catch (error) {\n\t\t\tthrow new GeneralError(\n\t\t\t\tJsonPathHelper.CLASS_NAME,\n\t\t\t\t\"query\",\n\t\t\t\t{\n\t\t\t\t\tpath\n\t\t\t\t},\n\t\t\t\tBaseError.fromError(error)\n\t\t\t);\n\t\t}\n\t}\n\n\t/**\n\t * Check if a JSONPath exists in the data.\n\t * @param path The JSONPath query string.\n\t * @param data The data to check.\n\t * @returns True if the path exists and returns at least one result.\n\t */\n\tpublic static exists(path: string, data: unknown): boolean {\n\t\tGuards.stringValue(JsonPathHelper.CLASS_NAME, nameof(path), path);\n\n\t\ttry {\n\t\t\tconst results = jsonpath.query(path, data as JSONValue);\n\t\t\treturn !results.empty();\n\t\t} catch {\n\t\t\treturn false;\n\t\t}\n\t}\n\n\t/**\n\t * Extract the first value from a JSONPath query.\n\t * @param path The JSONPath query string.\n\t * @param data The data to query.\n\t * @returns The first matched value, or undefined if no matches.\n\t * @throws GeneralError if the path is invalid or data cannot be queried.\n\t */\n\tpublic static extractSingle(path: string, data: unknown): unknown {\n\t\tGuards.stringValue(JsonPathHelper.CLASS_NAME, nameof(path), path);\n\n\t\ttry {\n\t\t\tconst node = jsonpath.match(path, data as JSONValue);\n\t\t\treturn node?.value;\n\t\t} catch (error) {\n\t\t\tthrow new GeneralError(\n\t\t\t\tJsonPathHelper.CLASS_NAME,\n\t\t\t\t\"extractSingleFailed\",\n\t\t\t\t{\n\t\t\t\t\tpath\n\t\t\t\t},\n\t\t\t\tBaseError.fromError(error)\n\t\t\t);\n\t\t}\n\t}\n\n\t/**\n\t * Extract all values from a JSONPath query.\n\t * @param path The JSONPath query string.\n\t * @param data The data to query.\n\t * @returns Array of all matched values.\n\t * @throws GeneralError if the path is invalid or data cannot be queried.\n\t */\n\tpublic static extractAll(path: string, data: unknown): unknown[] {\n\t\tGuards.stringValue(JsonPathHelper.CLASS_NAME, nameof(path), path);\n\n\t\ttry {\n\t\t\tconst results = jsonpath.query(path, data as JSONValue);\n\t\t\treturn results.values();\n\t\t} catch (error) {\n\t\t\tthrow new GeneralError(\n\t\t\t\tJsonPathHelper.CLASS_NAME,\n\t\t\t\t\"extractAllFailed\",\n\t\t\t\t{\n\t\t\t\t\tpath\n\t\t\t\t},\n\t\t\t\tBaseError.fromError(error)\n\t\t\t);\n\t\t}\n\t}\n\n\t/**\n\t * Validate if a JSONPath query is valid.\n\t * @param path The JSONPath query string to validate.\n\t * @returns True if the syntax is valid.\n\t * @throws GeneralError if the path is invalid or data cannot be queried.\n\t */\n\tpublic static validate(path: string): boolean {\n\t\tif (!Is.stringValue(path)) {\n\t\t\treturn false;\n\t\t}\n\n\t\ttry {\n\t\t\t// Query with empty object to test syntax\n\t\t\tjsonpath.query(path, {});\n\t\t\treturn true;\n\t\t} catch (error) {\n\t\t\tthrow new GeneralError(\n\t\t\t\tJsonPathHelper.CLASS_NAME,\n\t\t\t\t\"validateFailed\",\n\t\t\t\t{\n\t\t\t\t\tpath\n\t\t\t\t},\n\t\t\t\tBaseError.fromError(error)\n\t\t\t);\n\t\t}\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"jsonPathHelper.js","sourceRoot":"","sources":["../../src/jsonPathHelper.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,gBAAgB,CAAC;AAErE,OAAO,EAAE,QAAQ,EAAkB,MAAM,SAAS,CAAC;AAInD;;;GAGG;AACH,MAAM,OAAO,cAAc;IAC1B;;OAEG;IACI,MAAM,CAAU,UAAU,oBAAoC;IAErE;;;;;;OAMG;IACI,MAAM,CAAC,KAAK,CAAC,IAAY,EAAE,IAAa;QAC9C,MAAM,CAAC,WAAW,CAAC,cAAc,CAAC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AAC;YACD,OAAO,OAAO,CAAC,SAAS,CAAC,CAAC;QAC3B,CAAC;IACF,CAAC","sourcesContent":["// Copyright 2025 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { BaseError, GeneralError, Guards, Is } from \"@twin.org/core\";\nimport { nameof } from \"@twin.org/nameof\";\nimport { jsonpath, type JSONValue } from \"json-p3\";\nimport type { IJsonPathLocation } from \"./models/IJsonPathLocation.js\";\nimport type { IJsonPathResult } from \"./models/IJsonPathResult.js\";\n\n/**\n * Helper class for JSONPath operations.\n * Provides abstraction over the json-p3 library.\n */\nexport class JsonPathHelper {\n\t/**\n\t * The name of the class name of the JsonPathHelper class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<JsonPathHelper>();\n\n\t/**\n\t * Execute a JSONPath query and return results with values and locations.\n\t * @param path The JSONPath query string (e.g., \"$.store.book[*].title\").\n\t * @param data The data to query.\n\t * @returns Array of results containing values and their locations.\n\t * @throws GeneralError if the path is invalid or data cannot be queried.\n\t */\n\tpublic static query(path: string, data: unknown): IJsonPathResult[] {\n\t\tGuards.stringValue(JsonPathHelper.CLASS_NAME, nameof(path), path);\n\n\t\ttry {\n\t\t\tconst results = jsonpath.query(path, data as JSONValue);\n\t\t\t// Convert JSONPathNodeList to array of IJsonPathResult\n\t\t\tconst resultArray: IJsonPathResult[] = [];\n\t\t\tfor (const node of results) {\n\t\t\t\tresultArray.push({\n\t\t\t\t\tvalue: node.value,\n\t\t\t\t\tlocation: node.location,\n\t\t\t\t\tpath: node.getPath({ form: \"canonical\" })\n\t\t\t\t});\n\t\t\t}\n\t\t\treturn resultArray;\n\t\t} catch (error) {\n\t\t\tthrow new GeneralError(\n\t\t\t\tJsonPathHelper.CLASS_NAME,\n\t\t\t\t\"query\",\n\t\t\t\t{\n\t\t\t\t\tpath\n\t\t\t\t},\n\t\t\t\tBaseError.fromError(error)\n\t\t\t);\n\t\t}\n\t}\n\n\t/**\n\t * Check if a JSONPath exists in the data.\n\t * @param path The JSONPath query string.\n\t * @param data The data to check.\n\t * @returns True if the path exists and returns at least one result.\n\t */\n\tpublic static exists(path: string, data: unknown): boolean {\n\t\tGuards.stringValue(JsonPathHelper.CLASS_NAME, nameof(path), path);\n\n\t\ttry {\n\t\t\tconst results = jsonpath.query(path, data as JSONValue);\n\t\t\treturn !results.empty();\n\t\t} catch {\n\t\t\treturn false;\n\t\t}\n\t}\n\n\t/**\n\t * Extract the first value from a JSONPath query.\n\t * @param path The JSONPath query string.\n\t * @param data The data to query.\n\t * @returns The first matched value, or undefined if no matches.\n\t * @throws GeneralError if the path is invalid or data cannot be queried.\n\t */\n\tpublic static extractSingle(path: string, data: unknown): unknown {\n\t\tGuards.stringValue(JsonPathHelper.CLASS_NAME, nameof(path), path);\n\n\t\ttry {\n\t\t\tconst node = jsonpath.match(path, data as JSONValue);\n\t\t\treturn node?.value;\n\t\t} catch (error) {\n\t\t\tthrow new GeneralError(\n\t\t\t\tJsonPathHelper.CLASS_NAME,\n\t\t\t\t\"extractSingleFailed\",\n\t\t\t\t{\n\t\t\t\t\tpath\n\t\t\t\t},\n\t\t\t\tBaseError.fromError(error)\n\t\t\t);\n\t\t}\n\t}\n\n\t/**\n\t * Extract all values from a JSONPath query.\n\t * @param path The JSONPath query string.\n\t * @param data The data to query.\n\t * @returns Array of all matched values.\n\t * @throws GeneralError if the path is invalid or data cannot be queried.\n\t */\n\tpublic static extractAll(path: string, data: unknown): unknown[] {\n\t\tGuards.stringValue(JsonPathHelper.CLASS_NAME, nameof(path), path);\n\n\t\ttry {\n\t\t\tconst results = jsonpath.query(path, data as JSONValue);\n\t\t\treturn results.values();\n\t\t} catch (error) {\n\t\t\tthrow new GeneralError(\n\t\t\t\tJsonPathHelper.CLASS_NAME,\n\t\t\t\t\"extractAllFailed\",\n\t\t\t\t{\n\t\t\t\t\tpath\n\t\t\t\t},\n\t\t\t\tBaseError.fromError(error)\n\t\t\t);\n\t\t}\n\t}\n\n\t/**\n\t * Validate if a JSONPath query is valid.\n\t * @param path The JSONPath query string to validate.\n\t * @returns True if the syntax is valid.\n\t * @throws GeneralError if the path is invalid or data cannot be queried.\n\t */\n\tpublic static validate(path: string): boolean {\n\t\tif (!Is.stringValue(path)) {\n\t\t\treturn false;\n\t\t}\n\n\t\ttry {\n\t\t\t// Query with empty object to test syntax\n\t\t\tjsonpath.query(path, {});\n\t\t\treturn true;\n\t\t} catch (error) {\n\t\t\tthrow new GeneralError(\n\t\t\t\tJsonPathHelper.CLASS_NAME,\n\t\t\t\t\"validateFailed\",\n\t\t\t\t{\n\t\t\t\t\tpath\n\t\t\t\t},\n\t\t\t\tBaseError.fromError(error)\n\t\t\t);\n\t\t}\n\t}\n\n\t/**\n\t * Set a value on the target object using a JSONPath location.\n\t * @param root The target root object.\n\t * @param location The JSONPath location tokens.\n\t * @param value The value to set.\n\t */\n\tpublic static setAtLocation(root: unknown, location: IJsonPathLocation, value: unknown): void {\n\t\tif (!Is.arrayValue(location) || location.length === 0) {\n\t\t\treturn;\n\t\t}\n\n\t\tlet current: unknown = root;\n\t\tfor (let i = 0; i < location.length - 1; i++) {\n\t\t\tconst token = location[i];\n\t\t\tconst nextToken = location[i + 1];\n\n\t\t\tif (!Is.array(current) && !Is.object(current)) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (Is.number(token)) {\n\t\t\t\tif (!Is.array(current)) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tconst existing = current[token];\n\t\t\t\tif (!Is.array(existing) && !Is.object(existing)) {\n\t\t\t\t\tcurrent[token] = Is.number(nextToken) ? [] : {};\n\t\t\t\t}\n\t\t\t\tcurrent = current[token];\n\t\t\t} else {\n\t\t\t\tif (!Is.object(current)) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tconst existing = current[token];\n\t\t\t\tif (!Is.array(existing) && !Is.object(existing)) {\n\t\t\t\t\tcurrent[token] = Is.number(nextToken) ? [] : {};\n\t\t\t\t}\n\t\t\t\tcurrent = current[token];\n\t\t\t}\n\t\t}\n\n\t\tconst lastToken = location[location.length - 1];\n\t\tif (!Is.array(current) && !Is.object(current)) {\n\t\t\treturn;\n\t\t}\n\t\tif (Is.number(lastToken)) {\n\t\t\tif (!Is.array(current)) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tcurrent[lastToken] = value;\n\t\t} else {\n\t\t\tif (!Is.object(current)) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tcurrent[lastToken] = value;\n\t\t}\n\t}\n\n\t/**\n\t * Delete a value on the target object using a JSONPath location.\n\t * @param root The target root object.\n\t * @param location The JSONPath location tokens.\n\t */\n\tpublic static deleteAtLocation(root: unknown, location: IJsonPathLocation): void {\n\t\tif (!Is.arrayValue(location) || location.length === 0) {\n\t\t\treturn;\n\t\t}\n\n\t\tlet current: unknown = root;\n\t\tfor (let i = 0; i < location.length - 1; i++) {\n\t\t\tconst token = location[i];\n\t\t\tif (!Is.array(current) && !Is.object(current)) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (Is.number(token)) {\n\t\t\t\tif (!Is.array(current)) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tcurrent = current[token];\n\t\t\t} else {\n\t\t\t\tif (!Is.object(current)) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tcurrent = current[token];\n\t\t\t}\n\t\t}\n\n\t\tif (!Is.array(current) && !Is.object(current)) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst lastToken = location[location.length - 1];\n\t\tif (Is.number(lastToken)) {\n\t\t\tif (!Is.array(current)) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tcurrent[lastToken] = undefined;\n\t\t} else {\n\t\t\tif (!Is.object(current)) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tdelete current[lastToken];\n\t\t}\n\t}\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IJsonPathLocation.js","sourceRoot":"","sources":["../../../src/models/IJsonPathLocation.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC","sourcesContent":["// Copyright 2025 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\n\n/**\n * Result paths from a JSONPath query operation.\n */\nexport type IJsonPathLocation = (string | number)[];\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"IJsonPathResult.js","sourceRoot":"","sources":["../../../src/models/IJsonPathResult.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"IJsonPathResult.js","sourceRoot":"","sources":["../../../src/models/IJsonPathResult.ts"],"names":[],"mappings":"","sourcesContent":["// Copyright 2025 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport type { IJsonPathLocation } from \"./IJsonPathLocation.js\";\n\n/**\n * Result from a JSONPath query operation.\n */\nexport interface IJsonPathResult {\n\t/**\n\t * The value at the matched path.\n\t */\n\tvalue: unknown;\n\n\t/**\n\t * The location path to the value.\n\t */\n\tlocation: IJsonPathLocation;\n\n\t/**\n\t * The location path as a string for debugging.\n\t */\n\tpath: string;\n}\n"]}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { IJsonPathLocation } from "./models/IJsonPathLocation.js";
|
|
1
2
|
import type { IJsonPathResult } from "./models/IJsonPathResult.js";
|
|
2
3
|
/**
|
|
3
4
|
* Helper class for JSONPath operations.
|
|
@@ -46,4 +47,17 @@ export declare class JsonPathHelper {
|
|
|
46
47
|
* @throws GeneralError if the path is invalid or data cannot be queried.
|
|
47
48
|
*/
|
|
48
49
|
static validate(path: string): boolean;
|
|
50
|
+
/**
|
|
51
|
+
* Set a value on the target object using a JSONPath location.
|
|
52
|
+
* @param root The target root object.
|
|
53
|
+
* @param location The JSONPath location tokens.
|
|
54
|
+
* @param value The value to set.
|
|
55
|
+
*/
|
|
56
|
+
static setAtLocation(root: unknown, location: IJsonPathLocation, value: unknown): void;
|
|
57
|
+
/**
|
|
58
|
+
* Delete a value on the target object using a JSONPath location.
|
|
59
|
+
* @param root The target root object.
|
|
60
|
+
* @param location The JSONPath location tokens.
|
|
61
|
+
*/
|
|
62
|
+
static deleteAtLocation(root: unknown, location: IJsonPathLocation): void;
|
|
49
63
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { IJsonPathLocation } from "./IJsonPathLocation.js";
|
|
1
2
|
/**
|
|
2
3
|
* Result from a JSONPath query operation.
|
|
3
4
|
*/
|
|
@@ -9,7 +10,7 @@ export interface IJsonPathResult {
|
|
|
9
10
|
/**
|
|
10
11
|
* The location path to the value.
|
|
11
12
|
*/
|
|
12
|
-
location:
|
|
13
|
+
location: IJsonPathLocation;
|
|
13
14
|
/**
|
|
14
15
|
* The location path as a string for debugging.
|
|
15
16
|
*/
|
package/docs/changelog.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# @twin.org/data-json-path - Changelog
|
|
2
2
|
|
|
3
|
+
## [0.0.3-next.9](https://github.com/twinfoundation/data/compare/data-json-path-v0.0.3-next.8...data-json-path-v0.0.3-next.9) (2026-02-23)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Miscellaneous Chores
|
|
7
|
+
|
|
8
|
+
* **data-json-path:** Synchronize repo versions
|
|
9
|
+
|
|
10
|
+
## [0.0.3-next.8](https://github.com/twinfoundation/data/compare/data-json-path-v0.0.3-next.7...data-json-path-v0.0.3-next.8) (2026-02-02)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
### Features
|
|
14
|
+
|
|
15
|
+
* add json path set and delete methods ([#39](https://github.com/twinfoundation/data/issues/39)) ([04b73f3](https://github.com/twinfoundation/data/commit/04b73f3d44525f7d3a5d01b56530ff8d9c7bd938))
|
|
16
|
+
|
|
3
17
|
## [0.0.3-next.7](https://github.com/twinfoundation/data/compare/data-json-path-v0.0.3-next.6...data-json-path-v0.0.3-next.7) (2026-01-21)
|
|
4
18
|
|
|
5
19
|
|
|
@@ -170,3 +170,61 @@ True if the syntax is valid.
|
|
|
170
170
|
#### Throws
|
|
171
171
|
|
|
172
172
|
GeneralError if the path is invalid or data cannot be queried.
|
|
173
|
+
|
|
174
|
+
***
|
|
175
|
+
|
|
176
|
+
### setAtLocation()
|
|
177
|
+
|
|
178
|
+
> `static` **setAtLocation**(`root`, `location`, `value`): `void`
|
|
179
|
+
|
|
180
|
+
Set a value on the target object using a JSONPath location.
|
|
181
|
+
|
|
182
|
+
#### Parameters
|
|
183
|
+
|
|
184
|
+
##### root
|
|
185
|
+
|
|
186
|
+
`unknown`
|
|
187
|
+
|
|
188
|
+
The target root object.
|
|
189
|
+
|
|
190
|
+
##### location
|
|
191
|
+
|
|
192
|
+
[`IJsonPathLocation`](../type-aliases/IJsonPathLocation.md)
|
|
193
|
+
|
|
194
|
+
The JSONPath location tokens.
|
|
195
|
+
|
|
196
|
+
##### value
|
|
197
|
+
|
|
198
|
+
`unknown`
|
|
199
|
+
|
|
200
|
+
The value to set.
|
|
201
|
+
|
|
202
|
+
#### Returns
|
|
203
|
+
|
|
204
|
+
`void`
|
|
205
|
+
|
|
206
|
+
***
|
|
207
|
+
|
|
208
|
+
### deleteAtLocation()
|
|
209
|
+
|
|
210
|
+
> `static` **deleteAtLocation**(`root`, `location`): `void`
|
|
211
|
+
|
|
212
|
+
Delete a value on the target object using a JSONPath location.
|
|
213
|
+
|
|
214
|
+
#### Parameters
|
|
215
|
+
|
|
216
|
+
##### root
|
|
217
|
+
|
|
218
|
+
`unknown`
|
|
219
|
+
|
|
220
|
+
The target root object.
|
|
221
|
+
|
|
222
|
+
##### location
|
|
223
|
+
|
|
224
|
+
[`IJsonPathLocation`](../type-aliases/IJsonPathLocation.md)
|
|
225
|
+
|
|
226
|
+
The JSONPath location tokens.
|
|
227
|
+
|
|
228
|
+
#### Returns
|
|
229
|
+
|
|
230
|
+
`void`
|
package/docs/reference/index.md
CHANGED