From 522477ab5beef02a45d636e15c9c3bcb42609f49 Mon Sep 17 00:00:00 2001 From: Ash Date: Mon, 18 Mar 2024 16:47:31 +0000 Subject: [PATCH] refactor(core): use path `toString` function from `@sanity/util/paths` (#5987) ### Description When deriving search weights, we previously relied on a new `pathToString` function that replicated much of the functionality already implemented in the `toString` function from `@sanity/util/paths`. The one additional piece of functionality (handling segments that are `true`, `false`, or `null`) has now been implemented in #5986. Once merged, we can remove the redundant `pathToString` function and switch to the `toString` function from `@sanity/util/paths`. ### What to review - Search requests are constructed correctly. ### Testing There are tests for this functionality in `packages/sanity/src/core/search/common/__tests__/deriveSearchWeightsFromType.test.ts`. --- .../common/deriveSearchWeightsFromType.ts | 20 ++----------------- 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/packages/sanity/src/core/search/common/deriveSearchWeightsFromType.ts b/packages/sanity/src/core/search/common/deriveSearchWeightsFromType.ts index abc6fdf4a24..341ad414fa7 100644 --- a/packages/sanity/src/core/search/common/deriveSearchWeightsFromType.ts +++ b/packages/sanity/src/core/search/common/deriveSearchWeightsFromType.ts @@ -1,4 +1,5 @@ import {type CrossDatasetType, type SchemaType, type SearchConfiguration} from '@sanity/types' +import {toString as pathToString} from '@sanity/util/paths' import {isRecord} from '../../util' import {type SearchPath, type SearchSpec} from './types' @@ -19,7 +20,6 @@ const BASE_WEIGHTS: Record> = { _id: {weight: 1, type: 'string'}, _type: {weight: 1, type: 'string'}, } -const GROQ_RESERVED_KEYWORDS = ['true', 'false', 'null'] const builtInObjectTypes = ['reference', 'crossDatasetReference'] const getTypeChain = (type: SchemaType | undefined): SchemaType[] => @@ -39,22 +39,6 @@ function isSchemaType(input: SchemaType | CrossDatasetType | undefined): input i return typeof input !== 'undefined' && 'name' in input } -/** - * Serialize field path for GROQ query. - * - * Reserved keywords, such as `null`, cannot be accessed using dot notation. These fields will - * instead be serialized using square bracket notation. - */ -function pathToString(...path: string[]): string { - const cleanPath = path.filter(Boolean) - return cleanPath.slice(1).reduce((nextPath, segment) => { - if (GROQ_RESERVED_KEYWORDS.includes(segment)) { - return `${nextPath}['${segment}']` - } - return `${nextPath}.${segment}` - }, cleanPath[0]) -} - function getLeafWeights( schemaType: SchemaType | CrossDatasetType | undefined, maxDepth: number, @@ -84,7 +68,7 @@ function getLeafWeights( ) for (const objectType of objectTypes) { for (const field of objectType.fields) { - const nextPath = pathToString(path, field.name) + const nextPath = pathToString([path, field.name].filter(Boolean)) results.push(...traverse(field.type, nextPath, depth + 1)) } }