Skip to content

Commit 6d31ded

Browse files
committed
fix: allow falsey at values in filter (now may require checking for presence of @ in some cases); fixes #136
1 parent ac7a453 commit 6d31ded

12 files changed

+28
-28
lines changed

badges/tests-badge.svg

+1-1
Loading

dist/index-browser-esm.js

-5
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,6 @@ JSONPath.prototype.evaluate = function (expr, json, callback, otherTypeCallback)
477477
return undefined;
478478
}
479479

480-
this._obj = json;
481480
var exprList = JSONPath.toPathArray(expr);
482481

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

869868
JSONPath.prototype._eval = function (code, _v, _vname, path, parent, parentPropName) {
870-
if (!this._obj || !_v) {
871-
return false;
872-
}
873-
874869
if (code.includes('@parentProperty')) {
875870
this.currSandbox._$_parentProperty = parentPropName;
876871
code = code.replace(/@parentProperty/g, '_$_parentProperty');

dist/index-browser-esm.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index-browser-esm.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index-browser-umd.js

-5
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,6 @@
483483
return undefined;
484484
}
485485

486-
this._obj = json;
487486
var exprList = JSONPath.toPathArray(expr);
488487

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

875874
JSONPath.prototype._eval = function (code, _v, _vname, path, parent, parentPropName) {
876-
if (!this._obj || !_v) {
877-
return false;
878-
}
879-
880875
if (code.includes('@parentProperty')) {
881876
this.currSandbox._$_parentProperty = parentPropName;
882877
code = code.replace(/@parentProperty/g, '_$_parentProperty');

dist/index-browser-umd.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index-browser-umd.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index-node-cjs.js

-5
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,6 @@ JSONPath.prototype.evaluate = function (expr, json, callback, otherTypeCallback)
234234
return undefined;
235235
}
236236

237-
this._obj = json;
238237
const exprList = JSONPath.toPathArray(expr);
239238

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

614613
JSONPath.prototype._eval = function (code, _v, _vname, path, parent, parentPropName) {
615-
if (!this._obj || !_v) {
616-
return false;
617-
}
618-
619614
if (code.includes('@parentProperty')) {
620615
this.currSandbox._$_parentProperty = parentPropName;
621616
code = code.replace(/@parentProperty/gu, '_$_parentProperty');

dist/index-node-esm.mjs

-5
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,6 @@ JSONPath.prototype.evaluate = function (expr, json, callback, otherTypeCallback)
226226
return undefined;
227227
}
228228

229-
this._obj = json;
230229
const exprList = JSONPath.toPathArray(expr);
231230

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

606605
JSONPath.prototype._eval = function (code, _v, _vname, path, parent, parentPropName) {
607-
if (!this._obj || !_v) {
608-
return false;
609-
}
610-
611606
if (code.includes('@parentProperty')) {
612607
this.currSandbox._$_parentProperty = parentPropName;
613608
code = code.replace(/@parentProperty/gu, '_$_parentProperty');

src/jsonpath.js

-2
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,6 @@ JSONPath.prototype.evaluate = function (
225225
if ((!expr && expr !== '') || !json) {
226226
return undefined;
227227
}
228-
this._obj = json;
229228

230229
const exprList = JSONPath.toPathArray(expr);
231230
if (exprList[0] === '$' && exprList.length > 1) { exprList.shift(); }
@@ -582,7 +581,6 @@ JSONPath.prototype._slice = function (
582581
JSONPath.prototype._eval = function (
583582
code, _v, _vname, path, parent, parentPropName
584583
) {
585-
if (!this._obj || !_v) { return false; }
586584
if (code.includes('@parentProperty')) {
587585
this.currSandbox._$_parentProperty = parentPropName;
588586
code = code.replace(/@parentProperty/gu, '_$_parentProperty');

test/test.at_and_dollar.js

+22
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,26 @@ describe('JSONPath - At and Dollar sign', function () {
3030
assert.strictEqual(jsonpath({json: t1, path: '$.`$.`@'})[0], t1.$['@']);
3131
assert.strictEqual(jsonpath({json: t1, path: '\\@'})[1], undefined);
3232
});
33+
34+
it('@ as false', () => {
35+
const json = {
36+
a: {
37+
b: false
38+
}
39+
};
40+
const expected = [false];
41+
const result = jsonpath({json, path: "$..*[?(@ === false)]", wrap: false});
42+
assert.deepEqual(result, expected);
43+
});
44+
45+
it('@ as 0', function () {
46+
const json = {
47+
a: {
48+
b: 0
49+
}
50+
};
51+
const expected = [0];
52+
const result = jsonpath({json, path: "$.a[?(@property === 'b' && @ < 1)]", wrap: false});
53+
assert.deepEqual(result, expected);
54+
});
3355
});

test/test.properties.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ checkBuiltInVMAndNodeVM(function (vmType, setBuiltInState) {
3737
it('At signs within properties (null data)', () => {
3838
const result = jsonpath({json: {
3939
datafield: [null]
40-
}, path: "$.datafield[?(@.tag=='xxx')]", wrap: false});
40+
}, path: "$.datafield[?(@ && @.tag=='xxx')]", wrap: false});
4141
assert.deepEqual(result, undefined);
4242
});
4343

0 commit comments

Comments
 (0)