Skip to content

Commit

Permalink
support for non-latin characters in search
Browse files Browse the repository at this point in the history
  • Loading branch information
jlarsson committed Jul 11, 2023
1 parent 75a1ecb commit d6b40b3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/adverts/filters/advert-filter-predicate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,19 @@ describe('createAdvertFilterPredicate', () => {
.map(({id}) => id))
.toMatchObject(['advert-30'])
})

it('bugfix: supports exotic characters', () => {
const p = createAdvertFilterPredicate(createTestUser(), {search: 'ÅÄÖ'})

const adverts = createSampleAdverts(100, {
'advert-10': {title: 'I like my unicorn named åäö!'},
'advert-20': {description: ' UniCorns are the best in the whole wåäörld'}
});

expect(
adverts.filter(p)
.map(({id}) => id))
.toMatchObject(['advert-10', 'advert-20'])
})

})
3 changes: 2 additions & 1 deletion src/adverts/filters/advert-filter-predicate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const regexpEscape = (s: string): string => s.replace(/[|\\{}()[\]^$+*?.]/g, '\\
const createFreeTextPredicate = (search: string): Predicate<Advert> => {
// extract individual words from search

const matchers = ((search||'').match(/(\w+)/g) || [])
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions/Character_classes#looking_for_a_word_from_unicode_characters
const matchers = ((search||'').match(/([\u0000-\u0019\u0021-\uFFFF]+)/gm) || [])
.filter(v => v)
.filter(v => v.length >= 3)
.map(regexpEscape)
Expand Down

0 comments on commit d6b40b3

Please sign in to comment.