All files / src/utils object-change.ts

87.5% Statements 77/88
85.42% Branches 41/48
92.31% Functions 12/13
91.25% Lines 73/80

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                                    38x 138x     38x 38x       13x     38x 200x 200x 200x 404x 404x 2x 2x 2x     402x 402x 402x 402x 402x           200x     38x 131x       6x     38x 63x 133x 133x   63x     38x 73x 73x 73x 73x 73x 45x 45x 45x   28x   73x     38x 61x 61x 61x 61x 39x 39x 38x 38x   38x 2x   36x     1x     22x 1x   21x     61x     38x         15x 15x 15x     38x 4x 4x   4x 4x 9x 9x 9x     9x 3x   6x         4x     38x                  
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
export function shallowCopy(v: any): any {
  return Array.isArray(v) ? v.slice() : Object.assign({}, v);
}
 
export function isEmpty(v: any): boolean {
  return !(Array.isArray(v) ? v.length : Object.keys(v).length);
}
 
function isObjectOrArray(v: any): boolean {
  return Boolean(v && typeof v === 'object');
}
 
export function parsePath(path: string): string[] {
  const parts: string[] = [];
  let rest = path;
  while (rest) {
    const escapedMatch = rest.match(/^\{([^{}]*)\}(?:\.(.*))?$/);
    if (escapedMatch) {
      parts.push(escapedMatch[1]);
      rest = escapedMatch[2];
      continue;
    }
 
    const normalMatch = rest.match(/^([^.]*)(?:\.(.*))?$/);
    Eif (normalMatch) {
      parts.push(normalMatch[1]);
      rest = normalMatch[2];
      continue;
    }
 
    throw new Error(`Could not parse path ${path}`);
  }
 
  return parts;
}
 
export function makePath(parts: string[]): string {
  return parts.map(p => (p.includes('.') ? `{${p}}` : p)).join('.');
}
 
function isAppend(key: string): boolean {
  return key === '[append]' || key === '-1';
}
 
export function deepGet<T extends Record<string, any>>(value: T, path: string): any {
  const parts = parsePath(path);
  for (const part of parts) {
    value = (value || {})[part];
  }
  return value;
}
 
export function deepSet<T extends Record<string, any>>(value: T, path: string, x: any): T {
  const parts = parsePath(path);
  let myKey = parts.shift() as string; // Must be defined
  const valueCopy = shallowCopy(value);
  if (Array.isArray(valueCopy) && isAppend(myKey)) myKey = String(valueCopy.length);
  if (parts.length) {
    const nextKey = parts[0];
    const rest = makePath(parts);
    valueCopy[myKey] = deepSet(value[myKey] || (isAppend(nextKey) ? [] : {}), rest, x);
  } else {
    valueCopy[myKey] = x;
  }
  return valueCopy;
}
 
export function deepDelete<T extends Record<string, any>>(value: T, path: string): T {
  const valueCopy = shallowCopy(value);
  const parts = parsePath(path);
  const firstKey = parts.shift() as string; // Must be defined
  if (parts.length) {
    const firstKeyValue = value[firstKey];
    if (firstKeyValue) {
      const restPath = makePath(parts);
      const prunedFirstKeyValue = deepDelete(value[firstKey], restPath);
 
      if (isEmpty(prunedFirstKeyValue)) {
        delete valueCopy[firstKey];
      } else {
        valueCopy[firstKey] = prunedFirstKeyValue;
      }
    } else {
      delete valueCopy[firstKey];
    }
  } else {
    if (Array.isArray(valueCopy) && !isNaN(Number(firstKey))) {
      valueCopy.splice(Number(firstKey), 1);
    } else {
      delete valueCopy[firstKey];
    }
  }
  return valueCopy;
}
 
export function deepMove<T extends Record<string, any>>(
  value: T,
  fromPath: string,
  toPath: string,
): T {
  value = deepSet(value, toPath, deepGet(value, fromPath));
  value = deepDelete(value, fromPath);
  return value;
}
 
export function deepExtend<T extends Record<string, any>>(target: T, diff: Record<string, any>): T {
  Iif (typeof target !== 'object') throw new TypeError(`Invalid target`);
  Iif (typeof diff !== 'object') throw new TypeError(`Invalid diff`);
 
  const newValue = shallowCopy(target);
  for (const key in diff) {
    const targetValue = target[key];
    const diffValue = diff[key];
    Iif (typeof diffValue === 'undefined') {
      delete newValue[key];
    } else {
      if (isObjectOrArray(targetValue) && isObjectOrArray(diffValue)) {
        newValue[key] = deepExtend(targetValue, diffValue);
      } else {
        newValue[key] = diffValue;
      }
    }
  }
 
  return newValue;
}
 
export function whitelistKeys(obj: Record<string, any>, whitelist: string[]): Record<string, any> {
  const newObj: Record<string, any> = {};
  for (const w of whitelist) {
    if (Object.prototype.hasOwnProperty.call(obj, w)) {
      newObj[w] = obj[w];
    }
  }
  return newObj;
}