@hyperlane-xyz/utils 28.1.0 → 29.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (116) hide show
  1. package/dist/addresses.d.ts +1 -1
  2. package/dist/addresses.d.ts.map +1 -1
  3. package/dist/addresses.js +5 -7
  4. package/dist/addresses.js.map +1 -1
  5. package/dist/validator.d.ts.map +1 -1
  6. package/dist/validator.js +7 -4
  7. package/dist/validator.js.map +1 -1
  8. package/package.json +7 -5
  9. package/dist/addresses.test.d.ts +0 -2
  10. package/dist/addresses.test.d.ts.map +0 -1
  11. package/dist/addresses.test.js +0 -124
  12. package/dist/addresses.test.js.map +0 -1
  13. package/dist/amount.test.d.ts +0 -2
  14. package/dist/amount.test.d.ts.map +0 -1
  15. package/dist/amount.test.js +0 -113
  16. package/dist/amount.test.js.map +0 -1
  17. package/dist/arrays.test.d.ts +0 -2
  18. package/dist/arrays.test.d.ts.map +0 -1
  19. package/dist/arrays.test.js +0 -77
  20. package/dist/arrays.test.js.map +0 -1
  21. package/dist/async.test.d.ts +0 -2
  22. package/dist/async.test.d.ts.map +0 -1
  23. package/dist/async.test.js +0 -335
  24. package/dist/async.test.js.map +0 -1
  25. package/dist/base58.test.d.ts +0 -2
  26. package/dist/base58.test.d.ts.map +0 -1
  27. package/dist/base58.test.js +0 -31
  28. package/dist/base58.test.js.map +0 -1
  29. package/dist/base64.test.d.ts +0 -2
  30. package/dist/base64.test.d.ts.map +0 -1
  31. package/dist/base64.test.js +0 -54
  32. package/dist/base64.test.js.map +0 -1
  33. package/dist/big-numbers.test.d.ts +0 -2
  34. package/dist/big-numbers.test.d.ts.map +0 -1
  35. package/dist/big-numbers.test.js +0 -96
  36. package/dist/big-numbers.test.js.map +0 -1
  37. package/dist/checkpoints.test.d.ts +0 -2
  38. package/dist/checkpoints.test.d.ts.map +0 -1
  39. package/dist/checkpoints.test.js +0 -105
  40. package/dist/checkpoints.test.js.map +0 -1
  41. package/dist/env.test.d.ts +0 -2
  42. package/dist/env.test.d.ts.map +0 -1
  43. package/dist/env.test.js +0 -16
  44. package/dist/env.test.js.map +0 -1
  45. package/dist/eslint-rules/no-restricted-imports-from-exports.test.d.ts +0 -2
  46. package/dist/eslint-rules/no-restricted-imports-from-exports.test.d.ts.map +0 -1
  47. package/dist/eslint-rules/no-restricted-imports-from-exports.test.js +0 -259
  48. package/dist/eslint-rules/no-restricted-imports-from-exports.test.js.map +0 -1
  49. package/dist/eslint-rules/sort-yaml-arrays.test.d.ts +0 -2
  50. package/dist/eslint-rules/sort-yaml-arrays.test.d.ts.map +0 -1
  51. package/dist/eslint-rules/sort-yaml-arrays.test.js +0 -629
  52. package/dist/eslint-rules/sort-yaml-arrays.test.js.map +0 -1
  53. package/dist/fs/format.test.d.ts +0 -2
  54. package/dist/fs/format.test.d.ts.map +0 -1
  55. package/dist/fs/format.test.js +0 -153
  56. package/dist/fs/format.test.js.map +0 -1
  57. package/dist/fs/json.test.d.ts +0 -2
  58. package/dist/fs/json.test.d.ts.map +0 -1
  59. package/dist/fs/json.test.js +0 -148
  60. package/dist/fs/json.test.js.map +0 -1
  61. package/dist/fs/utils.test.d.ts +0 -2
  62. package/dist/fs/utils.test.d.ts.map +0 -1
  63. package/dist/fs/utils.test.js +0 -120
  64. package/dist/fs/utils.test.js.map +0 -1
  65. package/dist/fs/yaml.test.d.ts +0 -2
  66. package/dist/fs/yaml.test.d.ts.map +0 -1
  67. package/dist/fs/yaml.test.js +0 -134
  68. package/dist/fs/yaml.test.js.map +0 -1
  69. package/dist/ids.test.d.ts +0 -2
  70. package/dist/ids.test.d.ts.map +0 -1
  71. package/dist/ids.test.js +0 -39
  72. package/dist/ids.test.js.map +0 -1
  73. package/dist/lazy.test.d.ts +0 -2
  74. package/dist/lazy.test.d.ts.map +0 -1
  75. package/dist/lazy.test.js +0 -129
  76. package/dist/lazy.test.js.map +0 -1
  77. package/dist/logging.test.d.ts +0 -2
  78. package/dist/logging.test.d.ts.map +0 -1
  79. package/dist/logging.test.js +0 -35
  80. package/dist/logging.test.js.map +0 -1
  81. package/dist/math.test.d.ts +0 -2
  82. package/dist/math.test.d.ts.map +0 -1
  83. package/dist/math.test.js +0 -45
  84. package/dist/math.test.js.map +0 -1
  85. package/dist/objects.test.d.ts +0 -2
  86. package/dist/objects.test.d.ts.map +0 -1
  87. package/dist/objects.test.js +0 -655
  88. package/dist/objects.test.js.map +0 -1
  89. package/dist/rpc.test.d.ts +0 -2
  90. package/dist/rpc.test.d.ts.map +0 -1
  91. package/dist/rpc.test.js +0 -49
  92. package/dist/rpc.test.js.map +0 -1
  93. package/dist/sets.test.d.ts +0 -2
  94. package/dist/sets.test.d.ts.map +0 -1
  95. package/dist/sets.test.js +0 -35
  96. package/dist/sets.test.js.map +0 -1
  97. package/dist/strings.test.d.ts +0 -2
  98. package/dist/strings.test.d.ts.map +0 -1
  99. package/dist/strings.test.js +0 -42
  100. package/dist/strings.test.js.map +0 -1
  101. package/dist/typeof.test.d.ts +0 -2
  102. package/dist/typeof.test.d.ts.map +0 -1
  103. package/dist/typeof.test.js +0 -34
  104. package/dist/typeof.test.js.map +0 -1
  105. package/dist/url.test.d.ts +0 -2
  106. package/dist/url.test.d.ts.map +0 -1
  107. package/dist/url.test.js +0 -29
  108. package/dist/url.test.js.map +0 -1
  109. package/dist/validation.test.d.ts +0 -2
  110. package/dist/validation.test.d.ts.map +0 -1
  111. package/dist/validation.test.js +0 -11
  112. package/dist/validation.test.js.map +0 -1
  113. package/dist/yaml.test.d.ts +0 -2
  114. package/dist/yaml.test.d.ts.map +0 -1
  115. package/dist/yaml.test.js +0 -519
  116. package/dist/yaml.test.js.map +0 -1
