Skip to content

Commit

Permalink
refactor(base): migrate search utils to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars committed Sep 10, 2021
1 parent a634a89 commit 0ff4330
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 27 deletions.
7 changes: 5 additions & 2 deletions packages/@sanity/base/src/preview/utils/optimizeQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,14 @@ function toSubQuery({ids, fields}: {ids: string[]; fields: string[]}) {
.join(',')}}`
}

export function toGradientQuery(combinedSelections: CombinedSelection[]) {
export function toGradientQuery(combinedSelections: CombinedSelection[]): string {
return `[${combinedSelections.map(toSubQuery).join(',')}][0...${combinedSelections.length}]`
}

export function reassemble(queryResult: Result[], combinedSelections: CombinedSelection[]) {
export function reassemble(
queryResult: Result[],
combinedSelections: CombinedSelection[]
): (Doc | null)[] {
return queryResult.reduce((reprojected: (Doc | null)[], subResult, index) => {
const map = combinedSelections[index].map
map.forEach((resultIdx, i) => {
Expand Down
23 changes: 0 additions & 23 deletions packages/@sanity/base/src/util/searchUtils.js

This file was deleted.

35 changes: 35 additions & 0 deletions packages/@sanity/base/src/util/searchUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
type SearchPathSegment = string | []

const GROQ_KEYWORDS = ['match', 'in', 'asc', 'desc', 'true', 'false', 'null']
const VALID_FIELD = /^[a-zA-Z_][a-zA-Z0-9_]*$/

export const fieldNeedsEscape = (fieldName: string): boolean =>
!VALID_FIELD.test(fieldName) || GROQ_KEYWORDS.includes(fieldName)

export const escapeField = (fieldName: string): string => `["${fieldName}"]`

const escapeFirst = (fieldName: string): string => `@${escapeField(fieldName)}`

const isEmptyArray = (value: unknown): value is [] => Array.isArray(value) && value.length === 0

export const joinPath = (pathArray: SearchPathSegment[]): string => {
let path = ''
for (let i = 0; i < pathArray.length; i++) {
const pathSegment = pathArray[i]
if (isEmptyArray(pathSegment)) {
path += `[]`
continue
}

const isFirst = i === 0
const needsEscape = fieldNeedsEscape(pathSegment)

if (needsEscape) {
path = isFirst ? escapeFirst(pathSegment) : `${path}${escapeField(pathSegment)}`
} else {
path = isFirst ? pathSegment : `${path}.${pathSegment}`
}
}

return path
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ test('fieldNeedsEscape', () => {
expect(fieldNeedsEscape('foobar123')).toBe(false)

// Keywords
;[('match', 'in', 'asc', 'desc', 'true', 'false', 'null')].forEach((kw) => {
;['match', 'in', 'asc', 'desc', 'true', 'false', 'null'].forEach((kw) => {
expect(fieldNeedsEscape(kw)).toBe(true)
})
})
Expand All @@ -22,7 +22,7 @@ test('escapeField', () => {
expect(escapeField('foobar')).toBe('["foobar"]')

// Keywords
;[('match', 'in', 'asc', 'desc', 'true', 'false', 'null')].forEach((kw) => {
;['match', 'in', 'asc', 'desc', 'true', 'false', 'null'].forEach((kw) => {
expect(escapeField(kw)).toBe(`["${kw}"]`)
})
})
Expand Down

0 comments on commit 0ff4330

Please sign in to comment.