Skip to content

Commit

Permalink
fix(common): flat eq now returns false instead of comparing tostring …
Browse files Browse the repository at this point in the history
…to prevent false positives
  • Loading branch information
vitoke committed Jul 3, 2021
1 parent 339cbea commit b045177
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
7 changes: 4 additions & 3 deletions deno_dist/common/eq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ export namespace Eq {
return _objectEq(v1, v2);
}

return _anyToStringEq(v1, v2);
// cannot establish that they are equal in flat mode
return false;
}
}
};
Expand All @@ -213,7 +214,7 @@ export namespace Eq {

/**
* Returns an Eq instance that checks equality of any values. For composed values (objects and iterables)
* it will compare the JSON.stringify results of the values.
* it will compare with Object.is.
* @typeparam T - the value type
* @example
* const eq = anyFlatEq()
Expand All @@ -229,7 +230,7 @@ export namespace Eq {
/**
* Returns an Eq instance that checks equality of any values. For composed values (objects and iterables)
* it will enter 1 level, and if again compound values are found, they are compared
* using JSON.stringify.
* with Object.is.
* @typeparam T - the value type
* @example
* const eq = anyFlatEq()
Expand Down
7 changes: 4 additions & 3 deletions packages/common/src/eq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ export namespace Eq {
return _objectEq(v1, v2);
}

return _anyToStringEq(v1, v2);
// cannot establish that they are equal in flat mode
return false;
}
}
};
Expand All @@ -213,7 +214,7 @@ export namespace Eq {

/**
* Returns an Eq instance that checks equality of any values. For composed values (objects and iterables)
* it will compare the JSON.stringify results of the values.
* it will compare with Object.is.
* @typeparam T - the value type
* @example
* const eq = anyFlatEq()
Expand All @@ -229,7 +230,7 @@ export namespace Eq {
/**
* Returns an Eq instance that checks equality of any values. For composed values (objects and iterables)
* it will enter 1 level, and if again compound values are found, they are compared
* using JSON.stringify.
* with Object.is.
* @typeparam T - the value type
* @example
* const eq = anyFlatEq()
Expand Down
17 changes: 13 additions & 4 deletions packages/common/test/eq.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,17 @@ describe('Eq', () => {
).toBe(false);
expect(e(Object.is, Object.is)).toBe(true);
expect(e({}, new (class Object {})())).toBe(false);
expect(e({}, {})).toBe(true);
// flat does not compare object contents, to be safe returns false
expect(e({}, {})).toBe(false);
// not same constructor
expect(e(new (class Object {})(), new (class Object {})())).toBe(false);

class O {
constructor(readonly v: number) {}
}

// same constructor
expect(e(new O(1), new O(1))).toBe(true);
// same constructor but flat does not compare contents
expect(e(new O(1), new O(1))).toBe(false);
// because toString gives [object object], this will be true
// expect(e(new O(1), new O(2))).toBe(false);
});
Expand All @@ -82,14 +83,22 @@ describe('Eq', () => {
expect(e([1], [1, 3])).toBe(false);
expect(e({}, { a: 1 })).toBe(false);
expect(e({ a: 1 }, { a: 1 })).toBe(true);
expect(e({ a: 1, b: { c: 4 } }, { a: 1, b: { c: 4 } })).toBe(true);
// shallow only checks one level deep, to be safe will return false for deeper nested objects
expect(e({ a: 1, b: { c: 4 } }, { a: 1, b: { c: 4 } })).toBe(false);
expect(e({ a: 1, b: { c: 4 } }, { a: 1, b: { c: 5 } })).toBe(false);
expect(e(1, 2)).toBe(false);
expect(e(2, 2)).toBe(true);
expect(e(undefined, undefined)).toBe(true);
expect(e('a', 'a')).toBe(true);
expect(e('a', 'b')).toBe(false);
expect(e(Symbol('a'), Symbol('a'))).toBe(false);

class O {
constructor(readonly v: number) {}
}

// same constructor
expect(e(new O(1), new O(1))).toBe(true);
});

it('anyDeepEq', () => {
Expand Down

0 comments on commit b045177

Please sign in to comment.