diff --git a/__tests__/objectSorter.spec.ts b/__tests__/objectSorter.spec.ts index 9729b92..421d367 100644 --- a/__tests__/objectSorter.spec.ts +++ b/__tests__/objectSorter.spec.ts @@ -93,5 +93,13 @@ describe('Sorter', () => { const hash = hasher(); expect(hash.sort(Object.create(null))).toBe('<:unknonw>:unknown'); }); + test('unknown with data', () => { + const hash = hasher(); + + const obj = Object.create(null) as Record; + obj['a'] = 1; + + expect(hash.sort(obj)).toBe('<:unknonw>:{"a":1}'); + }); }); }); diff --git a/src/objectSorter.ts b/src/objectSorter.ts index de587b8..8ddf1ea 100644 --- a/src/objectSorter.ts +++ b/src/objectSorter.ts @@ -226,7 +226,14 @@ export const objectSorter = (options?: SorterOptions): StringifyFn => { unknown: function _unknown(obj: Object) { // `unknonw` - is a typo, saved for backward compatibility const constructorName: string = obj.constructor?.name ?? 'unknonw'; - const objectName = typeof obj.toString === 'function' ? obj.toString() : 'unknown'; + + let objectName = 'unknown'; + + if (typeof obj.toString === 'function') { + objectName = obj.toString(); + } else if (Object.keys(obj).length > 0) { + objectName = JSON.stringify(obj); + } return `<:${constructorName}>:${objectName}`; },