@etsoo/shared 1.2.36 → 1.2.38
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/__tests__/DataTypes.ts +16 -0
- package/lib/cjs/DataTypes.d.ts +12 -1
- package/lib/cjs/DataTypes.js +21 -0
- package/lib/mjs/DataTypes.d.ts +12 -1
- package/lib/mjs/DataTypes.js +21 -0
- package/package.json +2 -2
- package/src/DataTypes.ts +30 -1
package/__tests__/DataTypes.ts
CHANGED
|
@@ -17,6 +17,22 @@ test('Tests for IdLabelType', () => {
|
|
|
17
17
|
expect(item).toEqual(itemCopy);
|
|
18
18
|
});
|
|
19
19
|
|
|
20
|
+
test('Tests for Bigint Type', () => {
|
|
21
|
+
const item: DataTypes.IdLabelItem<bigint> = {
|
|
22
|
+
id: 9007199254740999n,
|
|
23
|
+
label: 'Etsoo'
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const json = JSON.stringify(item);
|
|
27
|
+
expect(json).toBe('{"id":"9007199254740999n","label":"Etsoo"}');
|
|
28
|
+
|
|
29
|
+
const itemBack = JSON.parse(
|
|
30
|
+
json,
|
|
31
|
+
DataTypes.jsonBigintReceiver('id')
|
|
32
|
+
) as DataTypes.IdLabelItem<bigint>;
|
|
33
|
+
expect(itemBack.id).toBe(9007199254740999n);
|
|
34
|
+
});
|
|
35
|
+
|
|
20
36
|
test('Tests for convert', () => {
|
|
21
37
|
expect(DataTypes.convert('5', 0)).toStrictEqual(5);
|
|
22
38
|
expect(
|
package/lib/cjs/DataTypes.d.ts
CHANGED
|
@@ -3,6 +3,11 @@
|
|
|
3
3
|
* Narrow case, uses StringRecord
|
|
4
4
|
* Before was wrong with {}, from 4.8 unknown = {} | null | undefined
|
|
5
5
|
*/
|
|
6
|
+
declare global {
|
|
7
|
+
interface BigInt {
|
|
8
|
+
toJSON(): String;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
6
11
|
/**
|
|
7
12
|
* Interface data types
|
|
8
13
|
*/
|
|
@@ -403,11 +408,17 @@ export declare namespace DataTypes {
|
|
|
403
408
|
* https://stackoverflow.com/questions/61681176/json-stringify-replacer-how-to-get-full-path
|
|
404
409
|
*/
|
|
405
410
|
function jsonReplacer(replacer: (this: any, key: string, value: any, path: string) => any): (this: any, key: any, value: any) => any;
|
|
411
|
+
/**
|
|
412
|
+
* JSON.stringify receiver for bigint
|
|
413
|
+
* @param args Keys or paths to convert to bigint
|
|
414
|
+
* @returns JSON receiver function
|
|
415
|
+
*/
|
|
416
|
+
function jsonBigintReceiver(...args: string[]): (this: any, key: any, value: any) => any;
|
|
406
417
|
}
|
|
407
418
|
/**
|
|
408
419
|
* Number and string combination id type
|
|
409
420
|
*/
|
|
410
|
-
export type IdType = number | string;
|
|
421
|
+
export type IdType = number | bigint | string;
|
|
411
422
|
/**
|
|
412
423
|
* List item with number id type
|
|
413
424
|
*/
|
package/lib/cjs/DataTypes.js
CHANGED
|
@@ -6,6 +6,9 @@
|
|
|
6
6
|
*/
|
|
7
7
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
8
|
exports.DataTypes = void 0;
|
|
9
|
+
BigInt.prototype.toJSON = function () {
|
|
10
|
+
return this.toString() + 'n';
|
|
11
|
+
};
|
|
9
12
|
/**
|
|
10
13
|
* Interface data types
|
|
11
14
|
*/
|
|
@@ -456,4 +459,22 @@ var DataTypes;
|
|
|
456
459
|
};
|
|
457
460
|
}
|
|
458
461
|
DataTypes.jsonReplacer = jsonReplacer;
|
|
462
|
+
/**
|
|
463
|
+
* JSON.stringify receiver for bigint
|
|
464
|
+
* @param args Keys or paths to convert to bigint
|
|
465
|
+
* @returns JSON receiver function
|
|
466
|
+
*/
|
|
467
|
+
function jsonBigintReceiver(...args) {
|
|
468
|
+
return jsonReplacer(function (key, value, path) {
|
|
469
|
+
if ((args.includes(key) || args.includes(path)) &&
|
|
470
|
+
typeof value === 'string') {
|
|
471
|
+
if (value.endsWith('n'))
|
|
472
|
+
return BigInt(value.slice(0, -1));
|
|
473
|
+
else
|
|
474
|
+
return BigInt(value);
|
|
475
|
+
}
|
|
476
|
+
return value;
|
|
477
|
+
});
|
|
478
|
+
}
|
|
479
|
+
DataTypes.jsonBigintReceiver = jsonBigintReceiver;
|
|
459
480
|
})(DataTypes || (exports.DataTypes = DataTypes = {}));
|
package/lib/mjs/DataTypes.d.ts
CHANGED
|
@@ -3,6 +3,11 @@
|
|
|
3
3
|
* Narrow case, uses StringRecord
|
|
4
4
|
* Before was wrong with {}, from 4.8 unknown = {} | null | undefined
|
|
5
5
|
*/
|
|
6
|
+
declare global {
|
|
7
|
+
interface BigInt {
|
|
8
|
+
toJSON(): String;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
6
11
|
/**
|
|
7
12
|
* Interface data types
|
|
8
13
|
*/
|
|
@@ -403,11 +408,17 @@ export declare namespace DataTypes {
|
|
|
403
408
|
* https://stackoverflow.com/questions/61681176/json-stringify-replacer-how-to-get-full-path
|
|
404
409
|
*/
|
|
405
410
|
function jsonReplacer(replacer: (this: any, key: string, value: any, path: string) => any): (this: any, key: any, value: any) => any;
|
|
411
|
+
/**
|
|
412
|
+
* JSON.stringify receiver for bigint
|
|
413
|
+
* @param args Keys or paths to convert to bigint
|
|
414
|
+
* @returns JSON receiver function
|
|
415
|
+
*/
|
|
416
|
+
function jsonBigintReceiver(...args: string[]): (this: any, key: any, value: any) => any;
|
|
406
417
|
}
|
|
407
418
|
/**
|
|
408
419
|
* Number and string combination id type
|
|
409
420
|
*/
|
|
410
|
-
export type IdType = number | string;
|
|
421
|
+
export type IdType = number | bigint | string;
|
|
411
422
|
/**
|
|
412
423
|
* List item with number id type
|
|
413
424
|
*/
|
package/lib/mjs/DataTypes.js
CHANGED
|
@@ -3,6 +3,9 @@
|
|
|
3
3
|
* Narrow case, uses StringRecord
|
|
4
4
|
* Before was wrong with {}, from 4.8 unknown = {} | null | undefined
|
|
5
5
|
*/
|
|
6
|
+
BigInt.prototype.toJSON = function () {
|
|
7
|
+
return this.toString() + 'n';
|
|
8
|
+
};
|
|
6
9
|
/**
|
|
7
10
|
* Interface data types
|
|
8
11
|
*/
|
|
@@ -453,4 +456,22 @@ export var DataTypes;
|
|
|
453
456
|
};
|
|
454
457
|
}
|
|
455
458
|
DataTypes.jsonReplacer = jsonReplacer;
|
|
459
|
+
/**
|
|
460
|
+
* JSON.stringify receiver for bigint
|
|
461
|
+
* @param args Keys or paths to convert to bigint
|
|
462
|
+
* @returns JSON receiver function
|
|
463
|
+
*/
|
|
464
|
+
function jsonBigintReceiver(...args) {
|
|
465
|
+
return jsonReplacer(function (key, value, path) {
|
|
466
|
+
if ((args.includes(key) || args.includes(path)) &&
|
|
467
|
+
typeof value === 'string') {
|
|
468
|
+
if (value.endsWith('n'))
|
|
469
|
+
return BigInt(value.slice(0, -1));
|
|
470
|
+
else
|
|
471
|
+
return BigInt(value);
|
|
472
|
+
}
|
|
473
|
+
return value;
|
|
474
|
+
});
|
|
475
|
+
}
|
|
476
|
+
DataTypes.jsonBigintReceiver = jsonBigintReceiver;
|
|
456
477
|
})(DataTypes || (DataTypes = {}));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@etsoo/shared",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.38",
|
|
4
4
|
"description": "TypeScript shared utilities and functions",
|
|
5
5
|
"main": "lib/cjs/index.js",
|
|
6
6
|
"module": "lib/mjs/index.js",
|
|
@@ -59,7 +59,7 @@
|
|
|
59
59
|
"jest": "^29.7.0",
|
|
60
60
|
"jest-environment-jsdom": "^29.7.0",
|
|
61
61
|
"ts-jest": "^29.1.2",
|
|
62
|
-
"typescript": "^5.4.
|
|
62
|
+
"typescript": "^5.4.5"
|
|
63
63
|
},
|
|
64
64
|
"dependencies": {
|
|
65
65
|
"lodash.isequal": "^4.5.0"
|
package/src/DataTypes.ts
CHANGED
|
@@ -4,6 +4,16 @@
|
|
|
4
4
|
* Before was wrong with {}, from 4.8 unknown = {} | null | undefined
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
+
declare global {
|
|
8
|
+
interface BigInt {
|
|
9
|
+
toJSON(): String;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
BigInt.prototype.toJSON = function () {
|
|
14
|
+
return this.toString() + 'n';
|
|
15
|
+
};
|
|
16
|
+
|
|
7
17
|
/**
|
|
8
18
|
* Interface data types
|
|
9
19
|
*/
|
|
@@ -771,12 +781,31 @@ export namespace DataTypes {
|
|
|
771
781
|
);
|
|
772
782
|
};
|
|
773
783
|
}
|
|
784
|
+
|
|
785
|
+
/**
|
|
786
|
+
* JSON.stringify receiver for bigint
|
|
787
|
+
* @param args Keys or paths to convert to bigint
|
|
788
|
+
* @returns JSON receiver function
|
|
789
|
+
*/
|
|
790
|
+
export function jsonBigintReceiver(...args: string[]) {
|
|
791
|
+
return jsonReplacer(function (key, value, path) {
|
|
792
|
+
if (
|
|
793
|
+
(args.includes(key) || args.includes(path)) &&
|
|
794
|
+
typeof value === 'string'
|
|
795
|
+
) {
|
|
796
|
+
if (value.endsWith('n')) return BigInt(value.slice(0, -1));
|
|
797
|
+
else return BigInt(value);
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
return value;
|
|
801
|
+
});
|
|
802
|
+
}
|
|
774
803
|
}
|
|
775
804
|
|
|
776
805
|
/**
|
|
777
806
|
* Number and string combination id type
|
|
778
807
|
*/
|
|
779
|
-
export type IdType = number | string;
|
|
808
|
+
export type IdType = number | bigint | string;
|
|
780
809
|
|
|
781
810
|
/**
|
|
782
811
|
* List item with number id type
|