-
Notifications
You must be signed in to change notification settings - Fork 390
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(base): migrate search utils to typescript
- Loading branch information
Showing
4 changed files
with
42 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters