@@ -40,7 +40,7 @@ describe('Hash Object', () => {
4040
4141 it ( 'should allow custom replacers to modify the hash' , ( ) => {
4242 const object = { key : 'value' , anotherKey : 42 } ;
43- const replacer = ( key : string , value : unknown ) =>
43+ const replacer = ( key : string , value : unknown ) : unknown =>
4444 key === 'key' ? 'modifiedValue' : value ;
4545
4646 const hashWithReplacer = hashUtils . hashObject ( object , replacer ) ;
@@ -71,3 +71,74 @@ describe('Hash Object', () => {
7171 expect ( ( ) => hashUtils . hashObject ( object ) ) . toThrow ( ) ;
7272 } ) ;
7373} ) ;
74+
75+ describe ( 'Hash Deterministic Object' , ( ) => {
76+ it ( 'should return a consistent hash for the same object' , ( ) => {
77+ const object = { key : 'value' , anotherKey : 42 } ;
78+ const hash1 = hashUtils . hashDeterministicObject ( object ) ;
79+ const hash2 = hashUtils . hashDeterministicObject ( object ) ;
80+
81+ expect ( hash1 ) . toEqual ( hash2 ) ;
82+ expect ( hash1 ) . toEqual (
83+ 'bcfbe7147cc6988bf4987c322ca615a900ed0bce28c358e8404c1b5c14ac389f' ,
84+ ) ;
85+ } ) ;
86+
87+ it ( 'should return the same hash for objects with keys in different orders' , ( ) => {
88+ const object1 = { key : 'value' , anotherKey : 42 } ;
89+ const object2 = { anotherKey : 42 , key : 'value' } ;
90+
91+ const hash1 = hashUtils . hashDeterministicObject ( object1 ) ;
92+ const hash2 = hashUtils . hashDeterministicObject ( object2 ) ;
93+
94+ expect ( hash1 ) . toEqual ( hash2 ) ;
95+ expect ( hash1 ) . toEqual (
96+ 'bcfbe7147cc6988bf4987c322ca615a900ed0bce28c358e8404c1b5c14ac389f' ,
97+ ) ;
98+ } ) ;
99+
100+ it ( 'should return different hashes for different objects' , ( ) => {
101+ const object1 = { key : 'value1' } ;
102+ const object2 = { key : 'value2' } ;
103+
104+ const hash1 = hashUtils . hashDeterministicObject ( object1 ) ;
105+ const hash2 = hashUtils . hashDeterministicObject ( object2 ) ;
106+
107+ expect ( hash1 ) . not . toEqual ( hash2 ) ;
108+ expect ( hash1 ) . toEqual (
109+ 'dfada72ccc2244e8c7aef8f0dbe7c026a6553bc5bda3f7654f3d0b94dd51a23b' ,
110+ ) ;
111+ expect ( hash2 ) . toEqual (
112+ '711db6965d4867a7c0f6f20864ae49896b97ba3616a9aa53b536a773468f662e' ,
113+ ) ;
114+ } ) ;
115+
116+ it ( 'should handle nested objects correctly and deterministically' , ( ) => {
117+ const nestedObject1 = { key : { b : 2 , a : 1 } } ;
118+ const nestedObject2 = { key : { a : 1 , b : 2 } } ;
119+
120+ const hash1 = hashUtils . hashDeterministicObject ( nestedObject1 ) ;
121+ const hash2 = hashUtils . hashDeterministicObject ( nestedObject2 ) ;
122+
123+ expect ( hash1 ) . toEqual ( hash2 ) ;
124+ expect ( hash1 ) . toEqual (
125+ '44a71c92a8d07443b2539b0d82a137c3a620aa81be56de2f29a9e2fe45fefbc4' ,
126+ ) ;
127+ } ) ;
128+
129+ it ( 'should handle empty objects' , ( ) => {
130+ const object = { } ;
131+
132+ const hash = hashUtils . hashDeterministicObject ( object ) ;
133+
134+ expect ( hash ) . toEqual (
135+ '44136fa355b3678a1146ad16f7e8649e94fb4fc21fe77e8310c060f61caaff8a' ,
136+ ) ;
137+ } ) ;
138+
139+ it ( 'should handle edge case with undefined object' , ( ) => {
140+ const object = undefined as unknown as object ;
141+
142+ expect ( ( ) => hashUtils . hashDeterministicObject ( object ) ) . toThrow ( ) ;
143+ } ) ;
144+ } ) ;
0 commit comments