Skip to content

Commit

Permalink
UDCSL #54 - Adding check to ObjectUtils to handle function equality
Browse files Browse the repository at this point in the history
  • Loading branch information
dleadbetter committed Aug 26, 2022
1 parent 1411ba2 commit bce4032
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
File renamed without changes.
5 changes: 5 additions & 0 deletions packages/shared/src/utils/Object.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ const isEqual = (a: any, b: any, userOptions: OptionsProps = {}) => {
}
}

// Test function equality using the toString method
if (_.isFunction(a) && _.isFunction(b) && a.toString() === b.toString()) {
return true;
}

if (a !== null && typeof a === 'object' && b !== null && typeof b === 'object') {
const aKeys = _.keys(a);
const bKeys = _.keys(b);
Expand Down
42 changes: 42 additions & 0 deletions packages/shared/test/utils/Object.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import ObjectUtils from '../../src/utils/Object';

describe('isEqual', () => {
test('should be equal for two null objects', () => {
expect(ObjectUtils.isEqual(null, null)).toBeTruthy();
});

test('should be equal for two strings of the same value', () => {
expect(ObjectUtils.isEqual('abc', 'abc')).toBeTruthy();
});

test('should be equal for the same object', () => {
const obj1 = {
id: 1,
name: 'Test',
children: [{
id: 1
}, {
id: 2
}]
};

const obj2 = {
id: 1,
name: 'Test',
children: [{
id: 1
}, {
id: 2
}]
};

expect(ObjectUtils.isEqual(obj1, obj2)).toBeTruthy();
});

test('should be equal for the same function', () => {
const func1 = () => 'test';
const func2 = () => 'test';

expect(ObjectUtils.isEqual(func1, func2)).toBeTruthy();
});
});

0 comments on commit bce4032

Please sign in to comment.