Skip to content

Commit

Permalink
fix(sanity): do not peform incremental search for exact tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
juice49 committed Nov 29, 2024
1 parent d2cb895 commit a63bb7b
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions packages/sanity/src/core/search/text-search/createTextSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,21 +94,30 @@ export function isPrefixToken(token: string | undefined): boolean {
return typeof token !== 'undefined' && token.trim().at(-1) === WILDCARD_TOKEN
}

// TODO: Solidify and test.
export function isExactMatchToken(token: string | undefined): boolean {
return [token?.at(0), token?.at(-1)].every((character) => character === '"')
}

export function prefixLast(query: string): string {
const tokens = (query.match(TOKEN_REGEX) ?? []).map((token) => token.trim())
const finalNonNegationTokenIndex = tokens.findLastIndex((token) => !isNegationToken(token))
const finalNonNegationToken = tokens[finalNonNegationTokenIndex]

const finalIncrementalTokenIndex = tokens.findLastIndex(
(token) => !isNegationToken(token) && !isExactMatchToken(token),
)

const finalIncrementalToken = tokens[finalIncrementalTokenIndex]

if (tokens.length === 0) {
return WILDCARD_TOKEN
}

if (isPrefixToken(finalNonNegationToken) || typeof finalNonNegationToken === 'undefined') {
if (isPrefixToken(finalIncrementalToken) || typeof finalIncrementalToken === 'undefined') {
return tokens.join(' ')
}

const prefixedTokens = [...tokens]
prefixedTokens.splice(finalNonNegationTokenIndex, 1, `${finalNonNegationToken}${WILDCARD_TOKEN}`)
prefixedTokens.splice(finalIncrementalTokenIndex, 1, `${finalIncrementalToken}${WILDCARD_TOKEN}`)
return prefixedTokens.join(' ')
}

Expand Down

0 comments on commit a63bb7b

Please sign in to comment.