Skip to content

Commit 9fc7c32

Browse files
committed
- Refactoring: Use test over exec
- Testing: Increase coverage
1 parent d2606e2 commit 9fc7c32

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/jsonpath.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ const vm = JSONPath.nodeVMSupported
6464

6565
const funcString = funcs.reduce((s, func) => {
6666
let fString = context[func].toString();
67-
if (!(/function/u).exec(fString)) {
67+
if (!(/function/u).test(fString)) {
6868
fString = 'function ' + fString;
6969
}
7070
return 'var ' + func + '=' + fString + ';' + s;

test/test.eval.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,35 @@ checkBuiltInVMAndNodeVM(function (vmType, setBuiltInState) {
6060
assert.deepEqual(result, expected);
6161
});
6262

63+
it('sandbox with function without "function" in string', () => {
64+
const expected = [json.store.book];
65+
const result = jsonpath({
66+
json,
67+
sandbox: {
68+
category () {
69+
return 'reference';
70+
}
71+
},
72+
path: "$..[?(@.category === category())]", wrap: false
73+
});
74+
assert.deepEqual(result, expected);
75+
});
76+
77+
it('sandbox with function with "function" in string', () => {
78+
const expected = [json.store.book];
79+
const result = jsonpath({
80+
json,
81+
sandbox: {
82+
// eslint-disable-next-line object-shorthand
83+
category: function () {
84+
return 'reference';
85+
}
86+
},
87+
path: "$..[?(@.category === category())]", wrap: false
88+
});
89+
assert.deepEqual(result, expected);
90+
});
91+
6392
it('sandbox (with parsing function)', () => {
6493
const expected = [json.store.book];
6594
const result = jsonpath({

0 commit comments

Comments
 (0)