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
22 lines (17 loc) • 594 B
text/typescript
import {isKeyedObject, type Path} from '@sanity/types'
export function pathToString(path: Path | undefined = []): string {
return path.reduce<string>((target, segment, i) => {
const segmentType = typeof segment
if (segmentType === 'number') {
return `${target}[${segment}]`
}
if (segmentType === 'string') {
const separator = i === 0 ? '' : '.'
return `${target}${separator}${segment}`
}
if (isKeyedObject(segment)) {
return `${target}[_key=="${segment._key}"]`
}
throw new Error(`Unsupported path segment "${segment}"`)
}, '')
}