Skip to content

Commit bc05f1a

Browse files
committed
refactor(utils): fix getOrderIndependentHash()
1 parent ab8b347 commit bc05f1a

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/__tests__/utils-test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,19 +106,25 @@ describe('utils', () => {
106106

107107
it('hashThisString()', () => {
108108
const hash1 = hashThisString('a');
109+
const hashNull = hashThisString('[ type: null ; value: ""]');
110+
const empty = hashThisString('');
109111
const hash2 = hashThisString('abc');
110112
const hash3 = hashThisString('abcsydghcsdagcyasjdcsdvcgsavdgcvsagdcbjhsdbbc');
111113

112114
expect(hash1).toBe(97);
115+
expect(empty).toBe(0);
116+
expect(hashNull).toBe(1593002687);
113117
expect(hash2).toBe(96354);
114118
expect(hash3).toBe(25191889);
115119
});
116120

117121
it('getOrderIndependentHash()', () => {
118122
const array = getOrderIndependentHash(['a', 'b']);
123+
const ifNull = getOrderIndependentHash(null);
119124
const obj = getOrderIndependentHash({ a: 'a', b: 'b' });
120125
const string = getOrderIndependentHash('ab');
121126

127+
expect(ifNull).toBe(1933772593);
122128
expect(array).toBe(976844698);
123129
expect(obj).toBe(-2385456289);
124130
expect(string).toBe(35072500);

src/utils.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export const realTypeOf = (obj: any): string => {
6565
if (Object.prototype.toString.call(obj) === '[object Date]') {
6666
return 'date';
6767
}
68-
if (typeof obj.toString === 'function' && /^\/.*\//.test(obj.toString())) {
68+
if (typeof obj?.toString === 'function' && /^\/.*\//.test(obj.toString())) {
6969
return 'regexp';
7070
}
7171
return 'object';
@@ -110,7 +110,8 @@ export const getOrderIndependentHash = (obj: any): number => {
110110
});
111111
return accum;
112112
}
113-
const stringToHash = `[ type: ${type} ; value: ${obj.toString()}]`;
113+
114+
const stringToHash = `[ type: ${type} ; value: ${obj ? obj.toString() : 'hash'}]`;
114115
return accum + hashThisString(stringToHash);
115116
};
116117

0 commit comments

Comments
 (0)