Skip to content

Commit

Permalink
Calling .includes on null causes a crash. This resolves that behavior.
Browse files Browse the repository at this point in the history
  • Loading branch information
TotalTechGeek committed Dec 6, 2023
1 parent ab7a103 commit 76a5478
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 2 additions & 2 deletions defaultMethods.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const defaultMethods = {
'%': (data) => data.reduce((a, b) => +a % +b),
max: (data) => Math.max(...data),
min: (data) => Math.min(...data),
in: ([item, array]) => array.includes(item),
in: ([item, array]) => (array || []).includes(item),
'>': ([a, b]) => a > b,
'<': ([a, b, c]) => (c === undefined ? a < b : a < b && b < c),
preserve: {
Expand Down Expand Up @@ -725,7 +725,7 @@ defaultMethods.or.compile = function (data, buildState) {
// @ts-ignore Allow custom attribute
defaultMethods.in.compile = function (data, buildState) {
if (Array.isArray(data)) {
return `(${buildString(data[1], buildState)}).includes(${buildString(
return `(${buildString(data[1], buildState)} || []).includes(${buildString(
data[0],
buildState
)})`
Expand Down
14 changes: 14 additions & 0 deletions general.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,18 @@ describe('Various Test Cases', () => {
for (const engine of [...normalEngines, ...permissiveEngines]) await testEngine(engine, { var: '\\foo' }, { '\\foo': 2 }, 2)
for (const engine of [...normalEngines, ...permissiveEngines]) await testEngine(engine, { var: '\\\\foo' }, { '\\foo': 2 }, 2)
})

it('should be able to handle various instances of "in" with good, bad and invalid data', async () => {
const rule = {
in: ['Spring', { var: 'city' }]
}

const goodData = { city: 'Springfield' }
const badData = { city: 'test' }
const invalidData = { city: null }

for (const engine of normalEngines) await testEngine(engine, rule, goodData, true)
for (const engine of normalEngines) await testEngine(engine, rule, badData, false)
for (const engine of normalEngines) await testEngine(engine, rule, invalidData, false)
})
})

0 comments on commit 76a5478

Please sign in to comment.