@etsoo/shared 1.1.58 → 1.1.59

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
@@ -131,6 +131,7 @@ Data type definitions and type safe functions. ListItemType and ListItemType1 ar
131
131
  |isBasicName|Check the type is a basic type or not (type guard)|
132
132
  |isSimpleObject|Is the target a simple object, all values are simple type (Type guard)|
133
133
  |isSimpleType|Is the input value simple type, include null and undefined|
134
+ |jsonReplacer|JSON.stringify replacer with full path|
134
135
 
135
136
 
136
137
  ## 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
+ });
@@ -339,6 +339,11 @@ export declare namespace DataTypes {
339
339
  * @param includeArray Is array included, first non null element shoud also be basic type
340
340
  */
341
341
  function isSimpleType(input: unknown, includeArray?: boolean): boolean;
342
+ /**
343
+ * JSON.stringify replacer with full path
344
+ * https://stackoverflow.com/questions/61681176/json-stringify-replacer-how-to-get-full-path
345
+ */
346
+ function jsonReplacer(replacer: (this: any, key: string, value: any, path: string) => any): (this: any, key: any, value: any) => any;
342
347
  }
343
348
  /**
344
349
  * 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 = {}));
@@ -339,6 +339,11 @@ export declare namespace DataTypes {
339
339
  * @param includeArray Is array included, first non null element shoud also be basic type
340
340
  */
341
341
  function isSimpleType(input: unknown, includeArray?: boolean): boolean;
342
+ /**
343
+ * JSON.stringify replacer with full path
344
+ * https://stackoverflow.com/questions/61681176/json-stringify-replacer-how-to-get-full-path
345
+ */
346
+ function jsonReplacer(replacer: (this: any, key: string, value: any, path: string) => any): (this: any, key: any, value: any) => any;
342
347
  }
343
348
  /**
344
349
  * 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.59",
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",
57
+ "@types/jest": "^29.1.1",
58
+ "@typescript-eslint/eslint-plugin": "^5.38.1",
59
+ "@typescript-eslint/parser": "^5.38.1",
60
60
  "eslint": "^8.24.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
@@ -667,6 +667,29 @@ export namespace DataTypes {
667
667
 
668
668
  return true;
669
669
  }
670
+
671
+ /**
672
+ * JSON.stringify replacer with full path
673
+ * https://stackoverflow.com/questions/61681176/json-stringify-replacer-how-to-get-full-path
674
+ */
675
+ export function jsonReplacer(
676
+ replacer: (this: any, key: string, value: any, path: string) => any
677
+ ) {
678
+ const m = new Map();
679
+
680
+ return function (this: any, key: any, value: any) {
681
+ const path =
682
+ m.get(this) + (Array.isArray(this) ? `[${key}]` : '.' + key);
683
+ if (value === Object(value)) m.set(value, path);
684
+
685
+ return replacer.call(
686
+ this,
687
+ key,
688
+ value,
689
+ path.replace(/undefined\.\.?/, '')
690
+ );
691
+ };
692
+ }
670
693
  }
671
694
 
672
695
  /**