Skip to content

Commit

Permalink
Fix MATCH string containing regex symbols (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolashefti committed Mar 22, 2023
1 parent e92fece commit 92c5582
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/lookup-reference.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,17 @@ export function MATCH(lookup_value, lookup_array, match_type) {
}
} else if (match_type === 0) {
if (typeof lookup_value === 'string' && typeof lookup_array[idx] === 'string') {
const lookupValueStr = lookup_value.toLowerCase().replace(/\?/g, '.').replace(/\*/g, '.*').replace(/~/g, '\\')
const lookupValueStr = lookup_value
.toLowerCase()
.replace(/\?/g, '.')
.replace(/\*/g, '.*')
.replace(/~/g, '\\')
.replace(/\+/g, '\\+')
.replace(/\(/g, '\\(')
.replace(/\)/g, '\\)')
.replace(/\[/g, '\\[')
.replace(/\]/g, '\\]')

const regex = new RegExp('^' + lookupValueStr + '$')

if (regex.test(lookup_array[idx].toLowerCase())) {
Expand Down
7 changes: 7 additions & 0 deletions test/lookup-reference.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ describe('Lookup Reference', () => {
expect(lookup.MATCH('selected', ['not_selected', 'not_selected', 'selected', 'not_selected'], 0)).to.equal(3)
})

it('should match with string containing ignored regex symbols', () => {
expect(lookup.MATCH('Under (200)', [['Under (200)'], ['200-350']], 0)).to.equal(1)
expect(lookup.MATCH('Under [200]', [['Under [200]'], ['200-350']], 0)).to.equal(1)
expect(lookup.MATCH('500+', [['Under 200'], ['200-349'], ['350-500'], ['500+']], 0)).to.equal(4)
expect(lookup.MATCH('5*3', [['Under 200'], ['200-349'], ['5*3'], ['500+']], 0)).to.equal(3)
})

it('should work with mixed type elements in the lookup_array', () => {
expect(lookup.MATCH('jimc', ['jima', 4, null, undefined, 'jimc', 'bernie'], 0)).to.equal(5)
})
Expand Down

0 comments on commit 92c5582

Please sign in to comment.