-
-
Notifications
You must be signed in to change notification settings - Fork 699
/
Copy pathflattenAttributes.test.ts
315 lines (282 loc) · 9.07 KB
/
flattenAttributes.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import { flattenAttributes, unflattenAttributes } from "../src/v3/utils/flattenAttributes.js";
describe("flattenAttributes", () => {
it("handles number keys correctl", () => {
expect(flattenAttributes({ bar: { "25": "foo" } })).toEqual({ "bar.25": "foo" });
expect(unflattenAttributes({ "bar.25": "foo" })).toEqual({ bar: { "25": "foo" } });
expect(flattenAttributes({ bar: ["foo", "baz"] })).toEqual({
"bar.[0]": "foo",
"bar.[1]": "baz",
});
expect(unflattenAttributes({ "bar.[0]": "foo", "bar.[1]": "baz" })).toEqual({
bar: ["foo", "baz"],
});
expect(flattenAttributes({ bar: { 25: "foo" } })).toEqual({ "bar.25": "foo" });
expect(unflattenAttributes({ "bar.25": "foo" })).toEqual({ bar: { 25: "foo" } });
});
it("handles null correctly", () => {
expect(flattenAttributes(null)).toEqual({ "": "$@null((" });
expect(unflattenAttributes({ "": "$@null((" })).toEqual(null);
expect(flattenAttributes(null, "$output")).toEqual({ $output: "$@null((" });
expect(flattenAttributes({ foo: null })).toEqual({ foo: "$@null((" });
expect(unflattenAttributes({ foo: "$@null((" })).toEqual({ foo: null });
expect(flattenAttributes({ foo: [null] })).toEqual({ "foo.[0]": "$@null((" });
expect(unflattenAttributes({ "foo.[0]": "$@null((" })).toEqual({ foo: [null] });
expect(flattenAttributes([null])).toEqual({ "[0]": "$@null((" });
expect(unflattenAttributes({ "[0]": "$@null((" })).toEqual([null]);
});
it("flattens string attributes correctly", () => {
const result = flattenAttributes("testString");
expect(result).toEqual({ "": "testString" });
expect(unflattenAttributes(result)).toEqual("testString");
});
it("flattens number attributes correctly", () => {
const result = flattenAttributes(12345);
expect(result).toEqual({ "": 12345 });
expect(unflattenAttributes(result)).toEqual(12345);
});
it("flattens boolean attributes correctly", () => {
const result = flattenAttributes(true);
expect(result).toEqual({ "": true });
expect(unflattenAttributes(result)).toEqual(true);
});
it("flattens boolean attributes correctly", () => {
const result = flattenAttributes(true, "$output");
expect(result).toEqual({ $output: true });
expect(unflattenAttributes(result)).toEqual({ $output: true });
});
it("flattens array attributes correctly", () => {
const input = [1, 2, 3];
const result = flattenAttributes(input);
expect(result).toEqual({ "[0]": 1, "[1]": 2, "[2]": 3 });
expect(unflattenAttributes(result)).toEqual(input);
});
it("flattens complex objects correctly", () => {
const obj = {
level1: {
level2: {
value: "test",
},
array: [1, 2, 3],
},
};
const expected = {
"level1.level2.value": "test",
"level1.array.[0]": 1,
"level1.array.[1]": 2,
"level1.array.[2]": 3,
};
expect(flattenAttributes(obj)).toEqual(expected);
});
it("applies prefixes correctly", () => {
const obj = { key: "value" };
const expected = { "prefix.key": "value" };
expect(flattenAttributes(obj, "prefix")).toEqual(expected);
});
it("handles arrays of objects correctly", () => {
const obj = {
array: [{ key: "value" }, { key: "value" }, { key: "value" }],
};
const expected = {
"array.[0].key": "value",
"array.[1].key": "value",
"array.[2].key": "value",
};
expect(flattenAttributes(obj)).toEqual(expected);
});
it("handles arrays of objects correctly with prefixing correctly", () => {
const obj = {
array: [{ key: "value" }, { key: "value" }, { key: "value" }],
};
const expected = {
"prefix.array.[0].key": "value",
"prefix.array.[1].key": "value",
"prefix.array.[2].key": "value",
};
expect(flattenAttributes(obj, "prefix")).toEqual(expected);
});
it("handles objects of objects correctly", () => {
const obj = {
level1: {
level2: {
key: "value",
},
},
};
const expected = { "level1.level2.key": "value" };
expect(flattenAttributes(obj)).toEqual(expected);
});
it("handles objects of objects correctly with prefixing", () => {
const obj = {
level1: {
level2: {
key: "value",
},
},
};
const expected = { "prefix.level1.level2.key": "value" };
expect(flattenAttributes(obj, "prefix")).toEqual(expected);
});
it("handles retry.byStatus correctly", () => {
const obj = {
"500": {
strategy: "backoff",
maxAttempts: 2,
factor: 2,
minTimeoutInMs: 1_000,
maxTimeoutInMs: 30_000,
randomize: false,
},
};
const expected = {
"retry.byStatus.500.strategy": "backoff",
"retry.byStatus.500.maxAttempts": 2,
"retry.byStatus.500.factor": 2,
"retry.byStatus.500.minTimeoutInMs": 1_000,
"retry.byStatus.500.maxTimeoutInMs": 30_000,
"retry.byStatus.500.randomize": false,
};
expect(flattenAttributes(obj, "retry.byStatus")).toEqual(expected);
});
it("handles circular references correctly", () => {
const user = { name: "Alice" };
user["blogPosts"] = [{ title: "Post 1", author: user }]; // Circular reference
const result = flattenAttributes(user);
expect(result).toEqual({
"name": "Alice",
"blogPosts.[0].title": "Post 1",
"blogPosts.[0].author": "$@circular((",
});
});
it("handles nested circular references correctly", () => {
const user = { name: "Bob" };
user["friends"] = [user]; // Circular reference
const result = flattenAttributes(user);
expect(result).toEqual({
"name": "Bob",
"friends.[0]": "$@circular((",
});
});
it("handles . (dot) in keys correctly", () => {
const obj = {
"Key 0.002mm": 31.4,
};
const expected = { "Key 0__DOT__002mm": 31.4 };
expect(flattenAttributes(obj)).toEqual(expected);
expect(unflattenAttributes(expected)).toEqual(obj);
const obj2 = {
level1: {
level2: {
value: "test",
"level3.key": "value",
},
array: [1, 2, 3],
},
};
const expected2 = {
"level1.level2.value": "test",
"level1.level2.level3__DOT__key": "value",
"level1.array.[0]": 1,
"level1.array.[1]": 2,
"level1.array.[2]": 3,
};
expect(flattenAttributes(obj2)).toEqual(expected2);
const PI = 3.14159265359;
const result = flattenAttributes(PI);
expect(result).toEqual({ "": PI });
expect(unflattenAttributes(result)).toEqual(PI);
});
});
describe("unflattenAttributes", () => {
it("returns the original object for primitive types", () => {
// @ts-expect-error
expect(unflattenAttributes("testString")).toEqual("testString");
// @ts-expect-error
expect(unflattenAttributes(12345)).toEqual(12345);
// @ts-expect-error
expect(unflattenAttributes(true)).toEqual(true);
});
it("correctly reconstructs an object from flattened attributes", () => {
const flattened = {
"level1.level2.value": "test",
"level1.array.[0]": 1,
"level1.array.[1]": 2,
"level1.array.[2]": 3,
};
const expected = {
level1: {
level2: {
value: "test",
},
array: [1, 2, 3],
},
};
expect(unflattenAttributes(flattened)).toEqual(expected);
});
it("handles complex nested objects with mixed types", () => {
const flattened = {
"user.details.name": "John Doe",
"user.details.age": 30,
"user.preferences.colors.[0]": "blue",
"user.preferences.colors.[1]": "green",
"user.active": true,
};
const expected = {
user: {
details: {
name: "John Doe",
age: 30,
},
preferences: {
colors: ["blue", "green"],
},
active: true,
},
};
expect(unflattenAttributes(flattened)).toEqual(expected);
});
it("correctly identifies arrays vs objects", () => {
const flattened = {
"array.[0]": 1,
"array.[1]": 2,
"object.key": "value",
};
const expected = {
array: [1, 2],
object: {
key: "value",
},
};
expect(unflattenAttributes(flattened)).toEqual(expected);
});
it("rehydrates circular references correctly", () => {
const flattened = {
"name": "Alice",
"blogPosts.[0].title": "Post 1",
"blogPosts.[0].author": "$@circular((",
};
const result = unflattenAttributes(flattened);
expect(result).toEqual({
name: "Alice",
blogPosts: [{ title: "Post 1", author: "[Circular Reference]" }],
});
});
it("handles espacaped . (dot) in keys correctly and returns original object", () => {
const flattened = {
"level1.level2.value": "test",
"level1.level2.level3__DOT__key": "value",
"level1.array.[0]": 1,
"level1.array.[1]": 2,
"level1.array.[2]": 3,
};
const original = {
level1: {
level2: {
value: "test",
"level3.key": "value",
},
array: [1, 2, 3],
},
};
expect(unflattenAttributes(flattened)).toEqual(original);
});
});