All files / src assign.ts

93.33% Statements 14/15
100% Branches 3/3
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 25 26 27 28 2922x 22x 22x 22x                     31x 9x   22x 15x   7x 6x   1x 1x     31x  
import id from './identifier.js'
import jsonPath from './json-path.js'
import S from './sanitised.js'
import tsql from './template.js'
import type Sid from './sanitised-identifier.js'
 
/**
 * @returns assigment operator LHS = RHS.
 *
 * `undefined` RHS is propagated.
 *
 * `null` RHS is left as is LHS = null.
 */
export function assign(lvalue: Sid | string, rvalue: unknown): undefined | S {
  if (typeof rvalue === 'undefined') {
    return undefined
  }
  if (lvalue instanceof S) {
    return tsql`${lvalue} = ${rvalue}`
  }
  if (lvalue.indexOf('->') === -1) {
    return tsql`${id(lvalue)} = ${rvalue}`
  }
  const [ llvalue, lrvalue ] = lvalue.split('->')
  return tsql`${id(llvalue)} = json_modify(${id(llvalue)}, ${jsonPath(lrvalue)}, ${rvalue})`
}
 
export default assign