ts-data-forge 6.1.1 → 6.2.0
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/dist/others/unknown-to-string.d.mts +5 -0
- package/dist/others/unknown-to-string.d.mts.map +1 -1
- package/dist/others/unknown-to-string.mjs +40 -8
- package/dist/others/unknown-to-string.mjs.map +1 -1
- package/package.json +6 -5
- package/src/others/unknown-to-string.mts +52 -12
- package/src/others/unknown-to-string.test.mts +238 -60
|
@@ -14,6 +14,11 @@
|
|
|
14
14
|
* - Functions: converted to their string representation
|
|
15
15
|
* - null: returns "null" (not "null" from JSON)
|
|
16
16
|
* - undefined: returns "undefined"
|
|
17
|
+
* - Date: converted to ISO string
|
|
18
|
+
* - RegExp: converted to regex literal string
|
|
19
|
+
* - Map: converted to Map representation with entries
|
|
20
|
+
* - Set: converted to Set representation with values
|
|
21
|
+
* - Error: returns error message
|
|
17
22
|
* - Objects: JSON stringified (with optional pretty printing)
|
|
18
23
|
*
|
|
19
24
|
* @param value - The unknown value to convert to string
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"unknown-to-string.d.mts","sourceRoot":"","sources":["../../src/others/unknown-to-string.mts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"unknown-to-string.d.mts","sourceRoot":"","sources":["../../src/others/unknown-to-string.mts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,eAAO,MAAM,eAAe,GAC1B,OAAO,OAAO,EACd,UAAU,OAAO,CAAC,QAAQ,CAAC;IAAE,iBAAiB,EAAE,OAAO,CAAA;CAAE,CAAC,CAAC,KAC1D,MAuEF,CAAC"}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { isError } from '@sindresorhus/is';
|
|
2
|
-
import { isNonNullish } from '../guard/is-type.mjs';
|
|
1
|
+
import { isDate, isRegExp, isError, isMap, isSet } from '@sindresorhus/is';
|
|
3
2
|
|
|
4
3
|
/**
|
|
5
4
|
* Converts an unknown value to its string representation in a type-safe manner.
|
|
@@ -17,6 +16,11 @@ import { isNonNullish } from '../guard/is-type.mjs';
|
|
|
17
16
|
* - Functions: converted to their string representation
|
|
18
17
|
* - null: returns "null" (not "null" from JSON)
|
|
19
18
|
* - undefined: returns "undefined"
|
|
19
|
+
* - Date: converted to ISO string
|
|
20
|
+
* - RegExp: converted to regex literal string
|
|
21
|
+
* - Map: converted to Map representation with entries
|
|
22
|
+
* - Set: converted to Set representation with values
|
|
23
|
+
* - Error: returns error message
|
|
20
24
|
* - Objects: JSON stringified (with optional pretty printing)
|
|
21
25
|
*
|
|
22
26
|
* @param value - The unknown value to convert to string
|
|
@@ -41,23 +45,51 @@ const unknownToString = (value, options) => {
|
|
|
41
45
|
case 'symbol':
|
|
42
46
|
case 'function':
|
|
43
47
|
return value.toString();
|
|
44
|
-
case '
|
|
45
|
-
|
|
48
|
+
case 'undefined':
|
|
49
|
+
return 'undefined';
|
|
50
|
+
case 'object': {
|
|
51
|
+
if (value === null) {
|
|
46
52
|
return 'null';
|
|
47
53
|
}
|
|
48
54
|
try {
|
|
49
|
-
|
|
55
|
+
// Special handling for Date
|
|
56
|
+
if (isDate(value)) {
|
|
57
|
+
return value.toISOString();
|
|
58
|
+
}
|
|
59
|
+
// Special handling for RegExp
|
|
60
|
+
if (isRegExp(value)) {
|
|
61
|
+
return value.toString();
|
|
62
|
+
}
|
|
63
|
+
if (isError(value)) {
|
|
64
|
+
return value.message;
|
|
65
|
+
}
|
|
66
|
+
const prettyPrintObject = options?.prettyPrintObject === true;
|
|
67
|
+
// Special handling for Map
|
|
68
|
+
if (isMap(value)) {
|
|
69
|
+
const entries = Array.from(value.entries());
|
|
70
|
+
const entriesStr = prettyPrintObject
|
|
71
|
+
? JSON.stringify(entries, undefined, 2)
|
|
72
|
+
: JSON.stringify(entries);
|
|
73
|
+
return `Map(${entriesStr})`;
|
|
74
|
+
}
|
|
75
|
+
// Special handling for Set
|
|
76
|
+
if (isSet(value)) {
|
|
77
|
+
const values = Array.from(value.values());
|
|
78
|
+
const valuesStr = prettyPrintObject
|
|
79
|
+
? JSON.stringify(values, undefined, 2)
|
|
80
|
+
: JSON.stringify(values);
|
|
81
|
+
return `Set(${valuesStr})`;
|
|
82
|
+
}
|
|
83
|
+
return prettyPrintObject
|
|
50
84
|
? JSON.stringify(value, undefined, 2)
|
|
51
85
|
: JSON.stringify(value);
|
|
52
|
-
return stringified;
|
|
53
86
|
}
|
|
54
87
|
catch (error) {
|
|
55
88
|
return isError(error)
|
|
56
89
|
? error.message
|
|
57
90
|
: '[Circular or Non-serializable]';
|
|
58
91
|
}
|
|
59
|
-
|
|
60
|
-
return 'undefined';
|
|
92
|
+
}
|
|
61
93
|
}
|
|
62
94
|
};
|
|
63
95
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"unknown-to-string.mjs","sources":["../../src/others/unknown-to-string.mts"],"sourcesContent":[null],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"unknown-to-string.mjs","sources":["../../src/others/unknown-to-string.mts"],"sourcesContent":[null],"names":[],"mappings":";;AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiCG;MACU,eAAe,GAAG,CAC7B,KAAc,EACd,OAA2D,KACjD;IACV,QAAQ,OAAO,KAAK;AAClB,QAAA,KAAK,QAAQ;AACX,YAAA,OAAO,KAAK;AAEd,QAAA,KAAK,QAAQ;AACX,YAAA,OAAO,GAAG,KAAK,CAAC,QAAQ,EAAE,GAAG;AAE/B,QAAA,KAAK,QAAQ;AACb,QAAA,KAAK,SAAS;AACd,QAAA,KAAK,QAAQ;AACb,QAAA,KAAK,UAAU;AACb,YAAA,OAAO,KAAK,CAAC,QAAQ,EAAE;AAEzB,QAAA,KAAK,WAAW;AACd,YAAA,OAAO,WAAW;QAEpB,KAAK,QAAQ,EAAE;AACb,YAAA,IAAI,KAAK,KAAK,IAAI,EAAE;AAClB,gBAAA,OAAO,MAAM;YACf;AAEA,YAAA,IAAI;;AAEF,gBAAA,IAAI,MAAM,CAAC,KAAK,CAAC,EAAE;AACjB,oBAAA,OAAO,KAAK,CAAC,WAAW,EAAE;gBAC5B;;AAGA,gBAAA,IAAI,QAAQ,CAAC,KAAK,CAAC,EAAE;AACnB,oBAAA,OAAO,KAAK,CAAC,QAAQ,EAAE;gBACzB;AAEA,gBAAA,IAAI,OAAO,CAAC,KAAK,CAAC,EAAE;oBAClB,OAAO,KAAK,CAAC,OAAO;gBACtB;AAEA,gBAAA,MAAM,iBAAiB,GAAG,OAAO,EAAE,iBAAiB,KAAK,IAAI;;AAG7D,gBAAA,IAAI,KAAK,CAAC,KAAK,CAAC,EAAE;oBAChB,MAAM,OAAO,GAAG,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC;oBAE3C,MAAM,UAAU,GAAG;0BACf,IAAI,CAAC,SAAS,CAAC,OAAO,EAAE,SAAS,EAAE,CAAC;AACtC,0BAAE,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC;oBAE3B,OAAO,CAAA,IAAA,EAAO,UAAU,CAAA,CAAA,CAAG;gBAC7B;;AAGA,gBAAA,IAAI,KAAK,CAAC,KAAK,CAAC,EAAE;oBAChB,MAAM,MAAM,GAAG,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC;oBAEzC,MAAM,SAAS,GAAG;0BACd,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE,SAAS,EAAE,CAAC;AACrC,0BAAE,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC;oBAE1B,OAAO,CAAA,IAAA,EAAO,SAAS,CAAA,CAAA,CAAG;gBAC5B;AAEA,gBAAA,OAAO;sBACH,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,SAAS,EAAE,CAAC;AACpC,sBAAE,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC;YAC3B;YAAE,OAAO,KAAK,EAAE;gBACd,OAAO,OAAO,CAAC,KAAK;sBAChB,KAAK,CAAC;sBACN,gCAAgC;YACtC;QACF;;AAEJ;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ts-data-forge",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.2.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
@@ -100,10 +100,11 @@
|
|
|
100
100
|
"@vitest/ui": "4.0.16",
|
|
101
101
|
"conventional-changelog-conventionalcommits": "9.1.0",
|
|
102
102
|
"cspell": "9.4.0",
|
|
103
|
+
"dedent": "^1.7.1",
|
|
103
104
|
"eslint": "9.39.2",
|
|
104
|
-
"eslint-config-typed": "4.
|
|
105
|
+
"eslint-config-typed": "4.3.0",
|
|
105
106
|
"github-settings-as-code": "1.0.6",
|
|
106
|
-
"immer": "11.0
|
|
107
|
+
"immer": "11.1.0",
|
|
107
108
|
"jiti": "2.6.1",
|
|
108
109
|
"markdownlint": "0.40.0",
|
|
109
110
|
"markdownlint-cli2": "0.20.0",
|
|
@@ -112,9 +113,9 @@
|
|
|
112
113
|
"prettier": "3.7.4",
|
|
113
114
|
"prettier-plugin-organize-imports": "4.3.0",
|
|
114
115
|
"prettier-plugin-packagejson": "2.5.20",
|
|
115
|
-
"rollup": "4.
|
|
116
|
+
"rollup": "4.54.0",
|
|
116
117
|
"semantic-release": "25.0.2",
|
|
117
|
-
"ts-repo-utils": "
|
|
118
|
+
"ts-repo-utils": "8.0.0",
|
|
118
119
|
"tslib": "2.8.1",
|
|
119
120
|
"tsx": "4.21.0",
|
|
120
121
|
"typedoc": "0.28.15",
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { isError } from '@sindresorhus/is';
|
|
2
|
-
import { isNonNullish } from '../guard/index.mjs';
|
|
1
|
+
import { isDate, isError, isMap, isRegExp, isSet } from '@sindresorhus/is';
|
|
3
2
|
|
|
4
3
|
/**
|
|
5
4
|
* Converts an unknown value to its string representation in a type-safe manner.
|
|
@@ -17,6 +16,11 @@ import { isNonNullish } from '../guard/index.mjs';
|
|
|
17
16
|
* - Functions: converted to their string representation
|
|
18
17
|
* - null: returns "null" (not "null" from JSON)
|
|
19
18
|
* - undefined: returns "undefined"
|
|
19
|
+
* - Date: converted to ISO string
|
|
20
|
+
* - RegExp: converted to regex literal string
|
|
21
|
+
* - Map: converted to Map representation with entries
|
|
22
|
+
* - Set: converted to Set representation with values
|
|
23
|
+
* - Error: returns error message
|
|
20
24
|
* - Objects: JSON stringified (with optional pretty printing)
|
|
21
25
|
*
|
|
22
26
|
* @param value - The unknown value to convert to string
|
|
@@ -47,25 +51,61 @@ export const unknownToString = (
|
|
|
47
51
|
case 'function':
|
|
48
52
|
return value.toString();
|
|
49
53
|
|
|
50
|
-
case '
|
|
51
|
-
|
|
54
|
+
case 'undefined':
|
|
55
|
+
return 'undefined';
|
|
56
|
+
|
|
57
|
+
case 'object': {
|
|
58
|
+
if (value === null) {
|
|
52
59
|
return 'null';
|
|
53
60
|
}
|
|
54
61
|
|
|
55
62
|
try {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
63
|
+
// Special handling for Date
|
|
64
|
+
if (isDate(value)) {
|
|
65
|
+
return value.toISOString();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Special handling for RegExp
|
|
69
|
+
if (isRegExp(value)) {
|
|
70
|
+
return value.toString();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (isError(value)) {
|
|
74
|
+
return value.message;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const prettyPrintObject = options?.prettyPrintObject === true;
|
|
78
|
+
|
|
79
|
+
// Special handling for Map
|
|
80
|
+
if (isMap(value)) {
|
|
81
|
+
const entries = Array.from(value.entries());
|
|
82
|
+
|
|
83
|
+
const entriesStr = prettyPrintObject
|
|
84
|
+
? JSON.stringify(entries, undefined, 2)
|
|
85
|
+
: JSON.stringify(entries);
|
|
60
86
|
|
|
61
|
-
|
|
87
|
+
return `Map(${entriesStr})`;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Special handling for Set
|
|
91
|
+
if (isSet(value)) {
|
|
92
|
+
const values = Array.from(value.values());
|
|
93
|
+
|
|
94
|
+
const valuesStr = prettyPrintObject
|
|
95
|
+
? JSON.stringify(values, undefined, 2)
|
|
96
|
+
: JSON.stringify(values);
|
|
97
|
+
|
|
98
|
+
return `Set(${valuesStr})`;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return prettyPrintObject
|
|
102
|
+
? JSON.stringify(value, undefined, 2)
|
|
103
|
+
: JSON.stringify(value);
|
|
62
104
|
} catch (error) {
|
|
63
105
|
return isError(error)
|
|
64
106
|
? error.message
|
|
65
107
|
: '[Circular or Non-serializable]';
|
|
66
108
|
}
|
|
67
|
-
|
|
68
|
-
case 'undefined':
|
|
69
|
-
return 'undefined';
|
|
109
|
+
}
|
|
70
110
|
}
|
|
71
111
|
};
|
|
@@ -1,86 +1,274 @@
|
|
|
1
|
+
import dedent from 'dedent';
|
|
1
2
|
import { unknownToString } from './unknown-to-string.mjs';
|
|
2
3
|
|
|
3
4
|
describe(unknownToString, () => {
|
|
4
5
|
test('string', () => {
|
|
5
|
-
const
|
|
6
|
+
const value = 'aaaaa';
|
|
6
7
|
|
|
7
|
-
expect(
|
|
8
|
+
expect(unknownToString(value)).toBe('aaaaa');
|
|
8
9
|
|
|
9
|
-
expect(JSON.stringify(
|
|
10
|
+
expect(JSON.stringify(value)).toBe('"aaaaa"');
|
|
10
11
|
});
|
|
11
12
|
|
|
12
13
|
test('number', () => {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
expect(result).toBe('1');
|
|
14
|
+
expect(unknownToString(1)).toBe('1');
|
|
16
15
|
|
|
17
16
|
expect(JSON.stringify(1)).toBe('1');
|
|
18
17
|
});
|
|
19
18
|
|
|
20
|
-
test('
|
|
21
|
-
const
|
|
19
|
+
test('BigInt value', () => {
|
|
20
|
+
const value = 123n;
|
|
22
21
|
|
|
23
|
-
expect(
|
|
22
|
+
expect(unknownToString(value)).toBe('123n');
|
|
24
23
|
|
|
25
|
-
expect(JSON.stringify(
|
|
24
|
+
expect(() => JSON.stringify(value)).toThrowError(
|
|
25
|
+
'Do not know how to serialize a BigInt',
|
|
26
|
+
);
|
|
26
27
|
});
|
|
27
28
|
|
|
28
|
-
test('
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
expect(result).toBe('Symbol(sym)');
|
|
29
|
+
test('boolean', () => {
|
|
30
|
+
expect(unknownToString(true)).toBe('true');
|
|
32
31
|
|
|
33
|
-
expect(JSON.stringify(
|
|
32
|
+
expect(JSON.stringify(true)).toBe('true');
|
|
34
33
|
});
|
|
35
34
|
|
|
36
|
-
test('
|
|
37
|
-
|
|
35
|
+
test('undefined', () => {
|
|
36
|
+
expect(unknownToString(undefined)).toBe('undefined');
|
|
38
37
|
|
|
39
|
-
expect(
|
|
38
|
+
expect(JSON.stringify(undefined)).toBeUndefined();
|
|
39
|
+
});
|
|
40
40
|
|
|
41
|
-
|
|
41
|
+
test('null', () => {
|
|
42
|
+
expect(unknownToString(null)).toBe('null');
|
|
43
|
+
|
|
44
|
+
expect(JSON.stringify(null)).toBe('null');
|
|
42
45
|
});
|
|
43
46
|
|
|
44
|
-
test('
|
|
45
|
-
const
|
|
47
|
+
test('symbol', () => {
|
|
48
|
+
const value = Symbol('sym');
|
|
46
49
|
|
|
47
|
-
expect(
|
|
50
|
+
expect(unknownToString(value)).toBe('Symbol(sym)');
|
|
48
51
|
|
|
49
|
-
expect(JSON.stringify(
|
|
52
|
+
expect(JSON.stringify(value)).toBeUndefined();
|
|
50
53
|
});
|
|
51
54
|
|
|
52
|
-
|
|
53
|
-
|
|
55
|
+
describe('function', () => {
|
|
56
|
+
test('() => 0', () => {
|
|
57
|
+
const fn = (): number => 0;
|
|
54
58
|
|
|
55
|
-
|
|
59
|
+
expect(unknownToString(fn)).toBe('() => 0');
|
|
56
60
|
|
|
57
|
-
|
|
58
|
-
|
|
61
|
+
expect(JSON.stringify(fn)).toBeUndefined();
|
|
62
|
+
});
|
|
59
63
|
|
|
60
|
-
|
|
61
|
-
|
|
64
|
+
test('(args) => ({ ret: args })', () => {
|
|
65
|
+
const fn = (args: unknown): unknown => ({ ret: args });
|
|
62
66
|
|
|
63
|
-
|
|
67
|
+
expect(unknownToString(fn)).toBe('(args) => ({ ret: args })');
|
|
64
68
|
|
|
65
|
-
|
|
69
|
+
expect(JSON.stringify(fn)).toBeUndefined();
|
|
70
|
+
});
|
|
66
71
|
});
|
|
67
72
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
{ a: { b: 1 } }
|
|
71
|
-
|
|
72
|
-
|
|
73
|
+
describe('object', () => {
|
|
74
|
+
test('object', () => {
|
|
75
|
+
const obj = { a: { b: 1 } };
|
|
76
|
+
|
|
77
|
+
expect(unknownToString(obj)).toBe('{"a":{"b":1}}');
|
|
73
78
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
`
|
|
82
|
-
|
|
83
|
-
|
|
79
|
+
expect(JSON.stringify(obj)).toBe('{"a":{"b":1}}');
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
test('object(prettyPrint=true)', () => {
|
|
83
|
+
const obj = { a: { b: 1 } };
|
|
84
|
+
|
|
85
|
+
expect(unknownToString(obj, { prettyPrintObject: true })).toBe(
|
|
86
|
+
dedent`
|
|
87
|
+
{
|
|
88
|
+
"a": {
|
|
89
|
+
"b": 1
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
`,
|
|
93
|
+
);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
describe('array', () => {
|
|
97
|
+
test('array of primitives', () => {
|
|
98
|
+
const arr = [1, 2, 3, 'test', true];
|
|
99
|
+
|
|
100
|
+
expect(unknownToString(arr)).toBe('[1,2,3,"test",true]');
|
|
101
|
+
|
|
102
|
+
expect(JSON.stringify(arr)).toBe('[1,2,3,"test",true]');
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
test('array of objects', () => {
|
|
106
|
+
const arr = [{ a: 1 }, { b: 2 }];
|
|
107
|
+
|
|
108
|
+
expect(unknownToString(arr)).toBe('[{"a":1},{"b":2}]');
|
|
109
|
+
|
|
110
|
+
expect(JSON.stringify(arr)).toBe('[{"a":1},{"b":2}]');
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
test('empty array', () => {
|
|
114
|
+
expect(unknownToString([])).toBe('[]');
|
|
115
|
+
|
|
116
|
+
expect(JSON.stringify([])).toBe('[]');
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
test('nested array', () => {
|
|
120
|
+
const nestedArray = [
|
|
121
|
+
[1, 2],
|
|
122
|
+
[3, 4],
|
|
123
|
+
];
|
|
124
|
+
|
|
125
|
+
expect(unknownToString(nestedArray)).toBe('[[1,2],[3,4]]');
|
|
126
|
+
|
|
127
|
+
expect(JSON.stringify(nestedArray)).toBe('[[1,2],[3,4]]');
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
test('array with prettyPrint', () => {
|
|
131
|
+
const arr = [1, { a: 2 }, 3];
|
|
132
|
+
|
|
133
|
+
expect(
|
|
134
|
+
unknownToString(arr, {
|
|
135
|
+
prettyPrintObject: true,
|
|
136
|
+
}),
|
|
137
|
+
).toBe(
|
|
138
|
+
dedent`
|
|
139
|
+
[
|
|
140
|
+
1,
|
|
141
|
+
{
|
|
142
|
+
"a": 2
|
|
143
|
+
},
|
|
144
|
+
3
|
|
145
|
+
]
|
|
146
|
+
`,
|
|
147
|
+
);
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
describe('special objects', () => {
|
|
152
|
+
test('Date object returns ISO string', () => {
|
|
153
|
+
const date = new Date('2024-01-15T10:30:00.000Z');
|
|
154
|
+
|
|
155
|
+
expect(unknownToString(date)).toBe('2024-01-15T10:30:00.000Z');
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
test('RegExp returns regex literal', () => {
|
|
159
|
+
const regex = /test/giu;
|
|
160
|
+
|
|
161
|
+
expect(unknownToString(regex)).toBe('/test/giu');
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
test('RegExp without flags', () => {
|
|
165
|
+
// eslint-disable-next-line require-unicode-regexp
|
|
166
|
+
const regex = /hello/;
|
|
167
|
+
|
|
168
|
+
expect(unknownToString(regex)).toBe('/hello/');
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
test('Map with entries', () => {
|
|
172
|
+
const map = new Map<number | string, boolean | number | string>([
|
|
173
|
+
['key1', 'value1'],
|
|
174
|
+
['key2', 42],
|
|
175
|
+
[3, true],
|
|
176
|
+
]);
|
|
177
|
+
|
|
178
|
+
expect(unknownToString(map)).toBe(
|
|
179
|
+
'Map([["key1","value1"],["key2",42],[3,true]])',
|
|
180
|
+
);
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
test('empty Map', () => {
|
|
184
|
+
const map = new Map();
|
|
185
|
+
|
|
186
|
+
expect(unknownToString(map)).toBe('Map([])');
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
test('Map with prettyPrint', () => {
|
|
190
|
+
const map = new Map([
|
|
191
|
+
['a', 1],
|
|
192
|
+
['b', 2],
|
|
193
|
+
]);
|
|
194
|
+
|
|
195
|
+
expect(unknownToString(map, { prettyPrintObject: true })).toBe(
|
|
196
|
+
dedent`
|
|
197
|
+
Map([
|
|
198
|
+
[
|
|
199
|
+
"a",
|
|
200
|
+
1
|
|
201
|
+
],
|
|
202
|
+
[
|
|
203
|
+
"b",
|
|
204
|
+
2
|
|
205
|
+
]
|
|
206
|
+
])
|
|
207
|
+
`,
|
|
208
|
+
);
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
test('Set with values', () => {
|
|
212
|
+
const set = new Set([1, 2, 3, 'test', true]);
|
|
213
|
+
|
|
214
|
+
expect(unknownToString(set)).toBe('Set([1,2,3,"test",true])');
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
test('empty Set', () => {
|
|
218
|
+
const set = new Set();
|
|
219
|
+
|
|
220
|
+
expect(unknownToString(set)).toBe('Set([])');
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
test('Set with prettyPrint', () => {
|
|
224
|
+
const set = new Set([1, { a: 2 }, 3]);
|
|
225
|
+
|
|
226
|
+
expect(unknownToString(set, { prettyPrintObject: true })).toBe(
|
|
227
|
+
dedent`
|
|
228
|
+
Set([
|
|
229
|
+
1,
|
|
230
|
+
{
|
|
231
|
+
"a": 2
|
|
232
|
+
},
|
|
233
|
+
3
|
|
234
|
+
])
|
|
235
|
+
`,
|
|
236
|
+
);
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
test('nested Map and Set', () => {
|
|
240
|
+
const map = new Map([['set', new Set([1, 2])]]);
|
|
241
|
+
|
|
242
|
+
const result = unknownToString(map);
|
|
243
|
+
|
|
244
|
+
// Set is converted to object notation in JSON.stringify
|
|
245
|
+
expect(result).toMatch(/Map\(\[\["set",/u);
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
test('Error object returns message', () => {
|
|
249
|
+
const err = new Error('Something went wrong');
|
|
250
|
+
|
|
251
|
+
expect(unknownToString(err)).toBe('Something went wrong');
|
|
252
|
+
|
|
253
|
+
expect(JSON.stringify(err)).toBe('{}');
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
test('Error object with empty message', () => {
|
|
257
|
+
// eslint-disable-next-line unicorn/error-message
|
|
258
|
+
const err = new Error('');
|
|
259
|
+
|
|
260
|
+
expect(unknownToString(err)).toBe('');
|
|
261
|
+
|
|
262
|
+
expect(JSON.stringify(err)).toBe('{}');
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
test('TypeError object with empty message', () => {
|
|
266
|
+
// eslint-disable-next-line unicorn/error-message
|
|
267
|
+
const err = new TypeError('');
|
|
268
|
+
|
|
269
|
+
expect(unknownToString(err)).toBe('');
|
|
270
|
+
});
|
|
271
|
+
});
|
|
84
272
|
});
|
|
85
273
|
|
|
86
274
|
test('circular reference returns error message', () => {
|
|
@@ -88,18 +276,10 @@ describe(unknownToString, () => {
|
|
|
88
276
|
|
|
89
277
|
mut_circular.self = mut_circular;
|
|
90
278
|
|
|
91
|
-
const result = unknownToString(mut_circular);
|
|
92
|
-
|
|
93
279
|
// Should return an error message string instead of throwing
|
|
94
|
-
expectTypeOf(
|
|
280
|
+
expectTypeOf(unknownToString(mut_circular)).toBeString();
|
|
95
281
|
|
|
96
|
-
expect(
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
test('BigInt value', () => {
|
|
100
|
-
const result = unknownToString(123n);
|
|
101
|
-
|
|
102
|
-
expect(result).toBe('123n');
|
|
282
|
+
expect(unknownToString(mut_circular)).toMatch(/circular|serialize/iu);
|
|
103
283
|
});
|
|
104
284
|
|
|
105
285
|
test('non-Error thrown during serialization returns fallback text', () => {
|
|
@@ -110,8 +290,6 @@ describe(unknownToString, () => {
|
|
|
110
290
|
},
|
|
111
291
|
};
|
|
112
292
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
expect(result).toBe('[Circular or Non-serializable]');
|
|
293
|
+
expect(unknownToString(value)).toBe('[Circular or Non-serializable]');
|
|
116
294
|
});
|
|
117
295
|
});
|