All files / lib/dynamic-form dynamic-form-helpers.ts

100% Statements 23/23
92.3% Branches 12/13
100% Functions 7/7
100% Lines 21/21

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 3318x   1x 43x 2x   41x 41x 41x 56x 56x 56x       1x 2x 2x 4x 4x 1x   3x   4x       3x 3x 3x    
export const cloneObject = <T>(obj: T): T => JSON.parse(JSON.stringify(obj)) as T;
 
export const mergeObject = <T1, T2>(obj1: T1, obj2: T2): T2 => {
  if (Array.isArray(obj2)) {
    return mergeArray(obj1 as any, obj2 as any) as T2;
  }
  const obj = obj1 ? { ...obj1 } : {};
  const keys = Object.keys(obj2);
  return keys.reduce((result, key) => {
    const value = typeof obj2[key] === 'object' ? mergeObject(result[key], obj2[key]) : obj2[key];
    result[key] = value;
    return result;
  }, obj as { [key: string]: any } as T2);
};
 
export const mergeArray = <T1 extends [], T2 extends []>(array1: T1, array2: T2): T2 => {
  const array = array1 ? [ ...array1 ] : [];
  return array2.reduce((result, item, index) => {
    const value = typeof item === 'object' ? mergeObject(result[index], item) : item;
    if (index < result.length) {
      result[index] = value;
    } else {
      result.push(value);
    }
    return result;
  }, array as any as T2);
};
 
export const extendObject = <T>(obj: T, paths: string[]): T => paths.reduce((result, path) => {
  result[path] = result[path] || {};
  return result[path];
}, obj) as T;