From 635b5b8d43980b358ed37c81704786490dfd0159 Mon Sep 17 00:00:00 2001 From: freestrings Date: Tue, 11 Jun 2019 22:16:37 +0900 Subject: [PATCH] update wasm README.md, nodejs README.md. restore nodejs build --- .travis.yml | 76 +++++++------- README.md | 6 -- benches/bench_bin/Cargo.toml | 2 +- nodejs/README.md | 195 +++++++++++++++++++++++++++------- nodejs/package.json | 2 +- wasm/README.md | 198 ++++++++++++++++++++++++++++------- wasm/www/package.json | 2 +- wasm/www_bench/package.json | 2 +- 8 files changed, 363 insertions(+), 120 deletions(-) diff --git a/.travis.yml b/.travis.yml index d1724f6c..72346336 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 88553831..2b806338 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 d7d16905..be4989e7 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 18598775..f4be3b49 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 jsonObj = selector.path('$..[?(@.age >= 30)]').selectAs(); - let resultObj = [{"name": "친구3", "age": 30}]; - console.log(JSON.stringify(jsonObj) === JSON.stringify(resultObj)); -} +let selector = new jsonpath.Selector() + .path('$..friends[0]') + .value(jsonObj); -{ - 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 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.value({"friends": [ {"name": "친구5", "age": 20} ]}).selectAs(); - let resultObj = [{"name": "친구5", "age": 20}]; - 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 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 a25435fe..3caf5a7e 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 d700c8ec..2596edc3 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) +
npm package -### jsonpath.Selector +```javascript +// browser +import * as jsonpath from "jsonpath-wasm"; +// NodeJs +const jsonpath = require('jsonpath-wasm'); +``` -> The selectTo function is deprecated since 0.1.3. please use the selectAs function instead. +
-```javascript -let 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(); -{ - 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)); -} +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.value({"friends": [ {"name": "친구5", "age": 20} ]}); - let jsonObj = selector.selectAs(); - let resultObj = [{"name": "친구5", "age": 20}]; - 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 } { 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 52180ef2..14e6f85c 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 0a9f8549..ec62adba 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",