@rsdk/common 3.5.0 → 4.0.0-next.11
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.
- package/CHANGELOG.md +30 -0
- package/dist/functions/index.d.ts +1 -0
- package/dist/functions/index.js +3 -0
- package/dist/functions/index.js.map +1 -1
- package/dist/functions/misc.js +1 -1
- package/dist/functions/misc.js.map +1 -1
- package/dist/functions/obj-normalization.d.ts +1 -1
- package/dist/functions/obj-normalization.js +15 -14
- package/dist/functions/obj-normalization.js.map +1 -1
- package/dist/functions/serializable.type.d.ts +2 -0
- package/dist/functions/serializable.type.js +8 -0
- package/dist/functions/serializable.type.js.map +1 -0
- package/package.json +5 -2
- package/src/functions/index.ts +1 -0
- package/src/functions/misc.spec.ts +56 -0
- package/src/functions/misc.ts +1 -1
- package/src/functions/obj-normalization.spec.ts +199 -0
- package/src/functions/obj-normalization.ts +18 -16
- package/src/functions/serializable.type.ts +6 -0
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,36 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [4.0.0-next.11](https://github.com/R-Vision/rsdk/compare/v4.0.0-next.10...v4.0.0-next.11) (2023-11-29)
|
|
7
|
+
|
|
8
|
+
**Note:** Version bump only for package @rsdk/common
|
|
9
|
+
|
|
10
|
+
## [4.0.0-next.10](https://github.com/R-Vision/rsdk/compare/v4.0.0-next.9...v4.0.0-next.10) (2023-11-29)
|
|
11
|
+
|
|
12
|
+
**Note:** Version bump only for package @rsdk/common
|
|
13
|
+
|
|
14
|
+
## [4.0.0-next.9](https://github.com/R-Vision/rsdk/compare/v4.0.0-next.8...v4.0.0-next.9) (2023-11-29)
|
|
15
|
+
|
|
16
|
+
**Note:** Version bump only for package @rsdk/common
|
|
17
|
+
|
|
18
|
+
## [4.0.0-next.6](https://github.com/R-Vision/rsdk/compare/v4.0.0-next.5...v4.0.0-next.6) (2023-11-21)
|
|
19
|
+
|
|
20
|
+
**Note:** Version bump only for package @rsdk/common
|
|
21
|
+
|
|
22
|
+
## [4.0.0-next.3](https://github.com/R-Vision/rsdk/compare/v4.0.0-next.2...v4.0.0-next.3) (2023-11-12)
|
|
23
|
+
|
|
24
|
+
### Features
|
|
25
|
+
|
|
26
|
+
* common: Added tests for the isRecord method ([02dc7ce](https://github.com/R-Vision/rsdk/commit/02dc7ce2a8edaac8bec9e63e4200d55a4987ced5))
|
|
27
|
+
|
|
28
|
+
### Bug Fixes
|
|
29
|
+
|
|
30
|
+
* lint ([a16e4e9](https://github.com/R-Vision/rsdk/commit/a16e4e91b77474226209a9ac15dcfa4abe724283))
|
|
31
|
+
|
|
32
|
+
## [4.0.0-next.2](https://github.com/R-Vision/rsdk/compare/v4.0.0-next.1...v4.0.0-next.2) (2023-11-10)
|
|
33
|
+
|
|
34
|
+
**Note:** Version bump only for package @rsdk/common
|
|
35
|
+
|
|
6
36
|
## [3.5.0](https://github.com/R-Vision/rsdk/compare/v3.5.0-next.9...v3.5.0) (2023-10-03)
|
|
7
37
|
|
|
8
38
|
**Note:** Version bump only for package @rsdk/common
|
package/dist/functions/index.js
CHANGED
|
@@ -14,7 +14,10 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.isSerializable = void 0;
|
|
17
18
|
__exportStar(require("./misc"), exports);
|
|
18
19
|
__exportStar(require("./obj-normalization"), exports);
|
|
19
20
|
__exportStar(require("./delay"), exports);
|
|
21
|
+
var serializable_type_1 = require("./serializable.type");
|
|
22
|
+
Object.defineProperty(exports, "isSerializable", { enumerable: true, get: function () { return serializable_type_1.isSerializable; } });
|
|
20
23
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/functions/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/functions/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,yCAAuB;AACvB,sDAAoC;AACpC,0CAAwB;AACxB,yDAAqD;AAA5C,mHAAA,cAAc,OAAA"}
|
package/dist/functions/misc.js
CHANGED
|
@@ -14,7 +14,7 @@ exports.noop = exports.isOneOf = exports.hasKey = exports.isUndefined = exports.
|
|
|
14
14
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/no-empty-function
|
|
15
15
|
const unreachable = (_value) => { };
|
|
16
16
|
exports.unreachable = unreachable;
|
|
17
|
-
const isRecord = (x) =>
|
|
17
|
+
const isRecord = (x) => !!x && x.constructor === Object;
|
|
18
18
|
exports.isRecord = isRecord;
|
|
19
19
|
const isUndefined = (value) => value === undefined;
|
|
20
20
|
exports.isUndefined = isUndefined;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"misc.js","sourceRoot":"","sources":["../../src/functions/misc.ts"],"names":[],"mappings":";;;AAEA;;;;;;;;;GASG;AACH,mGAAmG;AAC5F,MAAM,WAAW,GAAG,CAAC,MAAa,EAAO,EAAE,GAAE,CAAC,CAAC;AAAzC,QAAA,WAAW,eAA8B;AAE/C,MAAM,QAAQ,GAAG,CAAC,CAAU,EAAgC,EAAE,CACnE,
|
|
1
|
+
{"version":3,"file":"misc.js","sourceRoot":"","sources":["../../src/functions/misc.ts"],"names":[],"mappings":";;;AAEA;;;;;;;;;GASG;AACH,mGAAmG;AAC5F,MAAM,WAAW,GAAG,CAAC,MAAa,EAAO,EAAE,GAAE,CAAC,CAAC;AAAzC,QAAA,WAAW,eAA8B;AAE/C,MAAM,QAAQ,GAAG,CAAC,CAAU,EAAgC,EAAE,CACnE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,WAAW,KAAK,MAAM,CAAC;AADrB,QAAA,QAAQ,YACa;AAE3B,MAAM,WAAW,GAAG,CAAI,KAAoB,EAAc,EAAE,CACjE,KAAK,KAAK,SAAS,CAAC;AADT,QAAA,WAAW,eACF;AAEf,MAAM,MAAM,GAAG,CAAmB,GAAM,EAAE,GAAY,EAAW,EAAE,CACxE,GAAG,IAAI,GAAG,CAAC;AADA,QAAA,MAAM,UACN;AAEN,MAAM,OAAO,GAAG,CAAC,KAAc,EAAE,KAAoB,EAAW,EAAE,CACvE,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,KAAK,YAAY,IAAI,CAAC,CAAC;AADjC,QAAA,OAAO,WAC0B;AAE9C,gEAAgE;AACzD,MAAM,IAAI,GAAG,GAAS,EAAE,GAAE,CAAC,CAAC;AAAtB,QAAA,IAAI,QAAkB"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.sortErrorKeys = exports.normalizer = void 0;
|
|
4
|
-
const
|
|
4
|
+
const serializable_type_1 = require("./serializable.type");
|
|
5
5
|
const DEFAULT_MAX_DEPTH = 10;
|
|
6
6
|
/**
|
|
7
7
|
* Prepares object to be stringified with JSON.stringify()
|
|
@@ -16,39 +16,40 @@ const DEFAULT_MAX_DEPTH = 10;
|
|
|
16
16
|
*/
|
|
17
17
|
const normalizer = () => {
|
|
18
18
|
const cache = new Set();
|
|
19
|
-
const normalize = (
|
|
20
|
-
if (
|
|
21
|
-
return
|
|
19
|
+
const normalize = (x1, options, depth = 0) => {
|
|
20
|
+
if ((0, serializable_type_1.isSerializable)(x1)) {
|
|
21
|
+
return x1;
|
|
22
22
|
}
|
|
23
|
-
|
|
23
|
+
const xObject = x1;
|
|
24
|
+
if (cache.has(xObject)) {
|
|
24
25
|
return '[circular]';
|
|
25
26
|
}
|
|
26
27
|
else {
|
|
27
|
-
cache.add(
|
|
28
|
-
}
|
|
29
|
-
if (Array.isArray(x)) {
|
|
30
|
-
return x.map((x) => normalize(x, options));
|
|
28
|
+
cache.add(xObject);
|
|
31
29
|
}
|
|
30
|
+
const newDepth = depth + 1;
|
|
32
31
|
if (depth >= (options?.maxDepth ?? DEFAULT_MAX_DEPTH)) {
|
|
33
32
|
return '[max nesting depth reached]';
|
|
34
33
|
}
|
|
35
|
-
|
|
36
|
-
|
|
34
|
+
if (Array.isArray(xObject)) {
|
|
35
|
+
return xObject.map((x) => normalize(x, options, newDepth));
|
|
36
|
+
}
|
|
37
|
+
const unique = new Set(Object.keys(xObject));
|
|
38
|
+
if (xObject instanceof Error) {
|
|
37
39
|
unique.add('cause');
|
|
38
40
|
unique.add('stack');
|
|
39
41
|
unique.add('message');
|
|
40
42
|
}
|
|
41
43
|
const keys = [...unique];
|
|
42
44
|
// eslint-disable-next-line unicorn/prefer-regexp-test
|
|
43
|
-
if (options?.sortKeys && options.sortKeys.match(
|
|
45
|
+
if (options?.sortKeys && options.sortKeys.match(xObject)) {
|
|
44
46
|
keys.sort(options.sortKeys.sortFn);
|
|
45
47
|
}
|
|
46
|
-
const newDepth = depth + 1;
|
|
47
48
|
return keys.reduce((acc, key) => {
|
|
48
49
|
if (options?.filterKeys && options.filterKeys.includes(key)) {
|
|
49
50
|
return acc;
|
|
50
51
|
}
|
|
51
|
-
acc[key] = normalize(
|
|
52
|
+
acc[key] = normalize(xObject[key], options, newDepth);
|
|
52
53
|
return acc;
|
|
53
54
|
}, {});
|
|
54
55
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"obj-normalization.js","sourceRoot":"","sources":["../../src/functions/obj-normalization.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"obj-normalization.js","sourceRoot":"","sources":["../../src/functions/obj-normalization.ts"],"names":[],"mappings":";;;AAAA,2DAAqD;AAarD,MAAM,iBAAiB,GAAG,EAAE,CAAC;AAE7B;;;;;;;;;;GAUG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Property(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isSerializable = void 0;
|
|
4
|
+
const isSerializable = (x) => x instanceof Date ||
|
|
5
|
+
['string', 'number', 'boolean'].includes(typeof x) ||
|
|
6
|
+
[null, undefined].includes(x);
|
|
7
|
+
exports.isSerializable = isSerializable;
|
|
8
|
+
//# sourceMappingURL=serializable.type.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"serializable.type.js","sourceRoot":"","sources":["../../src/functions/serializable.type.ts"],"names":[],"mappings":";;;AAEO,MAAM,cAAc,GAAG,CAAC,CAAM,EAAqB,EAAE,CAC1D,CAAC,YAAY,IAAI;IACjB,CAAC,QAAQ,EAAE,QAAQ,EAAE,SAAS,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;IAClD,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;AAHnB,QAAA,cAAc,kBAGK"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rsdk/common",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0-next.11",
|
|
4
4
|
"description": "Useful common classes, functions and types",
|
|
5
5
|
"license": "Apache License 2.0",
|
|
6
6
|
"publishConfig": {
|
|
@@ -13,5 +13,8 @@
|
|
|
13
13
|
"scripts": {
|
|
14
14
|
"prepublishOnly": "npm run build"
|
|
15
15
|
},
|
|
16
|
-
"
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"rxjs": "^7.8.1"
|
|
18
|
+
},
|
|
19
|
+
"gitHead": "f1339ae22d78b41382b82a0feabacd49be19acd2"
|
|
17
20
|
}
|
package/src/functions/index.ts
CHANGED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-array-constructor */
|
|
2
|
+
/* eslint-disable unicorn/new-for-builtins */
|
|
3
|
+
/* eslint-disable prettier/prettier */
|
|
4
|
+
|
|
5
|
+
import { isRecord } from './misc';
|
|
6
|
+
|
|
7
|
+
/* eslint-disable sort-keys */
|
|
8
|
+
describe('misc', () => {
|
|
9
|
+
describe('isRecord', () => {
|
|
10
|
+
interface Case {
|
|
11
|
+
name: string;
|
|
12
|
+
input: any;
|
|
13
|
+
success: boolean;
|
|
14
|
+
output: boolean;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const tt: Case[] = [
|
|
18
|
+
// Positive
|
|
19
|
+
{ name: 'Correct object (literal)', input: {}, success: true, output: true },
|
|
20
|
+
{ name: 'Correct object (Object())', input: new Object(), success: true, output: true },
|
|
21
|
+
{ name: 'Correct object (new Object())', input: new Object(), success: true, output: true },
|
|
22
|
+
{ name: 'Correct object (Object.create())', input: Object.create({}), success: true, output: true },
|
|
23
|
+
{ name: 'Correct object (Object.assign())', input: Object.assign({}, {}), success: true, output: true },
|
|
24
|
+
|
|
25
|
+
{ name: 'Function (declarative)', input: function t(): void {}, success: true, output: false },
|
|
26
|
+
{ name: 'new Function() instance', input: new Function(), success: true, output: false },
|
|
27
|
+
{ name: 'Function constructor', input: new (function t() {})(), success: true, output: false },
|
|
28
|
+
{ name: 'Class', input: class T {}, success: true, output: false },
|
|
29
|
+
{ name: 'Class constructor', input: new (class T{})(), success: true, output: false },
|
|
30
|
+
{ name: 'Array (literal)', input: [], success: true, output: false },
|
|
31
|
+
{ name: 'Array() instance', input: Array(), success: true, output: false },
|
|
32
|
+
{ name: 'new Array() instance', input: new Array(), success: true, output: false },
|
|
33
|
+
{ name: 'The Map (new Map())', input: new Map(), success: true, output: false },
|
|
34
|
+
{ name: 'The Set (new Set())', input: new Set(), success: true, output: false },
|
|
35
|
+
{ name: 'Date object', input: new Date(), success: true, output: false },
|
|
36
|
+
{ name: 'String oject', input: new String('str'), success: true, output: false },
|
|
37
|
+
{ name: 'Number object', input: new Number(1), success: true, output: false },
|
|
38
|
+
{ name: 'Boolean object', input: new Boolean(true), success: true, output: false },
|
|
39
|
+
{ name: 'BigInt', input: BigInt(999999999999), success: true, output: false },
|
|
40
|
+
{ name: 'Symbol', input: Symbol(), success: true, output: false },
|
|
41
|
+
{ name: 'null', input: null, success: true, output: false },
|
|
42
|
+
{ name: 'undefined', input: undefined, success: true, output: false },
|
|
43
|
+
{ name: 'true', input: true, success: true, output: false },
|
|
44
|
+
{ name: 'number', input: 12, success: true, output: false },
|
|
45
|
+
{ name: 'string', input: 'str', success: true, output: false },
|
|
46
|
+
];
|
|
47
|
+
|
|
48
|
+
for (const { name, input, success, output } of tt) {
|
|
49
|
+
if (success) {
|
|
50
|
+
it(name, () => expect(isRecord(input)).toBe(output));
|
|
51
|
+
} else {
|
|
52
|
+
it(name, () => expect(() => isRecord(input)).toThrow());
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
});
|
package/src/functions/misc.ts
CHANGED
|
@@ -14,7 +14,7 @@ import type { Constructor } from '../types';
|
|
|
14
14
|
export const unreachable = (_value: never): any => {};
|
|
15
15
|
|
|
16
16
|
export const isRecord = (x: unknown): x is Record<string, unknown> =>
|
|
17
|
-
|
|
17
|
+
!!x && x.constructor === Object;
|
|
18
18
|
|
|
19
19
|
export const isUndefined = <T>(value: T | undefined): value is T =>
|
|
20
20
|
value === undefined;
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import type { NormalizeOptions, SortKeys } from './obj-normalization';
|
|
2
|
+
import { normalizer } from './obj-normalization';
|
|
3
|
+
|
|
4
|
+
describe('should serialize date', () => {
|
|
5
|
+
it.each`
|
|
6
|
+
input | output
|
|
7
|
+
${new Date()} | ${new Date()}
|
|
8
|
+
${{ d: new Date() }} | ${{ d: new Date() }}
|
|
9
|
+
`('$input to be $output', ({ input, output }) => {
|
|
10
|
+
const result = normalizer()(input);
|
|
11
|
+
|
|
12
|
+
expect(result).toEqual(output);
|
|
13
|
+
});
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
describe('should handle circular', () => {
|
|
17
|
+
const a: any = {};
|
|
18
|
+
|
|
19
|
+
a.a = a;
|
|
20
|
+
|
|
21
|
+
const b: any = { a };
|
|
22
|
+
|
|
23
|
+
b.b = b;
|
|
24
|
+
|
|
25
|
+
const c: any = { a: { b: {} } };
|
|
26
|
+
|
|
27
|
+
c.a.b.c = c;
|
|
28
|
+
|
|
29
|
+
const d: any = {};
|
|
30
|
+
|
|
31
|
+
d.d = [a, b, c, d];
|
|
32
|
+
|
|
33
|
+
it.each`
|
|
34
|
+
input | output
|
|
35
|
+
${a} | ${{ a: '[circular]' }}
|
|
36
|
+
${b} | ${{ a: { a: '[circular]' }, b: '[circular]' }}
|
|
37
|
+
${c} | ${{ a: { b: { c: '[circular]' } } }}
|
|
38
|
+
${d} | ${{ d: [{ a: '[circular]' }, { a: '[circular]', b: '[circular]' }, { a: { b: { c: '[circular]' } } }, '[circular]'] }}
|
|
39
|
+
`('$input to be $output', ({ input, output }) => {
|
|
40
|
+
expect(normalizer()(input)).toEqual(output);
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
describe('should sort keys', () => {
|
|
45
|
+
// eslint-disable-next-line unicorn/consistent-function-scoping
|
|
46
|
+
const sortDesc: SortKeys['sortFn'] = (a, b) => b.localeCompare(a);
|
|
47
|
+
// eslint-disable-next-line unicorn/consistent-function-scoping
|
|
48
|
+
const sortAsc: SortKeys['sortFn'] = (a, b) => a.localeCompare(b);
|
|
49
|
+
const tt: {
|
|
50
|
+
opts: NormalizeOptions;
|
|
51
|
+
input: any;
|
|
52
|
+
output: any;
|
|
53
|
+
}[] = [
|
|
54
|
+
{
|
|
55
|
+
input: { b: 1, a: 1 },
|
|
56
|
+
opts: {
|
|
57
|
+
sortKeys: { sortFn: sortAsc, match: () => true },
|
|
58
|
+
},
|
|
59
|
+
output: { a: 1, b: 1 },
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
input: { b: 1, a: 1 },
|
|
63
|
+
opts: {
|
|
64
|
+
sortKeys: { sortFn: sortAsc, match: () => false },
|
|
65
|
+
},
|
|
66
|
+
output: { b: 1, a: 1 },
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
input: { a: 1, b: 1 },
|
|
70
|
+
opts: {
|
|
71
|
+
sortKeys: { sortFn: sortDesc, match: () => true },
|
|
72
|
+
},
|
|
73
|
+
output: { b: 1, a: 1 },
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
input: { a: 1, b: { d: 1, c: 1 } },
|
|
77
|
+
opts: {
|
|
78
|
+
sortKeys: { sortFn: sortAsc, match: () => true },
|
|
79
|
+
},
|
|
80
|
+
output: { a: 1, b: { c: 1, d: 1 } },
|
|
81
|
+
},
|
|
82
|
+
{
|
|
83
|
+
input: { a: 1, b: { e: 1, d: 1, c: 1 } },
|
|
84
|
+
opts: {
|
|
85
|
+
sortKeys: {
|
|
86
|
+
sortFn: sortAsc,
|
|
87
|
+
match: (x) => Object.keys(x).length < 3,
|
|
88
|
+
},
|
|
89
|
+
},
|
|
90
|
+
output: { a: 1, b: { e: 1, d: 1, c: 1 } },
|
|
91
|
+
},
|
|
92
|
+
{
|
|
93
|
+
input: { a: 1, b: [{ c: 1, e: 1, d: 1 }] },
|
|
94
|
+
opts: {
|
|
95
|
+
sortKeys: {
|
|
96
|
+
sortFn: sortDesc,
|
|
97
|
+
match: () => true,
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
output: { b: [{ e: 1, d: 1, c: 1 }], a: 1 },
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
input: { a: 1, b: [{ e: 1, d: 1, c: 1 }] },
|
|
104
|
+
opts: {
|
|
105
|
+
sortKeys: {
|
|
106
|
+
sortFn: sortAsc,
|
|
107
|
+
match: (x) => Object.keys(x).length < 3,
|
|
108
|
+
},
|
|
109
|
+
},
|
|
110
|
+
output: { a: 1, b: [{ e: 1, d: 1, c: 1 }] },
|
|
111
|
+
},
|
|
112
|
+
];
|
|
113
|
+
|
|
114
|
+
it.each(tt)('$input to be $output', ({ input, opts, output }) => {
|
|
115
|
+
expect(JSON.stringify(normalizer()(input, opts))).toEqual(
|
|
116
|
+
JSON.stringify(output),
|
|
117
|
+
);
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
describe('max depth', () => {
|
|
122
|
+
const tt: {
|
|
123
|
+
input: any;
|
|
124
|
+
output: any;
|
|
125
|
+
options: NormalizeOptions;
|
|
126
|
+
title: string;
|
|
127
|
+
}[] = [
|
|
128
|
+
{
|
|
129
|
+
title: 'classic case',
|
|
130
|
+
input: {
|
|
131
|
+
a: {
|
|
132
|
+
b: {
|
|
133
|
+
c: {},
|
|
134
|
+
},
|
|
135
|
+
},
|
|
136
|
+
},
|
|
137
|
+
options: {
|
|
138
|
+
maxDepth: 2,
|
|
139
|
+
},
|
|
140
|
+
output: {
|
|
141
|
+
a: {
|
|
142
|
+
b: '[max nesting depth reached]',
|
|
143
|
+
},
|
|
144
|
+
},
|
|
145
|
+
},
|
|
146
|
+
{
|
|
147
|
+
title: 'maxdepth with array',
|
|
148
|
+
input: {
|
|
149
|
+
a: {
|
|
150
|
+
b: {
|
|
151
|
+
c: [{}],
|
|
152
|
+
},
|
|
153
|
+
},
|
|
154
|
+
},
|
|
155
|
+
options: {
|
|
156
|
+
maxDepth: 2,
|
|
157
|
+
},
|
|
158
|
+
output: {
|
|
159
|
+
a: {
|
|
160
|
+
b: '[max nesting depth reached]',
|
|
161
|
+
},
|
|
162
|
+
},
|
|
163
|
+
},
|
|
164
|
+
{
|
|
165
|
+
input: {
|
|
166
|
+
a1: {
|
|
167
|
+
a2: {
|
|
168
|
+
a3: {
|
|
169
|
+
a4: { a5: { a6: { a7: { a8: { a9: { a10: { a11: {} } } } } } } },
|
|
170
|
+
},
|
|
171
|
+
},
|
|
172
|
+
},
|
|
173
|
+
},
|
|
174
|
+
options: {},
|
|
175
|
+
title: 'default max depth must be 10',
|
|
176
|
+
output: {
|
|
177
|
+
a1: {
|
|
178
|
+
a2: {
|
|
179
|
+
a3: {
|
|
180
|
+
a4: {
|
|
181
|
+
a5: {
|
|
182
|
+
a6: {
|
|
183
|
+
a7: { a8: { a9: { a10: '[max nesting depth reached]' } } },
|
|
184
|
+
},
|
|
185
|
+
},
|
|
186
|
+
},
|
|
187
|
+
},
|
|
188
|
+
},
|
|
189
|
+
},
|
|
190
|
+
},
|
|
191
|
+
},
|
|
192
|
+
];
|
|
193
|
+
|
|
194
|
+
it.each(tt)('$title', ({ input, options, output }) => {
|
|
195
|
+
expect(JSON.stringify(normalizer()(input, options))).toEqual(
|
|
196
|
+
JSON.stringify(output),
|
|
197
|
+
);
|
|
198
|
+
});
|
|
199
|
+
});
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { isSerializable } from './serializable.type';
|
|
2
2
|
|
|
3
3
|
export interface SortKeys {
|
|
4
|
-
match: (x:
|
|
4
|
+
match: (x: object) => boolean;
|
|
5
5
|
sortFn: (a: string, b: string) => number;
|
|
6
6
|
}
|
|
7
7
|
|
|
@@ -32,31 +32,35 @@ export const normalizer = (): ((
|
|
|
32
32
|
const cache = new Set();
|
|
33
33
|
|
|
34
34
|
const normalize = (
|
|
35
|
-
|
|
35
|
+
x1: unknown,
|
|
36
36
|
options?: NormalizeOptions,
|
|
37
37
|
depth = 0,
|
|
38
38
|
): unknown => {
|
|
39
|
-
if (
|
|
40
|
-
return
|
|
39
|
+
if (isSerializable(x1)) {
|
|
40
|
+
return x1;
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
-
|
|
43
|
+
const xObject = x1 as object;
|
|
44
|
+
|
|
45
|
+
if (cache.has(xObject)) {
|
|
44
46
|
return '[circular]';
|
|
45
47
|
} else {
|
|
46
|
-
cache.add(
|
|
48
|
+
cache.add(xObject);
|
|
47
49
|
}
|
|
48
50
|
|
|
49
|
-
|
|
50
|
-
return x.map((x) => normalize(x, options));
|
|
51
|
-
}
|
|
51
|
+
const newDepth = depth + 1;
|
|
52
52
|
|
|
53
53
|
if (depth >= (options?.maxDepth ?? DEFAULT_MAX_DEPTH)) {
|
|
54
54
|
return '[max nesting depth reached]';
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
-
|
|
57
|
+
if (Array.isArray(xObject)) {
|
|
58
|
+
return xObject.map((x) => normalize(x, options, newDepth));
|
|
59
|
+
}
|
|
58
60
|
|
|
59
|
-
|
|
61
|
+
const unique = new Set(Object.keys(xObject));
|
|
62
|
+
|
|
63
|
+
if (xObject instanceof Error) {
|
|
60
64
|
unique.add('cause');
|
|
61
65
|
unique.add('stack');
|
|
62
66
|
unique.add('message');
|
|
@@ -65,18 +69,16 @@ export const normalizer = (): ((
|
|
|
65
69
|
const keys = [...unique];
|
|
66
70
|
|
|
67
71
|
// eslint-disable-next-line unicorn/prefer-regexp-test
|
|
68
|
-
if (options?.sortKeys && options.sortKeys.match(
|
|
72
|
+
if (options?.sortKeys && options.sortKeys.match(xObject)) {
|
|
69
73
|
keys.sort(options.sortKeys.sortFn);
|
|
70
74
|
}
|
|
71
75
|
|
|
72
|
-
const newDepth = depth + 1;
|
|
73
|
-
|
|
74
76
|
return keys.reduce((acc: Record<string, unknown>, key) => {
|
|
75
77
|
if (options?.filterKeys && options.filterKeys.includes(key)) {
|
|
76
78
|
return acc;
|
|
77
79
|
}
|
|
78
80
|
|
|
79
|
-
acc[key] = normalize((
|
|
81
|
+
acc[key] = normalize((xObject as any)[key], options, newDepth);
|
|
80
82
|
|
|
81
83
|
return acc;
|
|
82
84
|
}, {});
|