@etsoo/shared 1.1.57 → 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 +2 -0
- package/__tests__/DataTypes.ts +26 -0
- package/lib/cjs/DataTypes.d.ts +14 -0
- package/lib/cjs/DataTypes.js +14 -0
- package/lib/mjs/DataTypes.d.ts +14 -0
- package/lib/mjs/DataTypes.js +14 -0
- package/package.json +9 -9
- package/src/DataTypes.ts +33 -0
package/README.md
CHANGED
|
@@ -100,6 +100,7 @@ Data type definitions and type safe functions. ListItemType and ListItemType1 ar
|
|
|
100
100
|
|IdItem|Item with id or id generator|
|
|
101
101
|
|IdLabelItem|Item with id and label|
|
|
102
102
|
|IdLabelType|Item with id and label dynamic type|
|
|
103
|
+
|IdNameItem|Item with id and name|
|
|
103
104
|
|KeyCollection|Key collection, like { key1: {}, key2: {} }|
|
|
104
105
|
|Keys|Get specific type keys|
|
|
105
106
|
|ObjType|Generic object type|
|
|
@@ -130,6 +131,7 @@ Data type definitions and type safe functions. ListItemType and ListItemType1 ar
|
|
|
130
131
|
|isBasicName|Check the type is a basic type or not (type guard)|
|
|
131
132
|
|isSimpleObject|Is the target a simple object, all values are simple type (Type guard)|
|
|
132
133
|
|isSimpleType|Is the input value simple type, include null and undefined|
|
|
134
|
+
|jsonReplacer|JSON.stringify replacer with full path|
|
|
133
135
|
|
|
134
136
|
|
|
135
137
|
## DateUtils
|
package/__tests__/DataTypes.ts
CHANGED
|
@@ -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
|
+
});
|
package/lib/cjs/DataTypes.d.ts
CHANGED
|
@@ -173,6 +173,15 @@ export declare namespace DataTypes {
|
|
|
173
173
|
*/
|
|
174
174
|
label: string;
|
|
175
175
|
};
|
|
176
|
+
/**
|
|
177
|
+
* Item with id and name property
|
|
178
|
+
*/
|
|
179
|
+
type IdNameItem<T extends IdType = number> = IdItem<T> & {
|
|
180
|
+
/**
|
|
181
|
+
* name field
|
|
182
|
+
*/
|
|
183
|
+
name: string;
|
|
184
|
+
};
|
|
176
185
|
/**
|
|
177
186
|
* Item with id and label dynamic type
|
|
178
187
|
*/
|
|
@@ -330,6 +339,11 @@ export declare namespace DataTypes {
|
|
|
330
339
|
* @param includeArray Is array included, first non null element shoud also be basic type
|
|
331
340
|
*/
|
|
332
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;
|
|
333
347
|
}
|
|
334
348
|
/**
|
|
335
349
|
* List item with number id type
|
package/lib/cjs/DataTypes.js
CHANGED
|
@@ -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 = {}));
|
package/lib/mjs/DataTypes.d.ts
CHANGED
|
@@ -173,6 +173,15 @@ export declare namespace DataTypes {
|
|
|
173
173
|
*/
|
|
174
174
|
label: string;
|
|
175
175
|
};
|
|
176
|
+
/**
|
|
177
|
+
* Item with id and name property
|
|
178
|
+
*/
|
|
179
|
+
type IdNameItem<T extends IdType = number> = IdItem<T> & {
|
|
180
|
+
/**
|
|
181
|
+
* name field
|
|
182
|
+
*/
|
|
183
|
+
name: string;
|
|
184
|
+
};
|
|
176
185
|
/**
|
|
177
186
|
* Item with id and label dynamic type
|
|
178
187
|
*/
|
|
@@ -330,6 +339,11 @@ export declare namespace DataTypes {
|
|
|
330
339
|
* @param includeArray Is array included, first non null element shoud also be basic type
|
|
331
340
|
*/
|
|
332
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;
|
|
333
347
|
}
|
|
334
348
|
/**
|
|
335
349
|
* List item with number id type
|
package/lib/mjs/DataTypes.js
CHANGED
|
@@ -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.
|
|
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.
|
|
58
|
-
"@typescript-eslint/eslint-plugin": "^5.
|
|
59
|
-
"@typescript-eslint/parser": "^5.
|
|
60
|
-
"eslint": "^8.
|
|
57
|
+
"@types/jest": "^29.1.1",
|
|
58
|
+
"@typescript-eslint/eslint-plugin": "^5.38.1",
|
|
59
|
+
"@typescript-eslint/parser": "^5.38.1",
|
|
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.
|
|
64
|
-
"jest-environment-jsdom": "^29.
|
|
65
|
-
"ts-jest": "^29.0.
|
|
66
|
-
"typescript": "^4.8.
|
|
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
|
@@ -219,6 +219,16 @@ export namespace DataTypes {
|
|
|
219
219
|
label: string;
|
|
220
220
|
};
|
|
221
221
|
|
|
222
|
+
/**
|
|
223
|
+
* Item with id and name property
|
|
224
|
+
*/
|
|
225
|
+
export type IdNameItem<T extends IdType = number> = IdItem<T> & {
|
|
226
|
+
/**
|
|
227
|
+
* name field
|
|
228
|
+
*/
|
|
229
|
+
name: string;
|
|
230
|
+
};
|
|
231
|
+
|
|
222
232
|
/**
|
|
223
233
|
* Item with id and label dynamic type
|
|
224
234
|
*/
|
|
@@ -657,6 +667,29 @@ export namespace DataTypes {
|
|
|
657
667
|
|
|
658
668
|
return true;
|
|
659
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
|
+
}
|
|
660
693
|
}
|
|
661
694
|
|
|
662
695
|
/**
|