# All files / utils identityMerge.ts

79.41% Statements 54/68
55% Branches 22/40
83.33% Functions 5/6
77.42% Lines 48/62

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
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160

    1x

    3x
    3x
    3x
    3x

    124x
    124x
    124x

    124x

    124x

    3x

    1x

    130x
    130x

    127x
    104x

    124x
    124x

    124x
    124x

    23x

    1x
    248x

    248x
    248x
    248x

    248x
    467x
    127x

    340x
    340x

    1x
    124x

    124x

    124x
    3x

    124x
    124x

    124x
    219x

    219x

    219x
    219x

    124x
    124x

    1x
    124x
    124x

     /*
      Given a source and target object, produce an object that is logically
      equivalent to the target. The produced object will preserve the reference
      identity of nodes in the source object whenever the path of the node matches
      the path of a logically equivalent node in the target.

      This helper enables memoization of functions whose inputs often change their
      reference identity, but rarely change their logical identity.

      More about logical and reference identity: a primitive object is one of the
      following:

      - number
      - string
      - boolean
      - function
      - `undefined`
      - `null`

      If `a` and `b` are primitive objects, then `a` and `b` are logically
      equivalent iff `a === b`. Two non-primitive objects are logically equivalent
      iff they have logically equivalent children.

      For example, let

      ```
      const foo = {a: [4, 5, {b: 'six'}]}
      const bar = {a: [4, 5, {b: 'six'}]}
      const baz = {a: [4, 5]}
      ```

      Then `foo` and `bar` are logically equivalent even though `foo !== bar` (i.e.
      `foo` and `bar` do not have the same reference identity).  The objects `foo`
      and `baz` are neither logically nor referentially identical.

    */
    export const identityMerge = <S extends object, T extends object>(
      source: S,
      target: T
    ): T => {
      const wrappedSource = {root: source}
      const wrappedTarget = {root: target}
      const paths = enumeratePaths(wrappedTarget)
      const result: any = {}

      for (const path of paths) {
        const sourceValue = getByPath(wrappedSource, path)
        const targetValue = getByPath(wrappedTarget, path)

        Iif (isEqual(sourceValue, targetValue)) {
          setByPath(result, path, sourceValue)
        } else {
          setByPath(result, path, targetValue)
        }
      }

      return result.root
    }

    type Path = string[]

    export const enumeratePaths = (
      target: any,
      pathToTarget: Path = [],
      acc: Path[] = []
    ): Path[] => {
      if (target === undefined || target === null || typeof target !== 'object') {
        return acc
      }

      for (const key of Object.keys(target)) {
        const currentPath = [...pathToTarget, key]

        acc.push(currentPath)
        enumeratePaths(target[key], currentPath, acc)
      }

      return acc
    }

    export const getByPath = (target: any, path: Path): any => {
      Iif (!path.length) {
        return
      }

      try {
        let i = 0
        let currentTarget = target[path[i]]

        while (currentTarget !== undefined) {
          if (i === path.length - 1) {
            return currentTarget
          }

          i += 1
          currentTarget = currentTarget[path[i]]
        }
      } catch {}
    }

    export const setByPath = (target: any, path: Path, value: any): void => {
      Iif (!path.length) {
        throw new Error('invalid path')
      }

      Iif (typeof target !== 'object') {
        throw new Error('target of setByPath must be object')
      }

      if (path.length === 1) {
        target[path[0]] = value
      }

      let i = -1
      let currentTarget = target

      while (i < path.length - 2) {
        let nextTarget = currentTarget[path[i + 1]]

        Iif (typeof nextTarget !== 'object') {
          const isArrayKey = isNaN(+path[i + 1])

          currentTarget[path[i + 1]] = isArrayKey ? {} : []
          nextTarget = currentTarget[path[i + 1]]
        }

        i += 1
        currentTarget = nextTarget
      }

      // beware writing into the currentTarget being part of the input Config passed to
      // SizedPlot, Object.freeze might have been applied
      const oldValue = currentTarget[path[i + 1]]
      Iif (oldValue !== value) {
        currentTarget[path[i + 1]] = value
      }
    }

    export const isEqual = (a: any, b: any): boolean => {
      Eif (a === null || a === undefined || b === null || b === undefined) {
        return a === b
      }

      switch (typeof b) {
        case 'number':
        case 'boolean':
        case 'string':
        case 'function':
          return a === b
        default: {
          const aKeys = Object.keys(a)
          const bKeys = Object.keys(b)

          return (
            aKeys.length === bKeys.length && aKeys.every(k => isEqual(a[k], b[k]))
          )
        }
      }
    }