1
0
mirror of https://github.com/fluencelabs/jsonpath synced 2025-04-05 08:21:04 +00:00

64 lines
1.3 KiB
JavaScript
Raw Normal View History

2019-04-09 16:13:09 +09:00
const { CompileFn, SelectorFn, selectStr, Selector: _Selector } = require('../native');
2019-03-14 22:30:42 +09:00
function compile(path) {
2019-04-09 16:13:09 +09:00
let compile = new CompileFn(path);
2019-03-14 22:30:42 +09:00
return (json) => {
if(typeof json != 'string') {
json = JSON.stringify(json)
}
return JSON.parse(compile.template(json));
2019-03-14 22:30:42 +09:00
};
}
function selector(json) {
if(typeof json != 'string') {
json = JSON.stringify(json)
}
2019-04-09 16:13:09 +09:00
let selector = new SelectorFn(json);
2019-03-14 22:30:42 +09:00
return (path) => {
2019-04-09 16:13:09 +09:00
return JSON.parse(selector.select(path));
2019-03-14 22:30:42 +09:00
}
}
function select(json, path) {
if(typeof json != 'string') {
json = JSON.stringify(json)
}
return JSON.parse(selectStr(json, path));
2019-03-14 22:30:42 +09:00
}
2019-04-09 16:13:09 +09:00
class Selector {
constructor() {
this._selector = new _Selector();
return this;
}
path(path) {
this._selector.path(path);
return this;
}
value(json) {
if(typeof json != 'string') {
json = JSON.stringify(json)
}
this._selector.value_from_str(json);
return this;
}
selectToStr() {
return this._selector.select_to_str();
}
selectTo() {
return JSON.parse(this.selectToStr());
}
}
2019-03-14 22:30:42 +09:00
module.exports = {
compile,
selector,
2019-04-09 16:13:09 +09:00
select,
Selector
2019-03-14 22:30:42 +09:00
};