@etsoo/shared 1.1.58 → 1.1.60

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/README.md CHANGED
@@ -103,6 +103,7 @@ Data type definitions and type safe functions. ListItemType and ListItemType1 ar
103
103
  |IdNameItem|Item with id and name|
104
104
  |KeyCollection|Key collection, like { key1: {}, key2: {} }|
105
105
  |Keys|Get specific type keys|
106
+ |MConstructor|Mixins constructor|
106
107
  |ObjType|Generic object type|
107
108
  |RequireAtLeastOne|Require at least one property of the keys|
108
109
  |Simple|Basic or basic array type|
@@ -131,6 +132,7 @@ Data type definitions and type safe functions. ListItemType and ListItemType1 ar
131
132
  |isBasicName|Check the type is a basic type or not (type guard)|
132
133
  |isSimpleObject|Is the target a simple object, all values are simple type (Type guard)|
133
134
  |isSimpleType|Is the input value simple type, include null and undefined|
135
+ |jsonReplacer|JSON.stringify replacer with full path|
134
136
 
135
137
 
136
138
  ## DateUtils
@@ -154,3 +154,29 @@ test('Tests for IdDefaultType', () => {
154
154
  const v1 = test<D, LabelDefaultType<D>>(data, 'label');
155
155
  expect(v1).toBe('label');
156
156
  });
157
+
158
+ test('Tests for jsonReplacer', () => {
159
+ const obj = { a: 1, b: 'hello', c: { c1: 'C1', c2: false, c3: 128 } };
160
+
161
+ const json1 = JSON.stringify(
162
+ obj,
163
+ DataTypes.jsonReplacer(function (key, value, path) {
164
+ if (['', 'a'].includes(key)) {
165
+ return value;
166
+ }
167
+ return undefined;
168
+ })
169
+ );
170
+ expect(json1).toBe('{"a":1}');
171
+
172
+ const json2 = JSON.stringify(
173
+ obj,
174
+ DataTypes.jsonReplacer(function (key, value, path) {
175
+ if (['', 'c'].includes(key) || path === 'c.c2') {
176
+ return value;
177
+ }
178
+ return undefined;
179
+ })
180
+ );
181
+ expect(json2).toBe('{"c":{"c2":false}}');
182
+ });
@@ -143,6 +143,10 @@ export declare namespace DataTypes {
143
143
  * Function type
144
144
  */
145
145
  type Func<R> = (...args: any[]) => R;
146
+ /**
147
+ * Mixins constructor
148
+ */
149
+ type MConstructor<T = {}> = new (...args: any[]) => T;
146
150
  /**
147
151
  * String key, unknown value Record
148
152
  */
@@ -339,6 +343,11 @@ export declare namespace DataTypes {
339
343
  * @param includeArray Is array included, first non null element shoud also be basic type
340
344
  */
341
345
  function isSimpleType(input: unknown, includeArray?: boolean): boolean;
346
+ /**
347
+ * JSON.stringify replacer with full path
348
+ * https://stackoverflow.com/questions/61681176/json-stringify-replacer-how-to-get-full-path
349
+ */
350
+ function jsonReplacer(replacer: (this: any, key: string, value: any, path: string) => any): (this: any, key: any, value: any) => any;
342
351
  }
343
352
  /**
344
353
  * List item with number id type
@@ -391,4 +391,18 @@ var DataTypes;
391
391
  return true;
392
392
  }
393
393
  DataTypes.isSimpleType = isSimpleType;
394
+ /**
395
+ * JSON.stringify replacer with full path
396
+ * https://stackoverflow.com/questions/61681176/json-stringify-replacer-how-to-get-full-path
397
+ */
398
+ function jsonReplacer(replacer) {
399
+ const m = new Map();
400
+ return function (key, value) {
401
+ const path = m.get(this) + (Array.isArray(this) ? `[${key}]` : '.' + key);
402
+ if (value === Object(value))
403
+ m.set(value, path);
404
+ return replacer.call(this, key, value, path.replace(/undefined\.\.?/, ''));
405
+ };
406
+ }
407
+ DataTypes.jsonReplacer = jsonReplacer;
394
408
  })(DataTypes = exports.DataTypes || (exports.DataTypes = {}));
