All files / src json-modify.ts

92.85% Statements 13/14
100% Branches 1/1
100% Functions 2/2
92.3% Lines 12/13

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 25 26 27 28 29 30 31 32 33 34 35 3622x 22x 22x 22x       22x 3x 3x 6x 2x 2x                       4x                   22x  
import auto from './auto.js'
import jsonPath from './json-path.js'
import trueValue from './true-value.js'
import tsql from './template.js'
import type S from './sanitised.js'
 
export const jsonModify =
  (target: S, kvs: Record<string, unknown>): S => {
    const paths = Object.keys(kvs)
    return paths.reduce((result, path) => {
      if (path.startsWith('force ')) {
        const path_ = path.slice('force '.length)
        return tsql`
          json_modify(
            json_modify(
              ${result},
              ${'lax ' + jsonPath(path_)},
              ${trueValue}
            ),
            ${'strict ' + jsonPath(path_)},
            ${auto(kvs[path])}
          )
        `
      }
      return tsql`
        json_modify(
          ${result},
          ${jsonPath(path)},
          ${auto(kvs[path])}
        )
      `
    }, target)
  }
 
export default jsonModify