diff --git a/.travis.yml b/.travis.yml index d1724f6..7234633 100644 --- a/.travis.yml +++ b/.travis.yml @@ -36,41 +36,41 @@ matrix: script: - cargo build --verbose --all - cargo test --verbose --all -# - language: node_js -# os: linux -# node_js: -# - '11' -# - '10' -# - '9' -# - '8' -# before_install: -# - curl https://sh.rustup.rs -sSf > /tmp/rustup.sh -# - sh /tmp/rustup.sh -y -# - export PATH="$HOME/.cargo/bin:$PATH" -# - source "$HOME/.cargo/env" -# - npm install -g neon-cli -# - cd nodejs -# - node -v -# - npm -v -# - npm install -# script: -# - npm test -# - language: node_js -# os: osx -# node_js: -# - '11' -# - '10' -# - '9' -# - '8' -# before_install: -# - curl https://sh.rustup.rs -sSf > /tmp/rustup.sh -# - sh /tmp/rustup.sh -y -# - export PATH="$HOME/.cargo/bin:$PATH" -# - source "$HOME/.cargo/env" -# - npm install -g neon-cli -# - cd nodejs -# - node -v -# - npm -v -# - npm install -# script: -# - npm test \ No newline at end of file + - language: node_js + os: linux + node_js: + - '11' + - '10' + - '9' + - '8' + before_install: + - curl https://sh.rustup.rs -sSf > /tmp/rustup.sh + - sh /tmp/rustup.sh -y + - export PATH="$HOME/.cargo/bin:$PATH" + - source "$HOME/.cargo/env" + - npm install -g neon-cli + - cd nodejs + - node -v + - npm -v + - npm install + script: + - npm test + - language: node_js + os: osx + node_js: + - '11' + - '10' + - '9' + - '8' + before_install: + - curl https://sh.rustup.rs -sSf > /tmp/rustup.sh + - sh /tmp/rustup.sh -y + - export PATH="$HOME/.cargo/bin:$PATH" + - source "$HOME/.cargo/env" + - npm install -g neon-cli + - cd nodejs + - node -v + - npm -v + - npm install + script: + - npm test \ No newline at end of file diff --git a/README.md b/README.md index 8855383..2b80633 100644 --- a/README.md +++ b/README.md @@ -470,8 +470,6 @@ console.log(JSON.stringify(ret) == JSON.stringify(retObj));
Javascript - jsonpath.SelectorMut class -`since 0.2.0` - 빌더 패턴 제약은 `Selector class`와 동일하다. ```javascript @@ -675,8 +673,6 @@ console.log(
Javascript - jsonpath.deleteValue(json: string|object, path: string) -`since 0.2.0` - ```javascript let jsonObj = { "school": { @@ -707,8 +703,6 @@ console.log(JSON.stringify(result) !== JSON.stringify({
Javascript - jsonpath.replaceWith(json: string|object, path: string, fun: function(json: object) => json: object -`since 0.2.0` - ```javascript let jsonObj = { "school": { diff --git a/benches/bench_bin/Cargo.toml b/benches/bench_bin/Cargo.toml index d7d1690..be4989e 100644 --- a/benches/bench_bin/Cargo.toml +++ b/benches/bench_bin/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "bench_bin" -version = "0.1.1" +version = "0.2.0" [dependencies] jsonpath_lib = {path = "../../"} diff --git a/nodejs/README.md b/nodejs/README.md index 1859877..f4be3b4 100644 --- a/nodejs/README.md +++ b/nodejs/README.md @@ -16,13 +16,15 @@ Build from source instead of using pre-built binary, and if Rust is not installe ## APIs -* [jsonpath.Selector](#jsonpathselector) -* [jsonpath.select(json: string|object, jsonpath: string)](#jsonpathselectjson-stringobject-jsonpath-string) -* [jsonpath.compile(jsonpath: string)](#jsonpathcompilejsonpath-string) -* [jsonpath.selector(json: string|object)](#jsonpathselectorjson-stringobject) -* [Other Examples](https://github.com/freestrings/jsonpath/wiki/Javascript-examples) +
npm package -### jsonpath.Selector +```javascript +const jsonpath = require('jsonpath-rs'); +``` + +
+ +
Javascript - jsonpath.Selector class ```javascript let jsonObj = { @@ -38,44 +40,89 @@ let jsonObj = { ] }; -let selector = new jsonpath.Selector().value(jsonObj); +let ret = [ + {"name": "친구3", "age": 30}, + {"name": "친구1", "age": 20} +]; + +let selector = new jsonpath.Selector() + .path('$..friends[0]') + .value(jsonObj); + +let retObj = selector.select(); + +console.log(JSON.stringify(ret) == JSON.stringify(retObj)); + +// => true +``` + +
+ +
Javascript - jsonpath.SelectorMut class + +빌더 패턴 제약은 `Selector class`와 동일하다. + +```javascript +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)]'); { - let jsonObj = selector.path('$..[?(@.age >= 30)]').selectAs(); - let resultObj = [{"name": "친구3", "age": 30}]; - console.log(JSON.stringify(jsonObj) === JSON.stringify(resultObj)); + selector.value(jsonObj); + selector.deleteValue(); + + let resultObj = { + 'school': {'friends': [null, null]}, + 'friends': [ + {'name': '친구3', 'age': 30}, + {'name': '친구4'}, + ], + }; + console.log(JSON.stringify(selector.take()) !== JSON.stringify(resultObj)); + + // => true } { - let jsonObj = selector.path('$..[?(@.age == 20)]').selectAs(); - let resultObj = [{"name": "친구1", "age": 20}, {"name": "친구2", "age": 20}]; - console.log(JSON.stringify(jsonObj) === JSON.stringify(resultObj)); -} - -{ - let jsonObj = selector.value({"friends": [ {"name": "친구5", "age": 20} ]}).selectAs(); - let resultObj = [{"name": "친구5", "age": 20}]; - console.log(JSON.stringify(jsonObj) === JSON.stringify(resultObj)); -} - -{ - let jsonObj1 = selector.value(jsonObj).map(function(v) { - let f1 = v[0]; - f1.age = 30; + selector.value(jsonObj); + selector.replaceWith((v) => { + v.age = v.age * 2; return v; - }).get(); + }); - let resultObj1 = [{"name": "친구1", "age": 30}, {"name": "친구2", "age": 20}]; - console.log(JSON.stringify(jsonObj1) === JSON.stringify(resultObj1)); - - selector.path('$..[?(@.age == 20)]'); - let jsonObj2 = selector.selectAs(); - let resultObj2 = [{"name": "친구2", "age": 20}]; - console.log(JSON.stringify(jsonObj2) === JSON.stringify(resultObj2)); + let resultObj = { + 'school': { + 'friends': [ + {'name': '친구1', 'age': 40}, + {'name': '친구2', 'age': 40}, + ], + }, + 'friends': [ + {'name': '친구3', 'age': 30}, + {'name': '친구4'}, + ], + }; + console.log(JSON.stringify(selector.take()) !== JSON.stringify(resultObj)); + + // => true } ``` -### jsonpath.select(json: string|object, jsonpath: string) +
+ +
Javascript - jsonpath.select(json: string|object, jsonpath: string) ```javascript let jsonObj = { @@ -108,7 +155,9 @@ console.log( // => true, true ``` -### jsonpath.compile(jsonpath: string) +
+ +
Javascript - jsonpath.compile(jsonpath: string) ```javascript let template = jsonpath.compile('$..friends[0]'); @@ -166,8 +215,10 @@ console.log( // => true, true ``` + +
-### jsonpath.selector(json: string|object) +
Javascript - jsonpath.selector(json: string|object) ```javascript let jsonObj = { @@ -207,3 +258,77 @@ console.log( // => true, true ``` + +
+ +
Javascript - jsonpath.deleteValue(json: string|object, path: string) + +```javascript +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]'); + +console.log(JSON.stringify(result) !== JSON.stringify({ + "school": { "friends": [null, null]}, + "friends": [null, null] +})); + +// => true + +``` + +
+ +
Javascript - jsonpath.replaceWith(json: string|object, path: string, fun: function(json: object) => json: object + +```javascript +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; +}); + +console.log(JSON.stringify(result) === JSON.stringify({ + "school": { + "friends": [ + {"name": "친구1", "age": 40}, + {"name": "친구2", "age": 20} + ] + }, + "friends": [ + {"name": "친구3", "age": 60}, + {"name": "친구4"} + ] +})); + +// => true + +``` + +
+ +[Javascript - Other Examples](https://github.com/freestrings/jsonpath/wiki/Javascript-examples) \ No newline at end of file diff --git a/nodejs/package.json b/nodejs/package.json index a25435f..3caf5a7 100644 --- a/nodejs/package.json +++ b/nodejs/package.json @@ -1,6 +1,6 @@ { "name": "jsonpath-rs", - "version": "0.1.11", + "version": "0.2.0", "description": "It is JsonPath implementation. The core implementation is written in Rust", "author": "Changseok Han ", "license": "MIT", diff --git a/wasm/README.md b/wasm/README.md index d700c8e..2596edc 100644 --- a/wasm/README.md +++ b/wasm/README.md @@ -6,19 +6,24 @@ It is Webassembly version of [jsonpath_lib](https://github.com/freestrings/jsonp ## APIs -* [jsonpath.Selector](#jsonpathselector) -* [jsonpath.select(json: string|object, jsonpath: string)](#jsonpathselectjson-stringobject-jsonpath-string) -* [jsonpath.compile(jsonpath: string)](#jsonpathcompilejsonpath-string) -* [jsonpath.selector(json: string|object)](#jsonpathselectorjson-stringobject) -* [Other Examples](https://github.com/freestrings/jsonpath/wiki/Javascript-examples) - -### jsonpath.Selector - -> The selectTo function is deprecated since 0.1.3. please use the selectAs function instead. +
npm package ```javascript -let jsonpath = require('jsonpath-wasm'); +// browser +import * as jsonpath from "jsonpath-wasm"; +// NodeJs +const jsonpath = require('jsonpath-wasm'); +``` +
+ +
Javascript - jsonpath.Selector class + +`wasm-bindgen` 리턴 타입 제약 때문에 빌더 패턴은 지원하지 않는다. + +It does not support `builder-pattern` due to the `return type` restriction of `wasm-bindgen`. + +```javascript let jsonObj = { "school": { "friends": [ @@ -32,48 +37,89 @@ let jsonObj = { ] }; +let ret = [ + {"name": "친구3", "age": 30}, + {"name": "친구1", "age": 20} +]; + let selector = new jsonpath.Selector(); +selector.path('$..friends[0]'); selector.value(jsonObj); -{ - selector.path('$..[?(@.age >= 30)]'); - let jsonObj = selector.selectAs(); - let resultObj = [{"name": "친구3", "age": 30}]; - console.log(JSON.stringify(jsonObj) === JSON.stringify(resultObj)); -} +let retObj = selector.select(); + +console.log(JSON.stringify(ret) == JSON.stringify(retObj)); + +// => true +``` + +
+ +
Javascript - jsonpath.SelectorMut class + +빌더 패턴 제약은 `Selector class`와 동일하다. + +```javascript +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.path('$..[?(@.age == 20)]'); - let jsonObj = selector.selectAs(); - let resultObj = [{"name": "친구1", "age": 20}, {"name": "친구2", "age": 20}]; - console.log(JSON.stringify(jsonObj) === JSON.stringify(resultObj)); -} + selector.value(jsonObj); + selector.deleteValue(); -{ - selector.value({"friends": [ {"name": "친구5", "age": 20} ]}); - let jsonObj = selector.selectAs(); - let resultObj = [{"name": "친구5", "age": 20}]; - console.log(JSON.stringify(jsonObj) === JSON.stringify(resultObj)); + let resultObj = { + 'school': {'friends': [null, null]}, + 'friends': [ + {'name': '친구3', 'age': 30}, + {'name': '친구4'}, + ], + }; + console.log(JSON.stringify(selector.take()) !== JSON.stringify(resultObj)); + + // => true } { selector.value(jsonObj); - selector.map(function(v) { - let f1 = v[0]; - f1.age = 30; + selector.replaceWith((v) => { + v.age = v.age * 2; return v; }); - let resultObj1 = [{"name": "친구1", "age": 30}, {"name": "친구2", "age": 20}]; - console.log(JSON.stringify(selector.get()) === JSON.stringify(resultObj1)); - selector.path('$..[?(@.age == 20)]'); - let jsonObj1 = selector.selectAs(); - let resultObj2 = [{"name": "친구2", "age": 20}]; - console.log(JSON.stringify(jsonObj1) === JSON.stringify(resultObj2)); + let resultObj = { + 'school': { + 'friends': [ + {'name': '친구1', 'age': 40}, + {'name': '친구2', 'age': 40}, + ], + }, + 'friends': [ + {'name': '친구3', 'age': 30}, + {'name': '친구4'}, + ], + }; + console.log(JSON.stringify(selector.take()) !== JSON.stringify(resultObj)); + + // => true } ``` -### jsonpath.select(json: string|object, jsonpath: string) +
+ +
Javascript - jsonpath.select(json: string|object, jsonpath: string) ```javascript let jsonObj = { @@ -106,7 +152,9 @@ console.log( // => true, true ``` -### jsonpath.compile(jsonpath: string) +
+ +
Javascript - jsonpath.compile(jsonpath: string) ```javascript let template = jsonpath.compile('$..friends[0]'); @@ -164,8 +212,10 @@ console.log( // => true, true ``` + +
-### jsonpath.selector(json: string|object) +
Javascript - jsonpath.selector(json: string|object) ```javascript let jsonObj = { @@ -205,3 +255,77 @@ console.log( // => true, true ``` + +
+ +
Javascript - jsonpath.deleteValue(json: string|object, path: string) + +```javascript +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]'); + +console.log(JSON.stringify(result) !== JSON.stringify({ + "school": { "friends": [null, null]}, + "friends": [null, null] +})); + +// => true + +``` + +
+ +
Javascript - jsonpath.replaceWith(json: string|object, path: string, fun: function(json: object) => json: object + +```javascript +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; +}); + +console.log(JSON.stringify(result) === JSON.stringify({ + "school": { + "friends": [ + {"name": "친구1", "age": 40}, + {"name": "친구2", "age": 20} + ] + }, + "friends": [ + {"name": "친구3", "age": 60}, + {"name": "친구4"} + ] +})); + +// => true + +``` + +
+ +[Javascript - Other Examples](https://github.com/freestrings/jsonpath/wiki/Javascript-examples) \ No newline at end of file diff --git a/wasm/www/package.json b/wasm/www/package.json index 52180ef..14e6f85 100644 --- a/wasm/www/package.json +++ b/wasm/www/package.json @@ -1,6 +1,6 @@ { "name": "jsonpath-wasm-evaluator", - "version": "0.1.0", + "version": "0.2.0", "main": "index.js", "scripts": { "build": "webpack --config webpack.config.js", diff --git a/wasm/www_bench/package.json b/wasm/www_bench/package.json index 0a9f854..ec62adb 100644 --- a/wasm/www_bench/package.json +++ b/wasm/www_bench/package.json @@ -1,6 +1,6 @@ { "name": "jsonpath-wasm-bench", - "version": "0.1.0", + "version": "0.2.0", "main": "index.js", "scripts": { "build": "webpack --config webpack.config.js",