All files object.ts

100% Statements 40/40
100% Branches 20/20
100% Functions 5/5
100% Lines 40/40

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 411x 17x 17x 1x 1x 7x 4x 7x 4x 6x 6x 2x 2x 6x 4x 4x 6x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x  
export function isObject(value: any): value is Record<string, any> {
  return value !== null && typeof value === "object" && !Array.isArray(value);
}
 
export function mergeDeep<T>(target: T, ...sources: any[]): T {
  if (!sources.length) return target;
  const source = sources.shift();
  if (isObject(target) && isObject(source)) {
    for (const key in source) {
      const srcVal = source[key];
      if (isObject(srcVal)) {
        if (!target[key]) Object.assign(target, { [key]: {} });
        mergeDeep(target[key], srcVal);
      } else {
        Object.assign(target, { [key]: srcVal });
      }
    }
  }
  return mergeDeep(target, ...sources);
}
 
export function pick<T, K extends keyof T>(obj: T, keys: K[]): Pick<T, K> {
  const res = {} as Pick<T, K>;
  keys.forEach(k => { if (k in obj) res[k] = obj[k]; });
  return res;
}
 
export function omit<T, K extends keyof T>(obj: T, keys: K[]): Omit<T, K> {
  const res = { ...obj } as Omit<T, K>;
  keys.forEach(k => { delete res[k]; });
  return res;
}
 
export function get<T, K extends string>(
  obj: T,
  path: K,
  defaultVal?: any
): any {
  return path.split(".").reduce((o, k) => (o && k in o ? o[k] : undefined), obj) ?? defaultVal;
}