Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed RegExp in filter-adapter.ts and sort-context.ts to work in Safari #1279

Merged
merged 6 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/twelve-knives-rush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@meilisearch/instant-meilisearch": minor
---

Fixed RegExp in filter-adapter.ts and sort-context.ts to work in Safari
12 changes: 12 additions & 0 deletions packages/instant-meilisearch/__tests__/sort.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ describe('Sort browser test', () => {
])
})

test('split multiple sorting rules in different order', () => {
const sortRules = splitSortString(
'title:asc,_geoPoint(37.8153, -122.4784):asc,description:desc'
)

expect(sortRules).toEqual([
'title:asc',
'_geoPoint(37.8153, -122.4784):asc',
'description:desc',
])
})

test('split one sorting rule', () => {
const sortRules = splitSortString('_geoPoint(37.8153, -122.4784):asc')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ function transformFacetFilter(filter: string): string {
return `"${attribute}"="${value}"`
}

// Matches first occurrence of an operator
const numericSplitRegExp = /(?<!(?:[<!>]?=|<|>|:).*)([<!>]?=|<|>|:)/

/**
* Transform InstantSearch [numeric filter](https://www.algolia.com/doc/api-reference/api-parameters/numericFilters/)
* to Meilisearch compatible filter format.
Expand All @@ -47,9 +44,25 @@ const numericSplitRegExp = /(?<!(?:[<!>]?=|<|>|:).*)([<!>]?=|<|>|:)/
* @returns {string}
*/
function transformNumericFilter(filter: string): string {
// TODO: Warn users to not enable facet values escape for negative numbers.
// https://github.com/algolia/instantsearch/blob/da701529ed325bb7a1d782e80cb994711e20d94a/packages/instantsearch.js/src/lib/utils/escapeFacetValue.ts#L13-L21
const [attribute, operator, value] = filter.split(numericSplitRegExp)
const splitNumericFilter = (): [string, string, string] => {
const attributeMatch = filter.match(/^([^<!>:=]*)([<!>:=]+)(.*)$/)

if (attributeMatch) {
const [attribute, dirtyOperator, valueEnd] = attributeMatch.slice(1)
const operatorMatch = dirtyOperator.match(/^([<!>]?=|<|>|:){1}(.*)/) || [
'',
'',
]
const [operator, valueStart] = operatorMatch.slice(1)
const cleanedValue = valueStart + valueEnd

return [attribute, operator, cleanedValue]
}

return [filter, '', '']
}

const [attribute, operator, value] = splitNumericFilter()
const escapedAttribute = getValueWithEscapedBackslashesAndQuotes(attribute)
return `"${escapedAttribute.trim()}"${
operator === ':' ? ' ' : operator
Expand Down
10 changes: 8 additions & 2 deletions packages/instant-meilisearch/src/contexts/sort-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@
*/
export function splitSortString(sortStr: string): string[] {
if (!sortStr) return []
const sortRules = sortStr.split(/,(?=\w+:(?:asc|desc))/)
const regex = /[^:]+:(?:asc|desc)/g
const sortRules: string[] = []

return sortRules
let match
while ((match = regex.exec(sortStr)) !== null) {
sortRules.push(match[0])
}

return sortRules.map((str) => str.replace(/^,+|,+$/, ''))
}