ts-data-forge
    Preparing search index...

    Function shallowEq

    • Performs a shallow equality check on two records using a configurable equality function. Verifies that both records have the same number of entries and that for every key in the first record, the corresponding value passes the equality test with the value in the second record.

      Parameters

      • a: UnknownRecord

        The first record to compare

      • b: UnknownRecord

        The second record to compare

      • eq: (x: unknown, y: unknown) => boolean = Object.is

        Optional equality function (defaults to Object.is for strict equality)

      Returns boolean

      true if the records are shallowly equal according to the equality function, false otherwise

      const obj1 = { name: 'Alice', age: 30 };

      const obj2 = { name: 'Alice', age: 30 };

      const obj3 = { name: 'Alice', age: 31 };

      assert.isTrue(Obj.shallowEq(obj1, obj2));

      assert.isFalse(Obj.shallowEq(obj1, obj3));

      // Custom equality function
      const obj4 = { value: 1 };

      const obj5 = { value: 1.00001 };

      const closeEnough = (a: unknown, b: unknown): boolean => {
      if (typeof a === 'number' && typeof b === 'number') {
      return Math.abs(a - b) < 0.001;
      }

      return Object.is(a, b);
      };

      assert.isTrue(Obj.shallowEq(obj4, obj5, closeEnough));