Skip to content

Commit efb57ec

Browse files
Merge #1279
1279: Fixed RegExp in filter-adapter.ts and sort-context.ts to work in Safari r=curquiza a=papaIOprog # Pull Request ## Related issue Fixes #1277 ## What does this PR do? - Replaces regular expressions that use [lookbehind](https://caniuse.com/js-regexp-lookbehind). - Resolves a bug specifically affecting Safari. Co-authored-by: papaIOprog <[email protected]> Co-authored-by: papaIOprog <[email protected]>
2 parents 4d9991e + aeb1b4a commit efb57ec

File tree

4 files changed

+44
-8
lines changed

4 files changed

+44
-8
lines changed

.changeset/twelve-knives-rush.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@meilisearch/instant-meilisearch": minor
3+
---
4+
5+
Fixed RegExp in filter-adapter.ts and sort-context.ts to work in Safari

packages/instant-meilisearch/__tests__/sort.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,18 @@ describe('Sort browser test', () => {
6363
])
6464
})
6565

66+
test('split multiple sorting rules in different order', () => {
67+
const sortRules = splitSortString(
68+
'title:asc,_geoPoint(37.8153, -122.4784):asc,description:desc'
69+
)
70+
71+
expect(sortRules).toEqual([
72+
'title:asc',
73+
'_geoPoint(37.8153, -122.4784):asc',
74+
'description:desc',
75+
])
76+
})
77+
6678
test('split one sorting rule', () => {
6779
const sortRules = splitSortString('_geoPoint(37.8153, -122.4784):asc')
6880

packages/instant-meilisearch/src/adapter/search-request-adapter/filter-adapter.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ function transformFacetFilter(filter: string): string {
2929
return `"${attribute}"="${value}"`
3030
}
3131

32-
// Matches first occurrence of an operator
33-
const numericSplitRegExp = /(?<!(?:[<!>]?=|<|>|:).*)([<!>]?=|<|>|:)/
34-
3532
/**
3633
* Transform InstantSearch [numeric filter](https://www.algolia.com/doc/api-reference/api-parameters/numericFilters/)
3734
* to Meilisearch compatible filter format.
@@ -47,9 +44,25 @@ const numericSplitRegExp = /(?<!(?:[<!>]?=|<|>|:).*)([<!>]?=|<|>|:)/
4744
* @returns {string}
4845
*/
4946
function transformNumericFilter(filter: string): string {
50-
// TODO: Warn users to not enable facet values escape for negative numbers.
51-
// https://github.com/algolia/instantsearch/blob/da701529ed325bb7a1d782e80cb994711e20d94a/packages/instantsearch.js/src/lib/utils/escapeFacetValue.ts#L13-L21
52-
const [attribute, operator, value] = filter.split(numericSplitRegExp)
47+
const splitNumericFilter = (): [string, string, string] => {
48+
const attributeMatch = filter.match(/^([^<!>:=]*)([<!>:=]+)(.*)$/)
49+
50+
if (attributeMatch) {
51+
const [attribute, dirtyOperator, valueEnd] = attributeMatch.slice(1)
52+
const operatorMatch = dirtyOperator.match(/^([<!>]?=|<|>|:){1}(.*)/) || [
53+
'',
54+
'',
55+
]
56+
const [operator, valueStart] = operatorMatch.slice(1)
57+
const cleanedValue = valueStart + valueEnd
58+
59+
return [attribute, operator, cleanedValue]
60+
}
61+
62+
return [filter, '', '']
63+
}
64+
65+
const [attribute, operator, value] = splitNumericFilter()
5366
const escapedAttribute = getValueWithEscapedBackslashesAndQuotes(attribute)
5467
return `"${escapedAttribute.trim()}"${
5568
operator === ':' ? ' ' : operator

packages/instant-meilisearch/src/contexts/sort-context.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@
1616
*/
1717
export function splitSortString(sortStr: string): string[] {
1818
if (!sortStr) return []
19-
const sortRules = sortStr.split(/,(?=\w+:(?:asc|desc))/)
19+
const regex = /[^:]+:(?:asc|desc)/g
20+
const sortRules: string[] = []
2021

21-
return sortRules
22+
let match
23+
while ((match = regex.exec(sortStr)) !== null) {
24+
sortRules.push(match[0])
25+
}
26+
27+
return sortRules.map((str) => str.replace(/^,+|,+$/, ''))
2228
}

0 commit comments

Comments
 (0)