@@ -1,655 +0,0 @@
1
- import { expect } from 'chai';
2
- import { arrayToObject, deepCopy, deepEquals, deepFind, diffObjMerge, invertKeysAndValues, isObjEmpty, isObject, keepOnlyDiffObjects, mustGet, objDiff, objFilter, objKeys, objLength, objMap, objMapEntries, objMerge, objOmit, pick, promiseObjAll, sortArraysInObject, stringifyObject, transformObj, } from './objects.js';
3
- describe('Object utilities', () => {
4
- it('deepEquals', () => {
5
- expect(deepEquals({ a: 1, b: 2 }, { a: 1, b: 2 })).to.be.true;
6
- expect(deepEquals({ a: 1, b: 2 }, { a: 1, b: 2, c: 3 })).to.be.false;
7
- expect(deepEquals({ a: 1, b: 2 }, { a: 1, b: 4 })).to.be.false;
8
- });
9
- it('deepCopy', () => {
10
- expect(deepCopy({ a: 1, b: 2 })).to.eql({ a: 1, b: 2 });
11
- expect(deepCopy({ a: 1, b: 2 })).to.not.eql({ a: 1, b: 3 });
12
- });
13
- it('objMerge', () => {
14
- const obj1 = { a: 1, b: 2, c: { d: '4' } };
15
- const obj2 = { b: 3, c: { d: '5' } };
16
- const merged = objMerge(obj1, obj2);
17
- expect(merged).to.eql({ a: 1, b: 3, c: { d: '5' } });
18
- });
19
- it('objMerge with array', () => {
20
- const obj1 = { a: 1, b: { c: ['arr1'] } };
21
- const obj2 = { a: 2, b: { c: ['arr2'] } };
22
- const merged = objMerge(obj1, obj2, 10, true);
23
- expect(merged).to.eql({ a: 2, b: { c: ['arr2', 'arr1'] } });
24
- });
25
- it('objMerge without array', () => {
26
- const obj1 = { a: 1, b: { c: ['arr1'] } };
27
- const obj2 = { a: 2, b: { c: ['arr2'] } };
28
- const merged = objMerge(obj1, obj2, 10, false);
29
- expect(merged).to.eql({ a: 2, b: { c: ['arr2'] } });
30
- });
31
- it('objMerge overwrites nested values', () => {
32
- const obj1 = { a: { b: 10 }, c: 'value' };
33
- const obj2 = { a: { b: 20 } };
34
- const merged = objMerge(obj1, obj2);
35
- expect(merged).to.eql({ a: { b: 20 }, c: 'value' });
36
- });
37
- it('objOmit', () => {
38
- const obj1 = { a: 1, b: { c: ['arr1'], d: 'string' } };
39
- const obj2 = { a: true, b: { c: true } };
40
- const omitted = objOmit(obj1, obj2);
41
- expect(omitted).to.eql({ b: { d: 'string' } });
42
- });
43
- it('objOmit with array', () => {
44
- const obj1 = { a: 1, b: { c: ['arr1', 'arr2'], d: 'string' } };
45
- const obj2 = { b: { c: ['arr1'] } };
46
- const omitted1_2 = objOmit(obj1, obj2, 10, true);
47
- expect(omitted1_2).to.eql({ a: 1, b: { c: ['arr2'], d: 'string' } });
48
- const obj3 = { a: [{ b: 1 }], c: 2 };
49
- const obj4 = { a: [{ b: 1 }] };
50
- const omitted3_4 = objOmit(obj3, obj4, 10, true);
51
- expect(omitted3_4).to.eql({ a: [], c: 2 });
52
- });
53
- it('objOmit without array', () => {
54
- const obj1 = { a: 1, b: { c: ['arr1', 'arr2'], d: 'string' } };
55
- const obj2 = { b: { c: ['arr1'] } };
56
- const omitted1_2 = objOmit(obj1, obj2, 10, false);
57
- expect(omitted1_2).to.eql({ a: 1, b: { d: 'string' } });
58
- });
59
- it('isObject', () => {
60
- expect(isObject({})).to.be.true;
61
- expect(isObject([])).to.be.false;
62
- expect(isObject(null)).to.be.false;
63
- expect(isObject(undefined)).to.be.false;
64
- expect(isObject(42)).to.be.false;
65
- });
66
- it('objKeys', () => {
67
- const obj = { a: 1, b: 2 };
68
- expect(objKeys(obj)).to.eql(['a', 'b']);
69
- });
70
- it('objLength', () => {
71
- const obj = { a: 1, b: 2 };
72
- expect(objLength(obj)).to.equal(2);
73
- });
74
- it('isObjEmpty', () => {
75
- expect(isObjEmpty({})).to.be.true;
76
- expect(isObjEmpty({ a: 1 })).to.be.false;
77
- });
78
- it('objMapEntries', () => {
79
- const obj = { a: 1, b: 2 };
80
- const result = objMapEntries(obj, (k, v) => v * 2);
81
- expect(result).to.eql([
82
- ['a', 2],
83
- ['b', 4],
84
- ]);
85
- });
86
- it('objMap', () => {
87
- const obj = { a: 1, b: 2 };
88
- const result = objMap(obj, (k, v) => v * 2);
89
- expect(result).to.eql({ a: 2, b: 4 });
90
- });
91
- it('objFilter', () => {
92
- const obj = { a: 1, b: 2, c: 3 };
93
- const result = objFilter(obj, (k, v) => v > 1);
94
- expect(result).to.eql({ b: 2, c: 3 });
95
- });
96
- describe(objDiff.name, () => {
97
- const testCases = [
98
- {
99
- description: 'should return empty object when objects are identical',
100
- obj1: { a: 1, b: 2, c: 3 },
101
- obj2: { a: 1, b: 2, c: 3 },
102
- expected: {},
103
- },
104
- {
105
- description: 'should return keys that exist in first object but not in second',
106
- obj1: { a: 1, b: 2, c: 3 },
107
- obj2: { a: 1, b: 2 },
108
- expected: { c: 3 },
109
- },
110
- {
111
- description: 'should return keys with different values',
112
- obj1: { a: 1, b: 2, c: 3 },
113
- obj2: { a: 1, b: 5, c: 3 },
114
- expected: { b: 2 },
115
- },
116
- {
117
- description: 'should return combination of missing keys and different values',
118
- obj1: { a: 1, b: 2, c: 3, d: 4 },
119
- obj2: { a: 1, b: 5, c: 3 },
120
- expected: { b: 2, d: 4 },
121
- },
122
- {
123
- description: 'should work with string keys and values',
124
- obj1: { name: 'Alice', city: 'Paris', age: '25' },
125
- obj2: { name: 'Alice', city: 'London', age: '25' },
126
- expected: { city: 'Paris' },
127
- },
128
- {
129
- description: 'should work with number keys',
130
- obj1: { 1: 'one', 2: 'two', 3: 'three' },
131
- obj2: { 1: 'one', 2: 'TWO', 3: 'three' },
132
- expected: { 2: 'two' },
133
- },
134
- {
135
- description: 'should work with boolean values',
136
- obj1: { a: true, b: false, c: true },
137
- obj2: { a: true, b: true, c: true },
138
- expected: { b: false },
139
- },
140
- {
141
- description: 'should work with undefined values',
142
- obj1: { a: undefined, b: 'value', c: undefined },
143
- obj2: { a: undefined, b: 'different', c: undefined },
144
- expected: { b: 'value' },
145
- },
146
- {
147
- description: 'should work with mixed primitive types',
148
- obj1: { a: 1, b: 'hello', c: true, d: undefined },
149
- obj2: { a: 2, b: 'hello', c: false, d: undefined },
150
- expected: { a: 1, c: true },
151
- },
152
- {
153
- description: 'should handle empty objects',
154
- obj1: {},
155
- obj2: {},
156
- expected: {},
157
- },
158
- {
159
- description: 'should handle first object empty, second has values',
160
- obj1: {},
161
- obj2: { a: 1, b: 2 },
162
- expected: {},
163
- },
164
- {
165
- description: 'should handle first object has values, second empty',
166
- obj1: { a: 1, b: 2 },
167
- obj2: {},
168
- expected: { a: 1, b: 2 },
169
- },
170
- {
171
- description: 'should work with bigint values',
172
- obj1: { a: 1n, b: 2n, c: 3n },
173
- obj2: { a: 1n, b: 5n, c: 3n },
174
- expected: { b: 2n },
175
- },
176
- {
177
- description: 'should handle edge case with falsy values',
178
- obj1: { a: 0, b: '', c: false, d: null },
179
- obj2: { a: 1, b: 'test', c: true, d: undefined },
180
- expected: { a: 0, b: '', c: false, d: null },
181
- },
182
- {
183
- description: 'should work with custom equality function - case insensitive',
184
- obj1: { a: 'Hello', b: 'WORLD', c: 'Test' },
185
- obj2: { a: 'hello', b: 'world', c: 'different' },
186
- expected: { c: 'Test' },
187
- areEquals: (a, b) => a.toLowerCase() === b.toLowerCase(),
188
- },
189
- {
190
- description: 'should work with custom equality function - tolerance based numbers',
191
- obj1: { a: 1.1, b: 2.2, c: 3.3 },
192
- obj2: { a: 1.15, b: 2.25, c: 4.0 },
193
- expected: { c: 3.3 },
194
- areEquals: (a, b) => Math.abs(a - b) < 0.1,
195
- },
196
- {
197
- description: 'should work with complex custom equality function',
198
- obj1: { user1: 'active', user2: 'inactive', user3: 'pending' },
199
- obj2: { user1: 'ACTIVE', user2: 'disabled', user3: 'PENDING' },
200
- expected: {},
201
- areEquals: (a, b) => {
202
- const normalize = (status) => {
203
- const lower = status.toLowerCase();
204
- return lower === 'disabled' ? 'inactive' : lower;
205
- };
206
- return normalize(a) === normalize(b);
207
- },
208
- },
209
- ];
210
- for (const { description, obj1, obj2, expected, areEquals } of testCases) {
211
- it(description, () => {
212
- const result = objDiff(obj1, obj2, areEquals);
213
- expect(result).to.eql(expected);
214
- });
215
- }
216
- });
217
- it('deepFind should find nested object', () => {
218
- const obj = { a: { b: { c: 3 } } };
219
- const result = deepFind(obj, (v) => v && v.c === 3);
220
- expect(result).to.eql({ c: 3 });
221
- });
222
- it('deepFind should return undefined if object is not found', () => {
223
- const obj = { a: { b: { c: 3 } } };
224
- const result = deepFind(obj, (v) => v && v.c === 4);
225
- expect(result).to.be.undefined;
226
- });
227
- it('promiseObjAll', async () => {
228
- const obj = { a: Promise.resolve(1), b: Promise.resolve(2) };
229
- const result = await promiseObjAll(obj);
230
- expect(result).to.eql({ a: 1, b: 2 });
231
- });
232
- it('pick should return a subset of the object', () => {
233
- const obj = { a: 1, b: 2, c: 3 };
234
- const result = pick(obj, ['a', 'c']);
235
- expect(result).to.eql({ a: 1, c: 3 });
236
- });
237
- it('pick should return an empty object if no keys are provided', () => {
238
- const obj = { a: 1, b: 2, c: 3 };
239
- const result = pick(obj, []);
240
- expect(result).to.eql({});
241
- });
242
- it("pick should return an empty object if the object doesn't contain the keys", () => {
243
- const obj = { c: 4, d: 5 };
244
- const result = pick(obj, ['a', 'b']);
245
- expect(result).to.eql({});
246
- });
247
- describe('invertKeysAndValues', () => {
248
- it('invertKeysAndValues should invert the keys and values', () => {
249
- const obj = { a: '1', b: '2' };
250
- const result = invertKeysAndValues(obj);
251
- expect(result).to.eql({ '1': 'a', '2': 'b' });
252
- });
253
- it('invertKeysAndValues should return an empty object if the object is empty', () => {
254
- const obj = {};
255
- const result = invertKeysAndValues(obj);
256
- expect(result).to.eql({});
257
- });
258
- it('invertKeysAndValues should return an object if the object has duplicate values', () => {
259
- const obj = { a: '1', b: '1' };
260
- const result = invertKeysAndValues(obj);
261
- expect(result).to.eql({ '1': 'b' });
262
- });
263
- it('invertKeysAndValues should return an object if the object has undefined/null values', () => {
264
- const obj = { a: '1', b: '2', c: undefined, d: null, e: 0 };
265
- const result = invertKeysAndValues(obj);
266
- expect(result).to.eql({ '1': 'a', '2': 'b', '0': 'e' });
267
- });
268
- });
269
- it('arrayToObject', () => {
270
- const keys = ['a', 'b'];
271
- const result = arrayToObject(keys);
272
- expect(result).to.eql({ a: true, b: true });
273
- });
274
- it('stringifyObject', () => {
275
- const obj = { a: 1, b: 2 };
276
- const jsonResult = stringifyObject(obj, 'json');
277
- expect(jsonResult).to.equal('{"a":1,"b":2}');
278
- const yamlResult = stringifyObject(obj, 'yaml');
279
- expect(yamlResult).to.include('a: 1\nb: 2');
280
- });
281
- describe('diffObjMerge', () => {
282
- it('should merge objects with equal values', () => {
283
- const actual = { a: 1, b: 2 };
284
- const expected = { a: 1, b: 2 };
285
- const result = diffObjMerge(actual, expected);
286
- expect(result).to.eql({
287
- isInvalid: false,
288
- mergedObject: { a: 1, b: 2 },
289
- });
290
- });
291
- it('should return a diff for objects with different values', () => {
292
- const actual = { a: 1, b: 2 };
293
- const expected = { a: 1, b: 3 };
294
- const result = diffObjMerge(actual, expected);
295
- expect(result).to.eql({
296
- isInvalid: true,
297
- mergedObject: {
298
- a: 1,
299
- b: { actual: 2, expected: 3 },
300
- },
301
- });
302
- });
303
- it('should detect missing fields in the top level object', () => {
304
- const actual = { a: 1 };
305
- const expected = { a: 1, b: 3 };
306
- const result = diffObjMerge(actual, expected);
307
- expect(result).to.eql({
308
- isInvalid: true,
309
- mergedObject: {
310
- a: 1,
311
- b: { actual: '', expected: 3 },
312
- },
313
- });
314
- });
315
- it('should detect extra fields in the top level object', () => {
316
- const actual = { a: 1, b: 2 };
317
- const expected = { a: 1 };
318
- const result = diffObjMerge(actual, expected);
319
- expect(result).to.eql({
320
- isInvalid: true,
321
- mergedObject: {
322
- a: 1,
323
- b: { actual: 2, expected: '' },
324
- },
325
- });
326
- });
327
- it('should merge nested objects and show differences', () => {
328
- const actual = { a: 1, b: { c: 2, d: 4 } };
329
- const expected = { a: 1, b: { c: 2, d: 3 } };
330
- const result = diffObjMerge(actual, expected);
331
- expect(result).to.eql({
332
- isInvalid: true,
333
- mergedObject: {
334
- a: 1,
335
- b: {
336
- c: 2,
337
- d: { actual: 4, expected: 3 },
338
- },
339
- },
340
- });
341
- });
342
- it('should throw an error when maxDepth is exceeded', () => {
343
- const actual = { a: { b: { c: { d: { e: 5 } } } } };
344
- const expected = { a: { b: { c: { d: { e: 5 } } } } };
345
- expect(() => diffObjMerge(actual, expected, 3)).to.Throw('diffObjMerge tried to go too deep');
346
- });
347
- it('should merge arrays of equal length and show the diffs', () => {
348
- const actual = [1, 2, 3];
349
- const expected = [1, 2, 4];
350
- const result = diffObjMerge(actual, expected);
351
- expect(result).to.eql({
352
- isInvalid: true,
353
- mergedObject: [1, 2, { actual: 3, expected: 4 }],
354
- });
355
- });
356
- it('should return a diff for arrays of different lengths', () => {
357
- const actual = [1, 2];
358
- const expected = [1, 2, 3];
359
- const result = diffObjMerge(actual, expected);
360
- expect(result).to.eql({
361
- isInvalid: true,
362
- mergedObject: {
363
- actual,
364
- expected,
365
- },
366
- });
367
- });
368
- it('should handle null and undefined values properly', () => {
369
- const actual = { a: null, b: 2 };
370
- const expected = { a: undefined, b: 2 };
371
- const result = diffObjMerge(actual, expected);
372
- expect(result).to.eql({
373
- isInvalid: false,
374
- mergedObject: {
375
- a: undefined,
376
- b: 2,
377
- },
378
- });
379
- });
380
- });
381
- describe('mustGet', () => {
382
- it('should return the value if it exists', () => {
383
- const obj = { a: 1, b: 2 };
384
- expect(mustGet(obj, 'a')).to.equal(1);
385
- });
386
- it('should throw an error if the value does not exist', () => {
387
- const obj = { a: 1, b: 2 };
388
- expect(() => mustGet(obj, 'c')).to.Throw();
389
- });
390
- });
391
- describe(transformObj.name, () => {
392
- it('should format a string', () => {
393
- const actual = 'HELLO';
394
- const expected = 'hello';
395
- const formatter = (obj) => typeof obj === 'string' ? obj.toLowerCase() : obj;
396
- expect(transformObj(actual, formatter)).to.eql(expected);
397
- });
398
- it('should format a number', () => {
399
- const actual = 42;
400
- const expected = 84;
401
- const formatter = (obj) => typeof obj === 'number' ? obj * 2 : obj;
402
- expect(transformObj(actual, formatter)).to.eql(expected);
403
- });
404
- it('should return an empty object when given an empty object', () => {
405
- const actual = {};
406
- const expected = {};
407
- const formatter = (obj) => obj;
408
- expect(transformObj(actual, formatter)).to.eql(expected);
409
- });
410
- it('should return an empty array when given an empty array', () => {
411
- const actual = [];
412
- const expected = [];
413
- const formatter = (obj) => obj;
414
- expect(transformObj(actual, formatter)).to.eql(expected);
415
- });
416
- it('should remove values when shouldInclude is false', () => {
417
- const actual = {
418
- keep: 'value',
419
- remove: 'this should be removed',
420
- };
421
- const expected = {
422
- keep: 'value',
423
- };
424
- const formatter = (obj, propPath) => {
425
- const parentKey = propPath[propPath.length - 1];
426
- if (parentKey === 'remove') {
427
- return undefined;
428
- }
429
- return obj;
430
- };
431
- expect(transformObj(actual, formatter)).to.eql(expected);
432
- });
433
- it('should throw an error when maximum depth is exceeded', () => {
434
- // Build a nested object with depth > 15.
435
- const obj = {};
436
- let current = obj;
437
- for (let i = 0; i < 16; i++) {
438
- current['level' + i] = {};
439
- current = current['level' + i];
440
- }
441
- const formatter = (obj) => obj;
442
- expect(() => transformObj(obj, formatter)).to.throw('transformObj went too deep. Max depth is 15');
443
- });
444
- const testCases = [
445
- { actual: { a: 'Henlo', b: 2 }, expected: { a: 'henlo', b: 2 } },
446
- {
447
- actual: {
448
- a: {
449
- b: 'Test',
450
- },
451
- c: {
452
- d: {
453
- e: 'TeSt 2',
454
- },
455
- },
456
- },
457
- expected: {
458
- a: {
459
- b: 'test',
460
- },
461
- c: {
462
- d: {
463
- e: 'test 2',
464
- },
465
- },
466
- },
467
- },
468
- ];
469
- for (const { actual, expected } of testCases) {
470
- it('should successfully apply the formatter function to an object', () => {
471
- const formatter = (obj) => {
472
- return typeof obj === 'string' ? obj.toLowerCase() : obj;
473
- };
474
- const formatted = transformObj(actual, formatter);
475
- expect(formatted).to.eql(expected);
476
- });
477
- }
478
- });
479
- describe(sortArraysInObject.name, () => {
480
- [1, 'hello', true, null, undefined].map((value) => {
481
- it(`should return the same primitive value if the input is a primitive ${value}`, () => {
482
- expect(sortArraysInObject(value)).to.equal(value);
483
- });
484
- });
485
- it('should return an empty array if the input is an empty array', () => {
486
- expect(sortArraysInObject([])).to.deep.equal([]);
487
- });
488
- it('should recursively sort arrays within an array', () => {
489
- const input = [
490
- [3, 1, 2],
491
- [6, 4, 5],
492
- ];
493
- const expected = [
494
- [1, 2, 3],
495
- [4, 5, 6],
496
- ];
497
- expect(sortArraysInObject(input)).to.deep.equal(expected);
498
- });
499
- it('should return an empty object if the input is an empty object', () => {
500
- expect(sortArraysInObject({})).to.deep.equal({});
501
- });
502
- it('should recursively sort arrays within an object', () => {
503
- const input = {
504
- a: [3, 1, 2],
505
- b: { c: [6, 4, 5] },
506
- };
507
- const expected = {
508
- a: [1, 2, 3],
509
- b: { c: [4, 5, 6] },
510
- };
511
- expect(sortArraysInObject(input)).to.deep.equal(expected);
512
- });
513
- });
514
- describe(keepOnlyDiffObjects.name, () => {
515
- const testCases = [
516
- {
517
- input: {
518
- a: {
519
- foo: { expected: 1, actual: 2 },
520
- bar: { something: true },
521
- nested: {
522
- baz: { expected: 'x', actual: 'y' },
523
- qux: { nope: 0 },
524
- },
525
- },
526
- arr: [
527
- { alpha: { expected: 10, actual: 20 } },
528
- { beta: { wrong: true } },
529
- ],
530
- plain: 123,
531
- },
532
- expected: {
533
- a: {
534
- foo: { expected: 1, actual: 2 },
535
- nested: {
536
- baz: { expected: 'x', actual: 'y' },
537
- },
538
- },
539
- arr: [{ alpha: { expected: 10, actual: 20 } }],
540
- },
541
- },
542
- {
543
- input: {
544
- ethereum: {
545
- mailbox: '0xc005dc82818d67af737725bd4bf75435d065d239',
546
- owner: '0xd1e6626310fd54eceb5b9a51da2ec329d6d4b68a',
547
- hook: {
548
- type: 'aggregationHook',
549
- hooks: [
550
- {
551
- type: 'protocolFee',
552
- protocolFee: {
553
- expected: '158365200000000',
554
- actual: '129871800000000',
555
- },
556
- beneficiary: '0x8410927c286a38883bc23721e640f31d3e3e79f8',
557
- },
558
- ],
559
- },
560
- interchainSecurityModule: {
561
- type: 'staticAggregationIsm',
562
- modules: [
563
- {
564
- owner: '0xd1e6626310fd54eceb5b9a51da2ec329d6d4b68a',
565
- type: 'defaultFallbackRoutingIsm',
566
- domains: {},
567
- },
568
- {
569
- owner: '0xd1e6626310fd54eceb5b9a51da2ec329d6d4b68a',
570
- type: 'domainRoutingIsm',
571
- domains: {
572
- berachain: {
573
- type: 'staticAggregationIsm',
574
- modules: [
575
- {
576
- type: 'merkleRootMultisigIsm',
577
- validators: [
578
- '0xa7341aa60faad0ce728aa9aeb67bb880f55e4392',
579
- '0xae09cb3febc4cad59ef5a56c1df741df4eb1f4b6',
580
- ],
581
- threshold: 1,
582
- },
583
- {
584
- type: 'messageIdMultisigIsm',
585
- validators: [
586
- '0xa7341aa60faad0ce728aa9aeb67bb880f55e4392',
587
- '0xae09cb3febc4cad59ef5a56c1df741df4eb1f4b6',
588
- ],
589
- threshold: 1,
590
- },
591
- ],
592
- threshold: 1,
593
- },
594
- },
595
- },
596
- ],
597
- threshold: 2,
598
- },
599
- decimals: {
600
- expected: 18,
601
- actual: 10,
602
- },
603
- isNft: false,
604
- type: 'xERC20Lockbox',
605
- token: '0xbc5511354c4a9a50de928f56db01dd327c4e56d5',
606
- remoteRouters: {
607
- '80094': {
608
- address: {
609
- expected: '0x00000000000000000000000025a851bf599cb8aef00ac1d1a9fb575ebf9d94b0',
610
- actual: '0x00000000000000000000000025a851bf599cb8aef00ac1d1a9fb575ebf9d94b1',
611
- },
612
- },
613
- },
614
- },
615
- },
616
- expected: {
617
- ethereum: {
618
- type: 'xERC20Lockbox',
619
- hook: {
620
- type: 'aggregationHook',
621
- hooks: [
622
- {
623
- type: 'protocolFee',
624
- protocolFee: {
625
- expected: '158365200000000',
626
- actual: '129871800000000',
627
- },
628
- },
629
- ],
630
- },
631
- decimals: {
632
- expected: 18,
633
- actual: 10,
634
- },
635
- remoteRouters: {
636
- '80094': {
637
- address: {
638
- expected: '0x00000000000000000000000025a851bf599cb8aef00ac1d1a9fb575ebf9d94b0',
639
- actual: '0x00000000000000000000000025a851bf599cb8aef00ac1d1a9fb575ebf9d94b1',
640
- },
641
- },
642
- },
643
- },
644
- },
645
- },
646
- ];
647
- for (const { expected, input } of testCases) {
648
- it(`should keep only the fields that have diff objects`, () => {
649
- const act = keepOnlyDiffObjects(input);
650
- expect(act).to.eql(expected);
651
- });
652
- }
653
- });
654
- });
655
- //# sourceMappingURL=objects.test.js.map