Skip to content

Commit

Permalink
fix: allow falsey at values in filter (now may require checking for p…
Browse files Browse the repository at this point in the history
…resence of `@` in some cases); fixes #136
  • Loading branch information
brettz9 committed Mar 1, 2021
1 parent ac7a453 commit 6d31ded
Show file tree
Hide file tree
Showing 12 changed files with 28 additions and 28 deletions.
2 changes: 1 addition & 1 deletion badges/tests-badge.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 0 additions & 5 deletions dist/index-browser-esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,6 @@ JSONPath.prototype.evaluate = function (expr, json, callback, otherTypeCallback)
return undefined;
}

this._obj = json;
var exprList = JSONPath.toPathArray(expr);

if (exprList[0] === '$' && exprList.length > 1) {
Expand Down Expand Up @@ -867,10 +866,6 @@ JSONPath.prototype._slice = function (loc, expr, val, path, parent, parentPropNa
};

JSONPath.prototype._eval = function (code, _v, _vname, path, parent, parentPropName) {
if (!this._obj || !_v) {
return false;
}

if (code.includes('@parentProperty')) {
this.currSandbox._$_parentProperty = parentPropName;
code = code.replace(/@parentProperty/g, '_$_parentProperty');
Expand Down
2 changes: 1 addition & 1 deletion dist/index-browser-esm.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index-browser-esm.min.js.map

Large diffs are not rendered by default.

5 changes: 0 additions & 5 deletions dist/index-browser-umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,6 @@
return undefined;
}

this._obj = json;
var exprList = JSONPath.toPathArray(expr);

if (exprList[0] === '$' && exprList.length > 1) {
Expand Down Expand Up @@ -873,10 +872,6 @@
};

JSONPath.prototype._eval = function (code, _v, _vname, path, parent, parentPropName) {
if (!this._obj || !_v) {
return false;
}

if (code.includes('@parentProperty')) {
this.currSandbox._$_parentProperty = parentPropName;
code = code.replace(/@parentProperty/g, '_$_parentProperty');
Expand Down
2 changes: 1 addition & 1 deletion dist/index-browser-umd.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index-browser-umd.min.js.map

Large diffs are not rendered by default.

5 changes: 0 additions & 5 deletions dist/index-node-cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,6 @@ JSONPath.prototype.evaluate = function (expr, json, callback, otherTypeCallback)
return undefined;
}

this._obj = json;
const exprList = JSONPath.toPathArray(expr);

if (exprList[0] === '$' && exprList.length > 1) {
Expand Down Expand Up @@ -612,10 +611,6 @@ JSONPath.prototype._slice = function (loc, expr, val, path, parent, parentPropNa
};

JSONPath.prototype._eval = function (code, _v, _vname, path, parent, parentPropName) {
if (!this._obj || !_v) {
return false;
}

if (code.includes('@parentProperty')) {
this.currSandbox._$_parentProperty = parentPropName;
code = code.replace(/@parentProperty/gu, '_$_parentProperty');
Expand Down
5 changes: 0 additions & 5 deletions dist/index-node-esm.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,6 @@ JSONPath.prototype.evaluate = function (expr, json, callback, otherTypeCallback)
return undefined;
}

this._obj = json;
const exprList = JSONPath.toPathArray(expr);

if (exprList[0] === '$' && exprList.length > 1) {
Expand Down Expand Up @@ -604,10 +603,6 @@ JSONPath.prototype._slice = function (loc, expr, val, path, parent, parentPropNa
};

JSONPath.prototype._eval = function (code, _v, _vname, path, parent, parentPropName) {
if (!this._obj || !_v) {
return false;
}

if (code.includes('@parentProperty')) {
this.currSandbox._$_parentProperty = parentPropName;
code = code.replace(/@parentProperty/gu, '_$_parentProperty');
Expand Down
2 changes: 0 additions & 2 deletions src/jsonpath.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,6 @@ JSONPath.prototype.evaluate = function (
if ((!expr && expr !== '') || !json) {
return undefined;
}
this._obj = json;

const exprList = JSONPath.toPathArray(expr);
if (exprList[0] === '$' && exprList.length > 1) { exprList.shift(); }
Expand Down Expand Up @@ -582,7 +581,6 @@ JSONPath.prototype._slice = function (
JSONPath.prototype._eval = function (
code, _v, _vname, path, parent, parentPropName
) {
if (!this._obj || !_v) { return false; }
if (code.includes('@parentProperty')) {
this.currSandbox._$_parentProperty = parentPropName;
code = code.replace(/@parentProperty/gu, '_$_parentProperty');
Expand Down
22 changes: 22 additions & 0 deletions test/test.at_and_dollar.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,26 @@ describe('JSONPath - At and Dollar sign', function () {
assert.strictEqual(jsonpath({json: t1, path: '$.`$.`@'})[0], t1.$['@']);
assert.strictEqual(jsonpath({json: t1, path: '\\@'})[1], undefined);
});

it('@ as false', () => {
const json = {
a: {
b: false
}
};
const expected = [false];
const result = jsonpath({json, path: "$..*[?(@ === false)]", wrap: false});
assert.deepEqual(result, expected);
});

it('@ as 0', function () {
const json = {
a: {
b: 0
}
};
const expected = [0];
const result = jsonpath({json, path: "$.a[?(@property === 'b' && @ < 1)]", wrap: false});
assert.deepEqual(result, expected);
});
});
2 changes: 1 addition & 1 deletion test/test.properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ checkBuiltInVMAndNodeVM(function (vmType, setBuiltInState) {
it('At signs within properties (null data)', () => {
const result = jsonpath({json: {
datafield: [null]
}, path: "$.datafield[?(@.tag=='xxx')]", wrap: false});
}, path: "$.datafield[?(@ && @.tag=='xxx')]", wrap: false});
assert.deepEqual(result, undefined);
});

Expand Down

0 comments on commit 6d31ded

Please sign in to comment.