Skip to content

Commit

Permalink
fix initial findings in issue #2
Browse files Browse the repository at this point in the history
  • Loading branch information
f5io committed Jan 28, 2017
1 parent d0875a0 commit 1b4a2ba
Show file tree
Hide file tree
Showing 7 changed files with 231 additions and 19 deletions.
29 changes: 24 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,38 @@
---
Performant JSONPath implementation that focusses on results

**Features**

- Fast
- Extended syntax support
- Error safe, using a simple Maybe functor under the hood
- Memoized compilation of queries
- 100% code coverage

**Syntax**

COMING SOON

**Installation**

```bash
$npm install --save @f5io/jsonpath
$ npm install --save @f5io/jsonpath
```
or if you have yarn installed on your machine

```bash
$yarn add @f5io/jsonpath
$ yarn add @f5io/jsonpath
```

**Usage**

```js
var jp = require('@f5io/jsonpath');
var jsonPathQueryStr = "$..h[?(@.foo>13)]";
var result = jp(jsonPathQueryStr,jsonObject);
const jp = require('@f5io/jsonpath');
const jsonPathQueryStr = '$..h[?(@.foo>13)]';
const result = jp(jsonPathQueryStr,jsonObject);
```

**Contributors**

- Joe Harlow [@f5io](https://github.com/f5io)
- Amit Gupta [@amitguptagql](https://github.com/amitguptagwl)
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@f5io/jsonpath",
"version": "1.0.1",
"version": "1.0.2",
"description": "Performant JSONPath implementation",
"main": "lib/index.js",
"scripts": {
Expand Down
6 changes: 3 additions & 3 deletions src/maybe.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ class Maybe {
}
get() {
return this.value;
}
};
}
}

function maybe(v) {
return new Maybe(v);
};
}
12 changes: 6 additions & 6 deletions src/rules/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import tokenizer from '../tokenizer';
import parser from '../parser';
import { Identity, head, map, isPlainObject } from '../utils';
import { Identity, head, map, mapFilter, isPlainObject } from '../utils';

const rules = {
[1]: {
Expand All @@ -27,7 +27,7 @@ const rules = {
a = a.concat(search(x[k], p));
}
return a;
};
}

const next = arr[i + 1];
if (next && next.type === 'prop')
Expand All @@ -41,12 +41,12 @@ const rules = {
parse: function prop(acc, { value }, i, arr) {
const prev = arr[i - 1];
if (prev && prev.type === 'recurse') return acc;

const getProps = x =>
Array.isArray(x) ?
x.map(y => y[value]) :
mapFilter(y => y[value])(x) :
x[value];

return acc.concat(map(getProps));
}
},
Expand Down Expand Up @@ -189,4 +189,4 @@ function getMatch(str) {
parse,
value: map(mapRes)
};
};
}
12 changes: 11 additions & 1 deletion src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,14 @@ export const isPlainObject = o => {
if (isObjectObject(o.constructor.prototype) === false) return false;
if (o.constructor.prototype.hasOwnProperty('isPrototypeOf') === false) return false;
return true;
};
};

export const mapFilter = f => x => {
const output = [];
let xi;
while (xi = x.shift()) {
const result = f(xi);
if (typeof result !== 'undefined') output.push(result);
}
return output;
};
166 changes: 166 additions & 0 deletions test/change.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
{
"a": {
"1": "la",
"2": "boo",
"h": [
{
"foo": [
1,
2,
3
]
},
{
"foo": [
4,
5,
6
]
},
{
"foo": 12,
"name": "a"
},
{
"foo": 13.5,
"name": {
"h": 45
}
},
{
"foo": 11.8,
"name": "c"
},
true,
123,
[
3,
4,
5
]
]
},
"b": {
"1": "la",
"2": "boo",
"h": [
{
"foo": [
1,
2,
3
]
},
{
"foo": [
4,
5,
6
]
},
{
"foo": 12,
"name": "a"
},
{
"foo": 13.5,
"name": {
"h": 45
}
},
{
"foo": 11.8,
"name": "c"
},
true,
123,
[
3,
4,
5
]
]
},
"c": {
"1": "la",
"2": "boo",
"h": [
{
"foo": [
1,
2,
3
]
},
{
"foo": [
4,
5,
6
]
},
{
"foo": 12,
"name": "a"
},
{
"foo": 13.5,
"name": {
"h": 45
}
},
{
"foo": 11.8,
"name": "c"
},
true,
123,
[
3,
4,
5
]
]
},
"d": {
"1": "la",
"2": "boo",
"h": [
{
"foo": [
1,
2,
3
]
},
{
"foo": [
4,
5,
6
]
},
{
"foo": 12,
"name": "a"
},
{
"foo": 13.5,
"name": {
"h": 45
}
},
{
"foo": 11.8,
"name": "c"
},
true,
123,
[
3,
4,
5
]
]
}
}
23 changes: 20 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ const measure = (name, fn, ...args) => {
return result;
};

const runQueries = (test, queries, expected) => {
const runQueries = (test, queries, expected, d = data) => {
test.plan(queries.length);
queries.forEach(q => {
const qy = measure(`parse: '${q}'`, query, q);
const result = measure(`query: '${q}'`, qy, data);
const result = measure(`query: '${q}'`, qy, d);
test.deepEquals(result, expected, `'${q}' should return expected results`);
});
};
Expand Down Expand Up @@ -270,4 +270,21 @@ test('get bicycle with omember', t => {
'$.store.bicycle{"color"}'
];
runQueries(t, queries, expected);
});
});

test('changejs example', t => {
const expected = [
[ 1, 2, 3 ], [ 4, 5, 6 ],
12, 13.5, 11.8,
[ 1, 2, 3 ], [ 4, 5, 6 ],
12, 13.5, 11.8,
[ 1, 2, 3 ], [ 4, 5, 6 ],
12, 13.5, 11.8,
[ 1, 2, 3 ], [ 4, 5, 6 ],
12, 13.5, 11.8
];
const queries = [
'$..h[*].foo'
];
runQueries(t, queries, expected, require('./change.json'));
});

0 comments on commit 1b4a2ba

Please sign in to comment.