Skip to content

Instantly share code, notes, and snippets.

@mshahin364
Forked from jsdevtom/deep-equals.ts
Created October 6, 2024 13:13
Show Gist options
  • Save mshahin364/c5652a084aa79a62b0db89319f7abe05 to your computer and use it in GitHub Desktop.
Save mshahin364/c5652a084aa79a62b0db89319f7abe05 to your computer and use it in GitHub Desktop.

Revisions

  1. @jsdevtom jsdevtom created this gist May 18, 2019.
    57 changes: 57 additions & 0 deletions deep-equals.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,57 @@
    const isArray = Array.isArray;
    const keyList = Object.keys;
    const hasProp = Object.prototype.hasOwnProperty;

    export function isDeeplyEqual<T extends any>(a: T, b: T): boolean {
    if (a === b) { return true; }


    if (a && b && typeof a === 'object' && typeof b === 'object') {
    const arrA = isArray(a)
    , arrB = isArray(b);
    let i
    , length
    , key;

    if (arrA && arrB) {
    length = a.length;
    if (length !== b.length) { return false; }
    for (i = length; i-- !== 0;) {
    if (!isDeeplyEqual(a[i], b[i])) { return false;
    } }
    return true;
    }

    if (arrA !== arrB) { return false; }

    const dateA = a instanceof Date
    , dateB = b instanceof Date;
    if (dateA !== dateB) { return false; }
    if (dateA && dateB) { return a.getTime() === b.getTime(); }

    const regexpA = a instanceof RegExp
    , regexpB = b instanceof RegExp;
    if (regexpA !== regexpB) { return false; }
    if (regexpA && regexpB) { return a.toString() === b.toString(); }

    const keys = keyList(a);
    length = keys.length;

    if (length !== keyList(b).length) {
    return false;
    }

    for (i = length; i-- !== 0;) {
    if (!hasProp.call(b, keys[i])) { return false;
    } }

    for (i = length; i-- !== 0;) {
    key = keys[i];
    if (!isDeeplyEqual(a[key], b[key])) { return false; }
    }

    return true;
    }

    return a !== a && b !== b;
    }