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 1 commit
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
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,22 @@ 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(/^,+|,+$/, ""))
}