sanity
Version:
Sanity is a real-time content infrastructure with a scalable, hosted backend featuring a Graph Oriented Query Language (GROQ), asset pipelines and fast edge caches
39 lines (31 loc) • 914 B
text/typescript
import {arrayToJSONMatchPath} from '@sanity/mutator'
import {type Path, type PathSegment} from '@sanity/types'
const IS_NUMERIC = /^\d+$/
function unquote(str: string) {
return str.replace(/^['"]/, '').replace(/['"]$/, '')
}
function splitAttr(segment: string) {
const [attr, key] = segment.split('==')
return {[attr]: unquote(key)}
}
function coerce(segment: string): PathSegment {
return IS_NUMERIC.test(segment) ? Number(segment) : segment
}
function parseGradientPath(focusPathStr: string): Path {
return focusPathStr
.split(/[[.\]]/g)
.filter(Boolean)
.map((seg) => (seg.includes('==') ? splitAttr(seg) : coerce(seg))) as Path
}
/**
* @internal
*/
export function encodePath(formBuilderPath: Path): string {
return arrayToJSONMatchPath(formBuilderPath)
}
/**
* @internal
*/
export function decodePath(gradientPath: string): Path {
return parseGradientPath(gradientPath)
}