@rsdk/common 4.0.0-next.3 → 4.0.0-next.9

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 CHANGED
@@ -3,6 +3,14 @@
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.9](https://github.com/R-Vision/rsdk/compare/v4.0.0-next.8...v4.0.0-next.9) (2023-11-29)
7
+
8
+ **Note:** Version bump only for package @rsdk/common
9
+
10
+ ## [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)
11
+
12
+ **Note:** Version bump only for package @rsdk/common
13
+
6
14
  ## [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)
7
15
 
8
16
  ### Features
@@ -1,3 +1,4 @@
1
1
  export * from './misc';
2
2
  export * from './obj-normalization';
3
3
  export * from './delay';
4
+ export { isSerializable } from './serializable.type';
@@ -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":";;;;;;;;;;;;;;;;AAAA,yCAAuB;AACvB,sDAAoC;AACpC,0CAAwB"}
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"}
@@ -1,5 +1,5 @@
1
1
  export interface SortKeys {
2
- match: (x: unknown) => boolean;
2
+ match: (x: object) => boolean;
3
3
  sortFn: (a: string, b: string) => number;
4
4
  }
5
5
  export interface NormalizeOptions {
@@ -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 types_1 = require("../types");
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 = (x, options, depth = 0) => {
20
- if (x === null || x === undefined || (0, types_1.isPrimitive)(x)) {
21
- return x;
19
+ const normalize = (x1, options, depth = 0) => {
20
+ if ((0, serializable_type_1.isSerializable)(x1)) {
21
+ return x1;
22
22
  }
23
- if (cache.has(x)) {
23
+ const xObject = x1;
24
+ if (cache.has(xObject)) {
24
25
  return '[circular]';
25
26
  }
26
27
  else {
27
- cache.add(x);
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
- const unique = new Set(Object.keys(x));
36
- if (x instanceof Error) {
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(x)) {
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(x[key], options, newDepth);
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,oCAAuC;AAavC,MAAM,iBAAiB,GAAG,EAAE,CAAC;AAE7B;;;;;;;;;;GAUG;AACI,MAAM,UAAU,GAAG,GAIZ,EAAE;IACd,MAAM,KAAK,GAAG,IAAI,GAAG,EAAE,CAAC;IAExB,MAAM,SAAS,GAAG,CAChB,CAAU,EACV,OAA0B,EAC1B,KAAK,GAAG,CAAC,EACA,EAAE;QACX,IAAI,CAAC,KAAK,IAAI,IAAI,CAAC,KAAK,SAAS,IAAI,IAAA,mBAAW,EAAC,CAAC,CAAC,EAAE;YACnD,OAAO,CAAC,CAAC;SACV;QAED,IAAI,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE;YAChB,OAAO,YAAY,CAAC;SACrB;aAAM;YACL,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;SACd;QAED,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE;YACpB,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,SAAS,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC;SAC5C;QAED,IAAI,KAAK,IAAI,CAAC,OAAO,EAAE,QAAQ,IAAI,iBAA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E,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"}
@@ -0,0 +1,2 @@
1
+ export type Serializable = number | string | boolean | Date | null | undefined;
2
+ export declare const isSerializable: (x: any) => x is Serializable;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(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": "4.0.0-next.3",
3
+ "version": "4.0.0-next.9",
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": "313c39a379ea9ace9d94939223a53c46c12d77f9"
19
+ "gitHead": "889228ebf4076fafc1c7fce4406b1ea5a3d985ff"
20
20
  }
@@ -1,3 +1,4 @@
1
1
  export * from './misc';
2
2
  export * from './obj-normalization';
3
3
  export * from './delay';
4
+ export { isSerializable } from './serializable.type';
@@ -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 { isPrimitive } from '../types';
1
+ import { isSerializable } from './serializable.type';
2
2
 
3
3
  export interface SortKeys {
4
- match: (x: unknown) => boolean;
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
- x: unknown,
35
+ x1: unknown,
36
36
  options?: NormalizeOptions,
37
37
  depth = 0,
38
38
  ): unknown => {
39
- if (x === null || x === undefined || isPrimitive(x)) {
40
- return x;
39
+ if (isSerializable(x1)) {
40
+ return x1;
41
41
  }
42
42
 
43
- if (cache.has(x)) {
43
+ const xObject = x1 as object;
44
+
45
+ if (cache.has(xObject)) {
44
46
  return '[circular]';
45
47
  } else {
46
- cache.add(x);
48
+ cache.add(xObject);
47
49
  }
48
50
 
49
- if (Array.isArray(x)) {
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
- const unique = new Set(Object.keys(x));
57
+ if (Array.isArray(xObject)) {
58
+ return xObject.map((x) => normalize(x, options, newDepth));
59
+ }
58
60
 
59
- if (x instanceof Error) {
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(x)) {
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((x as any)[key], options, newDepth);
81
+ acc[key] = normalize((xObject as any)[key], options, newDepth);
80
82
 
81
83
  return acc;
82
84
  }, {});
@@ -0,0 +1,6 @@
1
+ export type Serializable = number | string | boolean | Date | null | undefined;
2
+
3
+ export const isSerializable = (x: any): x is Serializable =>
4
+ x instanceof Date ||
5
+ ['string', 'number', 'boolean'].includes(typeof x) ||
6
+ [null, undefined].includes(x);