All files / src json-path.ts

87.5% Statements 14/16
100% Branches 5/5
100% Functions 1/1
92.85% Lines 13/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24  22x       22x 20x 5x   15x 15x 2x   15x 1x   15x 2x   13x     22x  
 
export const re = /^\s*((append|lax|strict)\s+)*\$\./i
 
/** @returns `[append] [lax | strict] $.<path>` json path from simplified path. */
export const jsonPath =
  (path: string, { mode, append }: { append?: boolean, mode?: 'lax' | 'strict' } = {}): string => {
    if (re.test(path)) {
      return path
    }
    const parts: string[] = []
    if (append) {
      parts.push('append')
    }
    if (mode) {
      parts.push(mode)
    }
    if (parts.length > 0) {
      return `${parts.join(' ')} $.${path}`
    }
    return `$.${path}`
  }
 
export default jsonPath