@@ -143,6 +143,10 @@ export declare namespace DataTypes {
143
143
  * Function type
144
144
  */
145
145
  type Func<R> = (...args: any[]) => R;
146
+ /**
147
+ * Mixins constructor
148
+ */
149
+ type MConstructor<T = {}> = new (...args: any[]) => T;
146
150
  /**
147
151
  * String key, unknown value Record
148
152
  */
@@ -339,6 +343,11 @@ export declare namespace DataTypes {
339
343
  * @param includeArray Is array included, first non null element shoud also be basic type
340
344
  */
341
345
  function isSimpleType(input: unknown, includeArray?: boolean): boolean;
346
+ /**
347
+ * JSON.stringify replacer with full path
348
+ * https://stackoverflow.com/questions/61681176/json-stringify-replacer-how-to-get-full-path
349
+ */
350
+ function jsonReplacer(replacer: (this: any, key: string, value: any, path: string) => any): (this: any, key: any, value: any) => any;
342
351
  }
343
352
  /**
344
353
  * List item with number id type
@@ -388,4 +388,18 @@ export var DataTypes;
388
388
  return true;
389
389
  }
390
390
  DataTypes.isSimpleType = isSimpleType;
391
+ /**
392
+ * JSON.stringify replacer with full path
393
+ * https://stackoverflow.com/questions/61681176/json-stringify-replacer-how-to-get-full-path
394
+ */
395
+ function jsonReplacer(replacer) {
396
+ const m = new Map();
397
+ return function (key, value) {
398
+ const path = m.get(this) + (Array.isArray(this) ? `[${key}]` : '.' + key);
399
+ if (value === Object(value))
400
+ m.set(value, path);
401
+ return replacer.call(this, key, value, path.replace(/undefined\.\.?/, ''));
402
+ };
403
+ }
404
+ DataTypes.jsonReplacer = jsonReplacer;
391
405
  })(DataTypes || (DataTypes = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.1.58",
3
+ "version": "1.1.60",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -54,15 +54,15 @@
54
54
  },
55
55
  "homepage": "https://github.com/ETSOO/Shared#readme",
56
56
  "devDependencies": {
57
- "@types/jest": "^29.0.3",
58
- "@typescript-eslint/eslint-plugin": "^5.38.0",
59
- "@typescript-eslint/parser": "^5.38.0",
60
- "eslint": "^8.24.0",
57
+ "@types/jest": "^29.1.2",
58
+ "@typescript-eslint/eslint-plugin": "^5.40.0",
59
+ "@typescript-eslint/parser": "^5.40.0",
60
+ "eslint": "^8.25.0",
61
61
  "eslint-config-airbnb-base": "^15.0.0",
62
62
  "eslint-plugin-import": "^2.26.0",
63
- "jest": "^29.0.3",
64
- "jest-environment-jsdom": "^29.0.3",
65
- "ts-jest": "^29.0.2",
66
- "typescript": "^4.8.3"
63
+ "jest": "^29.1.2",
64
+ "jest-environment-jsdom": "^29.1.2",
65
+ "ts-jest": "^29.0.3",
66
+ "typescript": "^4.8.4"
67
67
  }
68
68
  }
package/src/DataTypes.ts CHANGED
@@ -184,6 +184,11 @@ export namespace DataTypes {
184
184
  */
185
185
  export type Func<R> = (...args: any[]) => R;
186
186
 
187
+ /**
188
+ * Mixins constructor
189
+ */
190
+ export type MConstructor<T = {}> = new (...args: any[]) => T;
191
+
187
192
  /**
188
193
  * String key, unknown value Record
189
194
  */
@@ -667,6 +672,29 @@ export namespace DataTypes {
667
672
 
668
673
  return true;
669
674
  }
675
+
676
+ /**
677
+ * JSON.stringify replacer with full path
678
+ * https://stackoverflow.com/questions/61681176/json-stringify-replacer-how-to-get-full-path
679
+ */
680
+ export function jsonReplacer(
681
+ replacer: (this: any, key: string, value: any, path: string) => any
682
+ ) {
683
+ const m = new Map();
684
+
685
+ return function (this: any, key: any, value: any) {
686
+ const path =
687
+ m.get(this) + (Array.isArray(this) ? `[${key}]` : '.' + key);
688
+ if (value === Object(value)) m.set(value, path);
689
+
690
+ return replacer.call(
691
+ this,
692
+ key,
693
+ value,
694
+ path.replace(/undefined\.\.?/, '')
695
+ );
696
+ };
697
+ }
670
698
  }
671
699
 
672
700
  /**