SelectorMut WASM 적용

This commit is contained in:
freestrings
2019-06-10 18:16:08 +09:00
parent 24d18efb6f
commit ec5d76d2d6
7 changed files with 697 additions and 313 deletions

View File

@ -1,358 +1,358 @@
const jsonpath = require('jsonpath-wasm');
let jsonObj = {
"store": {
"book": [
'store': {
'book': [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
'category': 'reference',
'author': 'Nigel Rees',
'title': 'Sayings of the Century',
'price': 8.95,
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
'category': 'fiction',
'author': 'Evelyn Waugh',
'title': 'Sword of Honour',
'price': 12.99,
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
'category': 'fiction',
'author': 'Herman Melville',
'title': 'Moby Dick',
'isbn': '0-553-21311-3',
'price': 8.99,
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
'category': 'fiction',
'author': 'J. R. R. Tolkien',
'title': 'The Lord of the Rings',
'isbn': '0-395-19395-8',
'price': 22.99,
},
],
"bicycle": {
"color": "red",
"price": 19.95
}
'bicycle': {
'color': 'red',
'price': 19.95,
},
},
"expensive": 10
'expensive': 10,
};
let list = {
'$.store.book[*].author': [
"Nigel Rees",
"Evelyn Waugh",
"Herman Melville",
"J. R. R. Tolkien"
'Nigel Rees',
'Evelyn Waugh',
'Herman Melville',
'J. R. R. Tolkien',
],
'$..author':[
"Nigel Rees",
"Evelyn Waugh",
"Herman Melville",
"J. R. R. Tolkien"
'$..author': [
'Nigel Rees',
'Evelyn Waugh',
'Herman Melville',
'J. R. R. Tolkien',
],
'$.store.*': [
[
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
'category': 'reference',
'author': 'Nigel Rees',
'title': 'Sayings of the Century',
'price': 8.95,
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
'category': 'fiction',
'author': 'Evelyn Waugh',
'title': 'Sword of Honour',
'price': 12.99,
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
'category': 'fiction',
'author': 'Herman Melville',
'title': 'Moby Dick',
'isbn': '0-553-21311-3',
'price': 8.99,
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
'category': 'fiction',
'author': 'J. R. R. Tolkien',
'title': 'The Lord of the Rings',
'isbn': '0-395-19395-8',
'price': 22.99,
},
],
{
"color": "red",
"price": 19.95
}
'color': 'red',
'price': 19.95,
},
],
'$.store..price':[
'$.store..price': [
8.95,
12.99,
8.99,
22.99,
19.95
19.95,
],
'$..book[2]': [
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
}
'category': 'fiction',
'author': 'Herman Melville',
'title': 'Moby Dick',
'isbn': '0-553-21311-3',
'price': 8.99,
},
],
'$..book[-2]': [
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
}
'category': 'fiction',
'author': 'Herman Melville',
'title': 'Moby Dick',
'isbn': '0-553-21311-3',
'price': 8.99,
},
],
'$..book[0,1]': [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
'category': 'reference',
'author': 'Nigel Rees',
'title': 'Sayings of the Century',
'price': 8.95,
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
}
'category': 'fiction',
'author': 'Evelyn Waugh',
'title': 'Sword of Honour',
'price': 12.99,
},
],
'$..book[:2]': [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
'category': 'reference',
'author': 'Nigel Rees',
'title': 'Sayings of the Century',
'price': 8.95,
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
}
'category': 'fiction',
'author': 'Evelyn Waugh',
'title': 'Sword of Honour',
'price': 12.99,
},
],
'$..book[1:2]': [
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
}
'category': 'fiction',
'author': 'Evelyn Waugh',
'title': 'Sword of Honour',
'price': 12.99,
},
],
'$..book[-2:]': [
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
'category': 'fiction',
'author': 'Herman Melville',
'title': 'Moby Dick',
'isbn': '0-553-21311-3',
'price': 8.99,
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
'category': 'fiction',
'author': 'J. R. R. Tolkien',
'title': 'The Lord of the Rings',
'isbn': '0-395-19395-8',
'price': 22.99,
},
],
'$..book[2:]': [
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
'category': 'fiction',
'author': 'Herman Melville',
'title': 'Moby Dick',
'isbn': '0-553-21311-3',
'price': 8.99,
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
'category': 'fiction',
'author': 'J. R. R. Tolkien',
'title': 'The Lord of the Rings',
'isbn': '0-395-19395-8',
'price': 22.99,
},
],
'$..book[?(@.isbn)]': [
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
'category': 'fiction',
'author': 'Herman Melville',
'title': 'Moby Dick',
'isbn': '0-553-21311-3',
'price': 8.99,
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
'category': 'fiction',
'author': 'J. R. R. Tolkien',
'title': 'The Lord of the Rings',
'isbn': '0-395-19395-8',
'price': 22.99,
},
],
'$.store.book[?(@.price < 10)]': [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
'category': 'reference',
'author': 'Nigel Rees',
'title': 'Sayings of the Century',
'price': 8.95,
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
}
'category': 'fiction',
'author': 'Herman Melville',
'title': 'Moby Dick',
'isbn': '0-553-21311-3',
'price': 8.99,
},
],
'$..*': [
{
"book": [
'book': [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
'category': 'reference',
'author': 'Nigel Rees',
'title': 'Sayings of the Century',
'price': 8.95,
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
'category': 'fiction',
'author': 'Evelyn Waugh',
'title': 'Sword of Honour',
'price': 12.99,
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
'category': 'fiction',
'author': 'Herman Melville',
'title': 'Moby Dick',
'isbn': '0-553-21311-3',
'price': 8.99,
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
'category': 'fiction',
'author': 'J. R. R. Tolkien',
'title': 'The Lord of the Rings',
'isbn': '0-395-19395-8',
'price': 22.99,
},
],
"bicycle": {
"color": "red",
"price": 19.95
}
'bicycle': {
'color': 'red',
'price': 19.95,
},
},
10,
[
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
'category': 'reference',
'author': 'Nigel Rees',
'title': 'Sayings of the Century',
'price': 8.95,
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
'category': 'fiction',
'author': 'Evelyn Waugh',
'title': 'Sword of Honour',
'price': 12.99,
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
'category': 'fiction',
'author': 'Herman Melville',
'title': 'Moby Dick',
'isbn': '0-553-21311-3',
'price': 8.99,
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
'category': 'fiction',
'author': 'J. R. R. Tolkien',
'title': 'The Lord of the Rings',
'isbn': '0-395-19395-8',
'price': 22.99,
},
],
{
"color": "red",
"price": 19.95
'color': 'red',
'price': 19.95,
},
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
'category': 'reference',
'author': 'Nigel Rees',
'title': 'Sayings of the Century',
'price': 8.95,
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
'category': 'fiction',
'author': 'Evelyn Waugh',
'title': 'Sword of Honour',
'price': 12.99,
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
'category': 'fiction',
'author': 'Herman Melville',
'title': 'Moby Dick',
'isbn': '0-553-21311-3',
'price': 8.99,
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
'category': 'fiction',
'author': 'J. R. R. Tolkien',
'title': 'The Lord of the Rings',
'isbn': '0-395-19395-8',
'price': 22.99,
},
"reference",
"Nigel Rees",
"Sayings of the Century",
'reference',
'Nigel Rees',
'Sayings of the Century',
8.95,
"fiction",
"Evelyn Waugh",
"Sword of Honour",
'fiction',
'Evelyn Waugh',
'Sword of Honour',
12.99,
"fiction",
"Herman Melville",
"Moby Dick",
"0-553-21311-3",
'fiction',
'Herman Melville',
'Moby Dick',
'0-553-21311-3',
8.99,
"fiction",
"J. R. R. Tolkien",
"The Lord of the Rings",
"0-395-19395-8",
'fiction',
'J. R. R. Tolkien',
'The Lord of the Rings',
'0-395-19395-8',
22.99,
"red",
19.95
'red',
19.95,
],
'$..book[ ?( (@.price < 13 || $.store.bicycle.price < @.price) && @.price <=10 ) ]': [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
'category': 'reference',
'author': 'Nigel Rees',
'title': 'Sayings of the Century',
'price': 8.95,
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
}
]
'category': 'fiction',
'author': 'Herman Melville',
'title': 'Moby Dick',
'isbn': '0-553-21311-3',
'price': 8.99,
},
],
};
describe('compile test', () => {
@ -393,34 +393,132 @@ describe('filter test', () => {
}
}
for( var i in list ) {
for (var i in list) {
it(i, (done) => {
run (done, i, list[i]);
})
run(done, i, list[i]);
});
}
it('object equal', (done) => {
let selector = new jsonpath.Selector();
selector.path('$..[?(@.a == 1)]');
selector.value({
"a": 1,
"b" : {"a": 1},
"c" : {"a": 1}
'a': 1,
'b': {'a': 1},
'c': {'a': 1},
});
let result = selector.select();
if (JSON.stringify(result) === JSON.stringify([ {"a": 1}, {"a": 1} ])) {
if (JSON.stringify(result) === JSON.stringify([{'a': 1}, {'a': 1}])) {
done();
}
});
});
describe('SelectorMut test', () => {
it('delete', (done) => {
let jsonObjNew = JSON.parse(JSON.stringify(jsonObj));
let result = jsonpath.deleteValue(jsonObjNew, '$.store.book');
if (JSON.stringify(result) === JSON.stringify({
'store': {
'book': null,
'bicycle': {
'color': 'red',
'price': 19.95,
},
},
'expensive': 10,
})) {
done();
}
});
it('replaceWith', (done) => {
let jsonObjNew = JSON.parse(JSON.stringify(jsonObj));
let result = jsonpath.replaceWith(jsonObjNew, '$.store.book', (v) => {
let ret = v[0];
ret.price = 9;
return ret;
});
if (JSON.stringify(result) === JSON.stringify({
'store': {
'book': {
'category': 'reference',
'author': 'Nigel Rees',
'title': 'Sayings of the Century',
'price': 9,
},
'bicycle': {
'color': 'red',
'price': 19.95,
},
},
'expensive': 10,
})) {
done();
}
});
it('SeletorMut delete', (done) => {
let jsonObjNew = JSON.parse(JSON.stringify(jsonObj));
let selector = new jsonpath.SelectorMut();
selector.path('$.store.book');
selector.value(jsonObjNew);
selector.deleteValue();
let result = selector.take();
if (JSON.stringify(result) === JSON.stringify({
'store': {
'book': null,
'bicycle': {
'color': 'red',
'price': 19.95,
},
},
'expensive': 10,
})) {
done();
}
})
it('SeletorMut replaceWith', (done) => {
let jsonObjNew = JSON.parse(JSON.stringify(jsonObj));
let selector = new jsonpath.SelectorMut();
selector.path('$.store.book');
selector.value(jsonObjNew);
selector.replaceWith((v) => {
let ret = v[0];
ret.price = 9;
return ret;
});
let result = selector.take();
if (JSON.stringify(result) === JSON.stringify({
'store': {
'book': {
'category': 'reference',
'author': 'Nigel Rees',
'title': 'Sayings of the Century',
'price': 9,
},
'bicycle': {
'color': 'red',
'price': 19.95,
},
},
'expensive': 10,
})) {
done();
}
})
});
describe('Selector test', () => {
it('select', (done) => {
let selector = new jsonpath.Selector();
selector.value(jsonObj);
for(var i in list) {
for (var i in list) {
selector.path(i);
if(JSON.stringify(list[i]) !== JSON.stringify(selector.select())) {
if (JSON.stringify(list[i]) !== JSON.stringify(selector.select())) {
throw `fail: ${i}`;
}
}
@ -431,16 +529,16 @@ describe('Selector test', () => {
describe('README test', () => {
it('jsonpath.Selector', (done) => {
let jsonObj = {
"school": {
"friends": [
{"name": "친구1", "age": 20},
{"name": "친구2", "age": 20}
]
'school': {
'friends': [
{'name': '친구1', 'age': 20},
{'name': '친구2', 'age': 20},
],
},
"friends": [
{"name": "친구3", "age": 30},
{"name": "친구4"}
]
'friends': [
{'name': '친구3', 'age': 30},
{'name': '친구4'},
],
};
let selector = new jsonpath.Selector();
@ -449,8 +547,8 @@ describe('README test', () => {
{
selector.path('$..[?(@.age >= 30)]');
let jsonObj = selector.select();
let resultObj = [{"name": "친구3", "age": 30}];
if(JSON.stringify(jsonObj) !== JSON.stringify(resultObj)) {
let resultObj = [{'name': '친구3', 'age': 30}];
if (JSON.stringify(jsonObj) !== JSON.stringify(resultObj)) {
throw 'jsonpath.Selector: $..[?(@.age >= 30)]';
}
}
@ -458,17 +556,17 @@ describe('README test', () => {
{
selector.path('$..[?(@.age == 20)]');
let jsonObj = selector.select();
let resultObj = [{"name": "친구1", "age": 20}, {"name": "친구2", "age": 20}];
if(JSON.stringify(jsonObj) !== JSON.stringify(resultObj)) {
let resultObj = [{'name': '친구1', 'age': 20}, {'name': '친구2', 'age': 20}];
if (JSON.stringify(jsonObj) !== JSON.stringify(resultObj)) {
throw 'jsonpath.Selector: $..[?(@.age >= 20)]';
}
}
{
selector.value({"friends": [ {"name": "친구5", "age": 20} ]});
selector.value({'friends': [{'name': '친구5', 'age': 20}]});
let jsonObj = selector.select();
let resultObj = [{"name": "친구5", "age": 20}];
if(JSON.stringify(jsonObj) !== JSON.stringify(resultObj)) {
let resultObj = [{'name': '친구5', 'age': 20}];
if (JSON.stringify(jsonObj) !== JSON.stringify(resultObj)) {
throw 'jsonpath.Selector: change value';
}
}
@ -478,28 +576,27 @@ describe('README test', () => {
it('jsonpath.select(json: string|object, jsonpath: string)', (done) => {
let jsonObj = {
"school": {
"friends": [
{"name": "친구1", "age": 20},
{"name": "친구2", "age": 20}
]
'school': {
'friends': [
{'name': '친구1', 'age': 20},
{'name': '친구2', 'age': 20},
],
},
"friends": [
{"name": "친구3", "age": 30},
{"name": "친구4"}
]
'friends': [
{'name': '친구3', 'age': 30},
{'name': '친구4'},
],
};
let ret = [
{"name": "친구3", "age": 30},
{"name": "친구1", "age": 20}
{'name': '친구3', 'age': 30},
{'name': '친구1', 'age': 20},
];
let selectAsString = jsonpath.select(JSON.stringify(jsonObj), '$..friends[0]');
let selectAsObj = jsonpath.select(jsonObj, '$..friends[0]');
if(
if (
JSON.stringify(ret) !== JSON.stringify(selectAsString) ||
JSON.stringify(ret) !== JSON.stringify(selectAsObj)
) {
@ -513,27 +610,27 @@ describe('README test', () => {
let template = jsonpath.compile('$..friends[0]');
let jsonObj = {
"school": {
"friends": [
{"name": "친구1", "age": 20},
{"name": "친구2", "age": 20}
]
'school': {
'friends': [
{'name': '친구1', 'age': 20},
{'name': '친구2', 'age': 20},
],
},
"friends": [
{"name": "친구3", "age": 30},
{"name": "친구4"}
]
'friends': [
{'name': '친구3', 'age': 30},
{'name': '친구4'},
],
};
let ret = [
{"name": "친구3", "age": 30},
{"name": "친구1", "age": 20}
{'name': '친구3', 'age': 30},
{'name': '친구1', 'age': 20},
];
let selectAsString = template(JSON.stringify(jsonObj));
let selectAsObj = template(jsonObj);
if(
if (
JSON.stringify(ret) !== JSON.stringify(selectAsString) ||
JSON.stringify(ret) !== JSON.stringify(selectAsObj)
) {
@ -541,24 +638,24 @@ describe('README test', () => {
}
let jsonObj2 = {
"school": {
"friends": [
{"name": "Millicent Norman"},
{"name": "Vincent Cannon"}
]
'school': {
'friends': [
{'name': 'Millicent Norman'},
{'name': 'Vincent Cannon'},
],
},
"friends": [ {"age": 30}, {"age": 40} ]
'friends': [{'age': 30}, {'age': 40}],
};
let ret2 = [
{"age": 30},
{"name": "Millicent Norman"}
{'age': 30},
{'name': 'Millicent Norman'},
];
let selectAsString2 = template(JSON.stringify(jsonObj2));
let selectAsObj2 = template(jsonObj2);
if(
if (
JSON.stringify(ret2) !== JSON.stringify(selectAsString2) ||
JSON.stringify(ret2) !== JSON.stringify(selectAsObj2)
) {
@ -570,33 +667,33 @@ describe('README test', () => {
it('jsonpath.selector(json: string|object)', (done) => {
let jsonObj = {
"school": {
"friends": [
{"name": "친구1", "age": 20},
{"name": "친구2", "age": 20}
]
'school': {
'friends': [
{'name': '친구1', 'age': 20},
{'name': '친구2', 'age': 20},
],
},
"friends": [
{"name": "친구3", "age": 30},
{"name": "친구4"}
]
'friends': [
{'name': '친구3', 'age': 30},
{'name': '친구4'},
],
};
let ret1 = [
{"name": "친구3", "age": 30},
{"name": "친구1", "age": 20}
{'name': '친구3', 'age': 30},
{'name': '친구1', 'age': 20},
];
let ret2 = [
{"name": "친구4"},
{"name": "친구2", "age": 20}
{'name': '친구4'},
{'name': '친구2', 'age': 20},
];
let selector = jsonpath.selector(jsonObj);
let select1 = selector('$..friends[0]');
let select2 = selector('$..friends[1]');
if(
if (
JSON.stringify(ret1) !== JSON.stringify(select1) ||
JSON.stringify(ret2) !== JSON.stringify(select2)
) {