All files / util/src/json-equal/deepEqual v6.ts

97.77% Statements 44/45
93.75% Branches 30/32
100% Functions 1/1
100% Lines 30/30

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 452x 2x   2x   6868x   1366x 1366x     1366x 532x 528x 528x 1708x 518x       834x 799x 799x 9x 9x 8x 8x 8x 6x     790x 790x 790x 787x 786x 4774x 4774x   772x     35x    
const isArray = Array.isArray;
const OBJ_PROTO = Object.prototype;
 
export const deepEqual = (a: unknown, b: unknown): boolean => {
  // Primitives
  if (a === b) return true;
 
  let length: number = 0,
    i: number = 0;
 
  // Arrays
  if (isArray(a)) {
    if (!isArray(b)) return false;
    length = a.length;
    if (length !== (b as Array<unknown>).length) return false;
    for (i = length; i-- !== 0; ) if (!deepEqual(a[i], (b as Array<unknown>)[i])) return false;
    return true;
  }
 
  // Objects
  if (a && b && typeof a === 'object' && typeof b === 'object') {
    specific: {
      if ((<any>a).__proto__ === OBJ_PROTO) break specific;
      Eif (a instanceof Uint8Array) {
        if (!(b instanceof Uint8Array)) return false;
        const length = a.length;
        if (length !== b.length) return false;
        for (let i = 0; i < length; i++) Iif (a[i] !== b[i]) return false;
        return true;
      }
    }
    const keys = Object.keys(a);
    length = keys.length;
    if (length !== Object.keys(b).length) return false;
    if (isArray(b)) return false;
    for (i = length; i-- !== 0; ) {
      const key = keys[i];
      if (!deepEqual((a as Record<string, unknown>)[key], (b as Record<string, unknown>)[key])) return false;
    }
    return true;
  }
 
  return false;
};