mirror of
https://github.com/fluencelabs/jsonpath
synced 2025-06-13 16:11:26 +00:00
update READEME.md
This commit is contained in:
@ -89,7 +89,7 @@ class SelectorMut {
|
||||
}
|
||||
|
||||
path(path) {
|
||||
this.path = path;
|
||||
this._path = path;
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -97,43 +97,43 @@ class SelectorMut {
|
||||
if(typeof json != 'string') {
|
||||
json = JSON.stringify(json)
|
||||
}
|
||||
this.json = json;
|
||||
this._json = json;
|
||||
return this;
|
||||
}
|
||||
|
||||
deleteValue() {
|
||||
let selector = new _SelectorMut();
|
||||
if(!this.path) {
|
||||
if(!this._path) {
|
||||
selector.emptyPathError();
|
||||
return;
|
||||
}
|
||||
|
||||
if(!this.json) {
|
||||
if(!this._json) {
|
||||
selector.emptyValueError();
|
||||
return;
|
||||
}
|
||||
|
||||
this.json = deleteValue(this.json, this.path);
|
||||
this._json = deleteValue(this._json, this._path);
|
||||
return this;
|
||||
}
|
||||
|
||||
replaceWith(fun) {
|
||||
let selector = new _SelectorMut();
|
||||
if(!this.path) {
|
||||
if(!this._path) {
|
||||
selector.emptyPathError();
|
||||
return;
|
||||
}
|
||||
if(!this.json) {
|
||||
if(!this._json) {
|
||||
selector.emptyValueError();
|
||||
return;
|
||||
}
|
||||
this.json = replaceWith(this.json, this.path, fun);
|
||||
this._json = replaceWith(this._json, this._path, fun);
|
||||
return this;
|
||||
}
|
||||
|
||||
take() {
|
||||
let json = this.json;
|
||||
delete this.json;
|
||||
let json = this._json;
|
||||
delete this._json;
|
||||
return json;
|
||||
}
|
||||
}
|
||||
|
@ -14,7 +14,8 @@ exclude = ["artifacts.json", "index.node"]
|
||||
neon-build = "0.2.0"
|
||||
|
||||
[dependencies]
|
||||
jsonpath_lib = "0.2.0"
|
||||
#jsonpath_lib = "0.2.0"
|
||||
jsonpath_lib = {path="../../"}
|
||||
neon = "0.2.0"
|
||||
neon-serde = "0.1.1"
|
||||
serde_json = { version = "1.0", features = ["preserve_order"] }
|
||||
|
@ -550,6 +550,64 @@ describe('README test', () => {
|
||||
done();
|
||||
});
|
||||
|
||||
it('jsonpath.SelectorMut', (done) => {
|
||||
let jsonObj = {
|
||||
'school': {
|
||||
'friends': [
|
||||
{'name': '친구1', 'age': 20},
|
||||
{'name': '친구2', 'age': 20},
|
||||
],
|
||||
},
|
||||
'friends': [
|
||||
{'name': '친구3', 'age': 30},
|
||||
{'name': '친구4'},
|
||||
],
|
||||
};
|
||||
|
||||
let selector = new jsonpath.SelectorMut();
|
||||
selector.path('$..[?(@.age == 20)]');
|
||||
|
||||
{
|
||||
selector.value(jsonObj).deleteValue();
|
||||
|
||||
let resultObj = {
|
||||
'school': {'friends': [null, null]},
|
||||
'friends': [
|
||||
{'name': '친구3', 'age': 30},
|
||||
{'name': '친구4'},
|
||||
],
|
||||
};
|
||||
if (JSON.stringify(selector.take()) !== JSON.stringify(resultObj)) {
|
||||
throw 'jsonpath.SelectorMut.deleteValue';
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
selector.value(jsonObj).replaceWith((v) => {
|
||||
v.age = v.age * 2;
|
||||
return v;
|
||||
});
|
||||
|
||||
let resultObj = {
|
||||
'school': {
|
||||
'friends': [
|
||||
{'name': '친구1', 'age': 40},
|
||||
{'name': '친구2', 'age': 40},
|
||||
],
|
||||
},
|
||||
'friends': [
|
||||
{'name': '친구3', 'age': 30},
|
||||
{'name': '친구4'},
|
||||
],
|
||||
};
|
||||
if (JSON.stringify(selector.take()) !== JSON.stringify(resultObj)) {
|
||||
throw 'jsonpath.SelectorMut.replaceWith';
|
||||
}
|
||||
}
|
||||
|
||||
done();
|
||||
});
|
||||
|
||||
it('jsonpath.select(json: string|object, jsonpath: string)', (done) => {
|
||||
let jsonObj = {
|
||||
"school": {
|
||||
@ -679,4 +737,64 @@ describe('README test', () => {
|
||||
|
||||
done();
|
||||
});
|
||||
|
||||
it('jsonpath.deleteValue(json: string|object, path: string)', (done) => {
|
||||
let jsonObj = {
|
||||
"school": {
|
||||
"friends": [
|
||||
{"name": "친구1", "age": 20},
|
||||
{"name": "친구2", "age": 20}
|
||||
]
|
||||
},
|
||||
"friends": [
|
||||
{"name": "친구3", "age": 30},
|
||||
{"name": "친구4"}
|
||||
]
|
||||
};
|
||||
|
||||
let _1 = jsonpath.deleteValue(jsonObj, '$..friends[0]');
|
||||
let result = jsonpath.deleteValue(_1, '$..friends[1]');
|
||||
|
||||
if(JSON.stringify(result) === JSON.stringify({
|
||||
"school": { "friends": [null, null]},
|
||||
"friends": [null, null]
|
||||
})) {
|
||||
done();
|
||||
}
|
||||
});
|
||||
|
||||
it('jsonpath.replaceWith(json: string|object, path: string, fun: function(json: object) => json: object', (done) => {
|
||||
let jsonObj = {
|
||||
"school": {
|
||||
"friends": [
|
||||
{"name": "친구1", "age": 20},
|
||||
{"name": "친구2", "age": 20}
|
||||
]
|
||||
},
|
||||
"friends": [
|
||||
{"name": "친구3", "age": 30},
|
||||
{"name": "친구4"}
|
||||
]
|
||||
};
|
||||
|
||||
let result = jsonpath.replaceWith(jsonObj, '$..friends[0]', (v) => {
|
||||
v.age = v.age * 2;
|
||||
return v;
|
||||
});
|
||||
|
||||
if(JSON.stringify(result) === JSON.stringify({
|
||||
"school": {
|
||||
"friends": [
|
||||
{"name": "친구1", "age": 40},
|
||||
{"name": "친구2", "age": 20}
|
||||
]
|
||||
},
|
||||
"friends": [
|
||||
{"name": "친구3", "age": 60},
|
||||
{"name": "친구4"}
|
||||
]
|
||||
})) {
|
||||
done();
|
||||
}
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user