Skip to content

Commit 31d940f

Browse files
authored
add check into the utility isEqual for comparision of functions by reference (#63)
1 parent bfda7c4 commit 31d940f

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/is/__tests__/equal.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,16 @@ describe('utils/is/equal', () => {
2929
});
3030

3131
it('equal objects', () => {
32+
const obj = {
33+
f: () => {}
34+
};
35+
const obj2 = {
36+
f: () => {}
37+
}
3238
expect(isEqual({}, {})).toBe(true);
39+
expect(isEqual(obj, obj)).toBe(true);
40+
expect(isEqual(obj, obj2)).toBe(false);
41+
expect(isEqual({ f: () => {} }, { f: () => {} })).toBe(false);
3342
expect(isEqual({ a: 1 }, { a: 1 })).toBe(true);
3443
expect(isEqual({ a: 1 }, { a: 2 })).toBe(false);
3544
expect(isEqual({ a: 1, b: [1, 2, { c: 3 }] }, { a: 1, b: [1, 2, { c: 3 }] })).toBe(true);
@@ -45,4 +54,14 @@ describe('utils/is/equal', () => {
4554
expect(isEqual({}, 5)).toBe(false);
4655
expect(isEqual(3, 'test')).toBe(false);
4756
});
57+
58+
it('equal function', () => {
59+
const f = () => {};
60+
const f2 = () => {};
61+
62+
expect(isEqual(f, f)).toBe(true);
63+
expect(isEqual(f, f2)).toBe(false);
64+
expect(isEqual(() => {}, () => {})).toBe(false);
65+
expect(isEqual(() => {}, function () {})).toBe(false);
66+
});
4867
});

src/is/equal.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ const isEqual = (test1, test2): boolean => {
7070
return isEqualObjects(test1, test2);
7171
}
7272

73+
if (typeof test1 === 'function') {
74+
return test1 === test2;
75+
}
76+
7377
return isEqualNativeTypes(test1, test2);
7478
};
7579

0 commit comments

Comments
 (0)