@rsdk/common 6.0.0-next.0 → 6.0.0-next.1
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.
|
@@ -6,6 +6,7 @@ export interface NormalizeOptions {
|
|
|
6
6
|
filterKeys?: string[];
|
|
7
7
|
sortKeys?: SortKeys;
|
|
8
8
|
maxDepth?: number;
|
|
9
|
+
keepOriginPrototype?: boolean;
|
|
9
10
|
}
|
|
10
11
|
/**
|
|
11
12
|
* Creates a function that prepares objects for JSON.stringify().
|
|
@@ -24,6 +25,7 @@ export interface NormalizeOptions {
|
|
|
24
25
|
* - Map -> '[Map size]'
|
|
25
26
|
* - Set -> '[Set size]'
|
|
26
27
|
* - Limits object nesting depth (default: 10)
|
|
28
|
+
* - Keeps origin prototype (default: false)
|
|
27
29
|
* - Protects against large objects (>16M keys)
|
|
28
30
|
*
|
|
29
31
|
* @example
|
|
@@ -43,6 +45,7 @@ export interface NormalizeOptions {
|
|
|
43
45
|
* - filterKeys: Array of keys to exclude
|
|
44
46
|
* - sortKeys: Custom key sorting configuration
|
|
45
47
|
* - maxDepth: Maximum nesting depth (default: 10)
|
|
48
|
+
* - keepOriginPrototype: Whether to keep the original prototype (default: false)
|
|
46
49
|
* @param depth - Current recursion depth (internal use)
|
|
47
50
|
*/
|
|
48
51
|
export declare const normalizer: () => ((x: unknown, options?: NormalizeOptions, depth?: number) => unknown);
|
|
@@ -20,6 +20,7 @@ const DEFAULT_MAX_DEPTH = 10;
|
|
|
20
20
|
* - Map -> '[Map size]'
|
|
21
21
|
* - Set -> '[Set size]'
|
|
22
22
|
* - Limits object nesting depth (default: 10)
|
|
23
|
+
* - Keeps origin prototype (default: false)
|
|
23
24
|
* - Protects against large objects (>16M keys)
|
|
24
25
|
*
|
|
25
26
|
* @example
|
|
@@ -39,6 +40,7 @@ const DEFAULT_MAX_DEPTH = 10;
|
|
|
39
40
|
* - filterKeys: Array of keys to exclude
|
|
40
41
|
* - sortKeys: Custom key sorting configuration
|
|
41
42
|
* - maxDepth: Maximum nesting depth (default: 10)
|
|
43
|
+
* - keepOriginPrototype: Whether to keep the original prototype (default: false)
|
|
42
44
|
* @param depth - Current recursion depth (internal use)
|
|
43
45
|
*/
|
|
44
46
|
const normalizer = () => {
|
|
@@ -94,13 +96,22 @@ const normalizer = () => {
|
|
|
94
96
|
if (options?.sortKeys?.match(xObject)) {
|
|
95
97
|
keys.sort(options.sortKeys.sortFn);
|
|
96
98
|
}
|
|
99
|
+
/**
|
|
100
|
+
* There is several cases, when we have to keep the origin prototype.
|
|
101
|
+
* For example, if normalizer is executing implicitly
|
|
102
|
+
* and normalized object will be used in `instanceof` condition
|
|
103
|
+
* next to execution context
|
|
104
|
+
*/
|
|
105
|
+
const normalized = options?.keepOriginPrototype
|
|
106
|
+
? Object.setPrototypeOf({}, Object.getPrototypeOf(xObject))
|
|
107
|
+
: {};
|
|
97
108
|
return keys.reduce((acc, key) => {
|
|
98
109
|
if (options?.filterKeys?.includes(key)) {
|
|
99
110
|
return acc;
|
|
100
111
|
}
|
|
101
112
|
acc[key] = normalize(xObject[key], options, newDepth);
|
|
102
113
|
return acc;
|
|
103
|
-
},
|
|
114
|
+
}, normalized);
|
|
104
115
|
}
|
|
105
116
|
catch (error) {
|
|
106
117
|
return `[Can not get keys from object. ${error}]`;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"obj-normalization.js","sourceRoot":"","sources":["../../src/functions/obj-normalization.ts"],"names":[],"mappings":";;;AAAA,2DAAqD;
|
|
1
|
+
{"version":3,"file":"obj-normalization.js","sourceRoot":"","sources":["../../src/functions/obj-normalization.ts"],"names":[],"mappings":";;;AAAA,2DAAqD;AAcrD,MAAM,iBAAiB,GAAG,EAAE,CAAC;AAE7B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AACI,MAAM,UAAU,GAAG,GAIZ,EAAE;IACd,MAAM,KAAK,GAAG,IAAI,GAAG,EAAE,CAAC;I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AC,EAAE,UAAU,CAAC,CAAC;QACjB,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,OAAO,kCAAkC,KAAK,GAAG,CAAC;QACpD,CAAC;IACH,CAAC,CAAC;IAEF,OAAO,SAAS,CAAC;AACnB,CAAC,CAAC;AAtGW,QAAA,UAAU,cAsGrB;AAEF,MAAM,cAAc,GAAG,IAAI,GAAG,EAAkB;KAC7C,GAAG,CAAC,UAAU,EAAE,CAAC,CAAC;KAClB,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC;KACd,GAAG,CAAC,SAAS,EAAE,CAAC,CAAC;KACjB,GAAG,CAAC,SAAS,EAAE,CAAC,CAAC;KACjB,GAAG,CAAC,OAAO,EAAE,CAAC,CAAC;KACf,GAAG,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC;AAEnB;;GAEG;AACU,QAAA,aAAa,GAAa;IACrC,KAAK,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,YAAY,KAAK;IAChC,MAAM,EAAE,CAAC,CAAS,EAAE,CAAS,EAAU,EAAE,CACvC,CAAC,cAAc,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,cAAc,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;CACvE,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rsdk/common",
|
|
3
|
-
"version": "6.0.0-next.
|
|
3
|
+
"version": "6.0.0-next.1",
|
|
4
4
|
"description": "Useful common classes, functions and types",
|
|
5
5
|
"license": "Apache License 2.0",
|
|
6
6
|
"publishConfig": {
|
|
@@ -16,5 +16,5 @@
|
|
|
16
16
|
"dependencies": {
|
|
17
17
|
"rxjs": "^7.8.1"
|
|
18
18
|
},
|
|
19
|
-
"gitHead": "
|
|
19
|
+
"gitHead": "3114a76df17cf98d6f3cbb8a931599a883af6783"
|
|
20
20
|
}
|
|
@@ -207,3 +207,105 @@ describe('max depth', () => {
|
|
|
207
207
|
);
|
|
208
208
|
});
|
|
209
209
|
});
|
|
210
|
+
|
|
211
|
+
describe('keep origin prototype', () => {
|
|
212
|
+
class TestObject {
|
|
213
|
+
a = 1;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const objectCases: {
|
|
217
|
+
input: any;
|
|
218
|
+
output: any;
|
|
219
|
+
options?: NormalizeOptions;
|
|
220
|
+
title: string;
|
|
221
|
+
}[] = [
|
|
222
|
+
{
|
|
223
|
+
input: new TestObject(),
|
|
224
|
+
output: Object,
|
|
225
|
+
title: `should have the Object prototype if input is an instance of a class without keepOriginPrototype option`,
|
|
226
|
+
},
|
|
227
|
+
{
|
|
228
|
+
input: new TestObject(),
|
|
229
|
+
output: Object,
|
|
230
|
+
options: { keepOriginPrototype: false },
|
|
231
|
+
title: `should not have the same prototype if keepOriginPrototype is false`,
|
|
232
|
+
},
|
|
233
|
+
{
|
|
234
|
+
input: new TestObject(),
|
|
235
|
+
output: Object,
|
|
236
|
+
options: { keepOriginPrototype: true },
|
|
237
|
+
title: `should have the same prototype if keepOriginPrototype is true`,
|
|
238
|
+
},
|
|
239
|
+
{
|
|
240
|
+
input: {},
|
|
241
|
+
output: Object,
|
|
242
|
+
title: `should have the object prototype if input is an object by default without keepOriginPrototype option`,
|
|
243
|
+
},
|
|
244
|
+
{
|
|
245
|
+
input: {},
|
|
246
|
+
options: { keepOriginPrototype: false },
|
|
247
|
+
output: Object,
|
|
248
|
+
title: `should have the same prototype if input is an object and keepOriginPrototype is false`,
|
|
249
|
+
},
|
|
250
|
+
{
|
|
251
|
+
input: {},
|
|
252
|
+
options: { keepOriginPrototype: true },
|
|
253
|
+
output: Object,
|
|
254
|
+
title: `should have the same prototype if input is an object and keepOriginPrototype is true`,
|
|
255
|
+
},
|
|
256
|
+
] as const;
|
|
257
|
+
|
|
258
|
+
it.each(objectCases)('$title', ({ input, output, options }) => {
|
|
259
|
+
const result = normalizer()(input, options);
|
|
260
|
+
|
|
261
|
+
expect(result).toBeInstanceOf(output);
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
const primitiveCases: {
|
|
265
|
+
input: any;
|
|
266
|
+
output: any;
|
|
267
|
+
options?: NormalizeOptions;
|
|
268
|
+
title: string;
|
|
269
|
+
}[] = [
|
|
270
|
+
{
|
|
271
|
+
input: 1,
|
|
272
|
+
output: 'number',
|
|
273
|
+
title: `should not affect number primitive if options is not set`,
|
|
274
|
+
},
|
|
275
|
+
{
|
|
276
|
+
input: 1,
|
|
277
|
+
options: { keepOriginPrototype: false },
|
|
278
|
+
output: 'number',
|
|
279
|
+
title: `should not affect number primitive if keepOriginPrototype is false`,
|
|
280
|
+
},
|
|
281
|
+
{
|
|
282
|
+
input: 1,
|
|
283
|
+
options: { keepOriginPrototype: true },
|
|
284
|
+
output: 'number',
|
|
285
|
+
title: `should not affect number primitive if keepOriginPrototype is true`,
|
|
286
|
+
},
|
|
287
|
+
{
|
|
288
|
+
input: '1',
|
|
289
|
+
output: 'string',
|
|
290
|
+
title: `should not affect string primitive if options is not set`,
|
|
291
|
+
},
|
|
292
|
+
{
|
|
293
|
+
input: '1',
|
|
294
|
+
options: { keepOriginPrototype: false },
|
|
295
|
+
output: 'string',
|
|
296
|
+
title: `should not affect string primitive if keepOriginPrototype is false`,
|
|
297
|
+
},
|
|
298
|
+
{
|
|
299
|
+
input: '1',
|
|
300
|
+
options: { keepOriginPrototype: true },
|
|
301
|
+
output: 'string',
|
|
302
|
+
title: `should not affect string primitive if keepOriginPrototype is true`,
|
|
303
|
+
},
|
|
304
|
+
];
|
|
305
|
+
|
|
306
|
+
it.each(primitiveCases)('$title', ({ input, output, options }) => {
|
|
307
|
+
const result = normalizer()(input, options);
|
|
308
|
+
|
|
309
|
+
expect(typeof result).toBe(output);
|
|
310
|
+
});
|
|
311
|
+
});
|
|
@@ -9,6 +9,7 @@ export interface NormalizeOptions {
|
|
|
9
9
|
filterKeys?: string[];
|
|
10
10
|
sortKeys?: SortKeys;
|
|
11
11
|
maxDepth?: number;
|
|
12
|
+
keepOriginPrototype?: boolean;
|
|
12
13
|
}
|
|
13
14
|
|
|
14
15
|
const DEFAULT_MAX_DEPTH = 10;
|
|
@@ -30,6 +31,7 @@ const DEFAULT_MAX_DEPTH = 10;
|
|
|
30
31
|
* - Map -> '[Map size]'
|
|
31
32
|
* - Set -> '[Set size]'
|
|
32
33
|
* - Limits object nesting depth (default: 10)
|
|
34
|
+
* - Keeps origin prototype (default: false)
|
|
33
35
|
* - Protects against large objects (>16M keys)
|
|
34
36
|
*
|
|
35
37
|
* @example
|
|
@@ -49,6 +51,7 @@ const DEFAULT_MAX_DEPTH = 10;
|
|
|
49
51
|
* - filterKeys: Array of keys to exclude
|
|
50
52
|
* - sortKeys: Custom key sorting configuration
|
|
51
53
|
* - maxDepth: Maximum nesting depth (default: 10)
|
|
54
|
+
* - keepOriginPrototype: Whether to keep the original prototype (default: false)
|
|
52
55
|
* @param depth - Current recursion depth (internal use)
|
|
53
56
|
*/
|
|
54
57
|
export const normalizer = (): ((
|
|
@@ -128,6 +131,16 @@ export const normalizer = (): ((
|
|
|
128
131
|
keys.sort(options.sortKeys.sortFn);
|
|
129
132
|
}
|
|
130
133
|
|
|
134
|
+
/**
|
|
135
|
+
* There is several cases, when we have to keep the origin prototype.
|
|
136
|
+
* For example, if normalizer is executing implicitly
|
|
137
|
+
* and normalized object will be used in `instanceof` condition
|
|
138
|
+
* next to execution context
|
|
139
|
+
*/
|
|
140
|
+
const normalized = options?.keepOriginPrototype
|
|
141
|
+
? Object.setPrototypeOf({}, Object.getPrototypeOf(xObject))
|
|
142
|
+
: {};
|
|
143
|
+
|
|
131
144
|
return keys.reduce((acc: Record<string, unknown>, key) => {
|
|
132
145
|
if (options?.filterKeys?.includes(key)) {
|
|
133
146
|
return acc;
|
|
@@ -136,7 +149,7 @@ export const normalizer = (): ((
|
|
|
136
149
|
acc[key] = normalize((xObject as any)[key], options, newDepth);
|
|
137
150
|
|
|
138
151
|
return acc;
|
|
139
|
-
},
|
|
152
|
+
}, normalized);
|
|
140
153
|
} catch (error) {
|
|
141
154
|
return `[Can not get keys from object. ${error}]`;
|
|
142
155
|
}
|