Skip to content

Commit 87e0434

Browse files
committed
Add json-edit-related scriptlets
edit-outbound-object-.js @description Prune properties from an object returned by a specific method. Properties can only be removed. @param jsonq A uBO-flavored JSONPath query. trusted-edit-outbound-object.js @description Edit properties from an object returned by a specific method. Properties can be assigned new values. @param jsonq A uBO-flavored JSONPath query. json-edit-xhr-request.js @description Edit the object sent as the body in a XHR instance. Properties can only be removed. @param jsonq A uBO-flavored JSONPath query. @param [propsToMatch, value] An optional vararg detailing the arguments to match when xhr.open() is called. trusted-json-edit-xhr-request.js @description Edit the object sent as the body in a XHR instance. Properties can be assigned new values. @param jsonq A uBO-flavored JSONPath query. @param [propsToMatch, value] An optional vararg detailing the arguments to match when xhr.open() is called. json-edit-fetch-request.js @description Edit the request body sent through the fetch API. Properties can only be removed. @param jsonq A uBO-flavored JSONPath query. @param [propsToMatch, value] An optional vararg detailing the arguments to match when fetch() is called. trusted-json-edit-fetch-request.js @description Edit the request body sent through the fetch API. Properties can be assigned new values. @param jsonq A uBO-flavored JSONPath query. @param [propsToMatch, value] An optional vararg detailing the arguments to match when fetch() is called.
1 parent 3a2bb62 commit 87e0434

File tree

2 files changed

+327
-12
lines changed

2 files changed

+327
-12
lines changed

src/js/jsonpath.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ export class JSONPath {
9999
const r = this.#compile(query, 0);
100100
if ( r === undefined ) { return; }
101101
if ( r.i !== query.length ) {
102+
if ( query.startsWith('+=', r.i) ) {
103+
r.modify = '+';
104+
r.i += 1;
105+
}
102106
if ( query.startsWith('=', r.i) === false ) { return; }
103107
try { r.rval = JSON.parse(query.slice(r.i+1)); }
104108
catch { return; }
@@ -114,15 +118,19 @@ export class JSONPath {
114118
}
115119
apply(root) {
116120
if ( this.valid === false ) { return 0; }
117-
const { rval } = this.#compiled;
121+
const { modify, rval } = this.#compiled;
118122
this.#root = root;
119123
const paths = this.#evaluate(this.#compiled.steps, []);
120124
const n = paths.length;
121125
let i = n;
122126
while ( i-- ) {
123127
const { obj, key } = this.#resolvePath(paths[i]);
124128
if ( rval !== undefined ) {
125-
obj[key] = rval;
129+
if ( modify === '+' ) {
130+
this.#modifyVal(obj, key, rval);
131+
} else {
132+
obj[key] = rval;
133+
}
126134
} else if ( Array.isArray(obj) && typeof key === 'number' ) {
127135
obj.splice(key, 1);
128136
} else {
@@ -450,4 +458,13 @@ export class JSONPath {
450458
}
451459
if ( outcome ) { return k; }
452460
}
461+
#modifyVal(obj, key, rval) {
462+
const lval = obj[key];
463+
if ( rval instanceof Object === false ) { return; }
464+
if ( lval instanceof Object === false ) { return; }
465+
if ( Array.isArray(lval) ) { return; }
466+
for ( const [ k, v ] of Object.entries(rval) ) {
467+
lval[k] = v;
468+
}
469+
}
453470
}

0 commit comments

Comments
 (0)