Skip to content

Commit 2a043a7

Browse files
Merge pull request #26 from TotalTechGeek/bugfix/resolve-in-behavior-25
Calling .includes on null causes a crash. This resolves that behavior.
2 parents ab7a103 + 76a5478 commit 2a043a7

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

defaultMethods.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const defaultMethods = {
4747
'%': (data) => data.reduce((a, b) => +a % +b),
4848
max: (data) => Math.max(...data),
4949
min: (data) => Math.min(...data),
50-
in: ([item, array]) => array.includes(item),
50+
in: ([item, array]) => (array || []).includes(item),
5151
'>': ([a, b]) => a > b,
5252
'<': ([a, b, c]) => (c === undefined ? a < b : a < b && b < c),
5353
preserve: {
@@ -725,7 +725,7 @@ defaultMethods.or.compile = function (data, buildState) {
725725
// @ts-ignore Allow custom attribute
726726
defaultMethods.in.compile = function (data, buildState) {
727727
if (Array.isArray(data)) {
728-
return `(${buildString(data[1], buildState)}).includes(${buildString(
728+
return `(${buildString(data[1], buildState)} || []).includes(${buildString(
729729
data[0],
730730
buildState
731731
)})`

general.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,18 @@ describe('Various Test Cases', () => {
134134
for (const engine of [...normalEngines, ...permissiveEngines]) await testEngine(engine, { var: '\\foo' }, { '\\foo': 2 }, 2)
135135
for (const engine of [...normalEngines, ...permissiveEngines]) await testEngine(engine, { var: '\\\\foo' }, { '\\foo': 2 }, 2)
136136
})
137+
138+
it('should be able to handle various instances of "in" with good, bad and invalid data', async () => {
139+
const rule = {
140+
in: ['Spring', { var: 'city' }]
141+
}
142+
143+
const goodData = { city: 'Springfield' }
144+
const badData = { city: 'test' }
145+
const invalidData = { city: null }
146+
147+
for (const engine of normalEngines) await testEngine(engine, rule, goodData, true)
148+
for (const engine of normalEngines) await testEngine(engine, rule, badData, false)
149+
for (const engine of normalEngines) await testEngine(engine, rule, invalidData, false)
150+
})
137151
})

0 commit comments

Comments
 (0)