Skip to content

Commit

Permalink
fix(sanity): do not perform incremental search for exact tokens (#7972)
Browse files Browse the repository at this point in the history
  • Loading branch information
juice49 authored Dec 9, 2024
1 parent 5d5bac0 commit 9fba9b7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
getDocumentTypeConfiguration,
getOrder,
getQueryString,
isExactMatchToken,
isNegationToken,
isPrefixToken,
prefixLast,
Expand Down Expand Up @@ -268,7 +269,7 @@ describe('prefixLast', () => {
expect(prefixLast('a b')).toBe('a b*')
expect(prefixLast('a -b')).toBe('a* -b')
expect(prefixLast('a "bc" d')).toBe('a "bc" d*')
expect(prefixLast('ab "cd"')).toBe('ab "cd"*')
expect(prefixLast('ab "cd"')).toBe('ab* "cd"')
expect(prefixLast('a --')).toBe('a* --')
})

Expand All @@ -288,7 +289,19 @@ describe('prefixLast', () => {

it('preserves quoted tokens', () => {
expect(prefixLast('"a b" c d')).toBe('"a b" c d*')
expect(prefixLast('"a b" c d "ef" "g "')).toBe('"a b" c d "ef" "g "*')
expect(prefixLast('"a b" c d "ef" "g "')).toBe('"a b" c d* "ef" "g "')
expect(prefixLast('"a " b" c d')).toBe('"a " b c d*')
})
})

describe('isExactMatchToken', () => {
it('recognises that a token is encased in quote marks', () => {
expect(isExactMatchToken(undefined)).toBe(false)
expect(isExactMatchToken('"a"')).toBe(true)
expect(isExactMatchToken('"a b"')).toBe(true)
expect(isExactMatchToken('"a')).toBe(false)
expect(isExactMatchToken('a"')).toBe(false)
expect(isExactMatchToken('"a b')).toBe(false)
expect(isExactMatchToken('a b"')).toBe(false)
})
})
16 changes: 12 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,29 @@ export function isPrefixToken(token: string | undefined): boolean {
return typeof token !== 'undefined' && token.trim().at(-1) === WILDCARD_TOKEN
}

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 9fba9b7

Please sign in to comment.