# All files / utils identityMerge.ts

97.06% Statements 66/68
92.5% Branches 37/40
100% Functions 6/6
96.77% Lines 60/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

    3x

    20x
    20x
    20x
    20x

    732x
    732x
    732x

    732x
    278x

    454x

    20x

    3x

    781x
    781x

    760x
    617x

    739x
    739x

    739x
    739x

    143x

    3x
    1472x

    1472x
    1472x
    1472x

    1470x
    3303x
    1061x

    2242x
    2242x

    3x
    736x
    1x

    735x

    735x
    20x

    735x
    735x

    735x
    1283x

    1282x
    1x

    1x
    1x

    1282x
    1282x

    734x
    734x
    7x

    3x
    1142x
    429x

    713x

    584x

    129x
    129x

    129x
    402x

     /*
      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)

        if (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 => {
      if (!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]]

        if (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]]
      if (oldValue !== value) {
        currentTarget[path[i + 1]] = value
      }
    }

    export const isEqual = (a: any, b: any): boolean => {
      if (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]))
          )
        }
      }
    }