mirror of
https://github.com/fluencelabs/jsonpath
synced 2025-06-13 16:11:26 +00:00
SelectorMut NodeJs 적용
This commit is contained in:
@ -1,4 +1,12 @@
|
||||
const { CompileFn, SelectorFn, selectStr, Selector: _Selector } = require('../native');
|
||||
const {
|
||||
CompileFn,
|
||||
SelectorFn,
|
||||
selectStr,
|
||||
deleteValue: _deleteValue,
|
||||
replaceWith: _replaceWith,
|
||||
Selector: _Selector,
|
||||
SelectorMut: _SelectorMut
|
||||
} = require('../native');
|
||||
|
||||
function compile(path) {
|
||||
let compile = new CompileFn(path);
|
||||
@ -27,6 +35,30 @@ function select(json, path) {
|
||||
return JSON.parse(selectStr(json, path));
|
||||
}
|
||||
|
||||
function deleteValue(json, path) {
|
||||
if(typeof json != 'string') {
|
||||
json = JSON.stringify(json)
|
||||
}
|
||||
return JSON.parse(_deleteValue(json, path));
|
||||
}
|
||||
|
||||
function replaceWith(json, path, fun) {
|
||||
if(typeof json != 'string') {
|
||||
json = JSON.stringify(json)
|
||||
}
|
||||
let result = _replaceWith(json, path, (v) => {
|
||||
let result = fun(JSON.parse(v));
|
||||
if(typeof result != 'string') {
|
||||
result = JSON.stringify(result)
|
||||
}
|
||||
return result;
|
||||
});
|
||||
if(typeof result == 'string') {
|
||||
result = JSON.parse(result);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
class Selector {
|
||||
constructor() {
|
||||
this._selector = new _Selector();
|
||||
@ -51,9 +83,67 @@ class Selector {
|
||||
}
|
||||
}
|
||||
|
||||
class SelectorMut {
|
||||
constructor() {
|
||||
return this;
|
||||
}
|
||||
|
||||
path(path) {
|
||||
this.path = path;
|
||||
return this;
|
||||
}
|
||||
|
||||
value(json) {
|
||||
if(typeof json != 'string') {
|
||||
json = JSON.stringify(json)
|
||||
}
|
||||
this.json = json;
|
||||
return this;
|
||||
}
|
||||
|
||||
deleteValue() {
|
||||
let selector = new _SelectorMut();
|
||||
if(!this.path) {
|
||||
selector.emptyPathError();
|
||||
return;
|
||||
}
|
||||
|
||||
if(!this.json) {
|
||||
selector.emptyValueError();
|
||||
return;
|
||||
}
|
||||
|
||||
this.json = deleteValue(this.json, this.path);
|
||||
return this;
|
||||
}
|
||||
|
||||
replaceWith(fun) {
|
||||
let selector = new _SelectorMut();
|
||||
if(!this.path) {
|
||||
selector.emptyPathError();
|
||||
return;
|
||||
}
|
||||
if(!this.json) {
|
||||
selector.emptyValueError();
|
||||
return;
|
||||
}
|
||||
this.json = replaceWith(this.json, this.path, fun);
|
||||
return this;
|
||||
}
|
||||
|
||||
take() {
|
||||
let json = this.json;
|
||||
delete this.json;
|
||||
return json;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
compile,
|
||||
selector,
|
||||
select,
|
||||
Selector
|
||||
deleteValue,
|
||||
replaceWith,
|
||||
Selector,
|
||||
SelectorMut
|
||||
};
|
Reference in New Issue
Block a user