@simplysm/core-common 13.0.76 → 13.0.78
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 +64 -21
- package/dist/extensions/arr-ext.d.ts +1 -1
- package/dist/extensions/arr-ext.d.ts.map +1 -1
- package/dist/extensions/arr-ext.helpers.d.ts +8 -0
- package/dist/extensions/arr-ext.helpers.d.ts.map +1 -1
- package/dist/extensions/arr-ext.helpers.js +65 -0
- package/dist/extensions/arr-ext.helpers.js.map +2 -2
- package/dist/extensions/arr-ext.js +16 -124
- package/dist/extensions/arr-ext.js.map +2 -2
- package/dist/extensions/arr-ext.types.d.ts +40 -32
- package/dist/extensions/arr-ext.types.d.ts.map +1 -1
- package/dist/extensions/map-ext.js.map +1 -1
- package/dist/extensions/set-ext.js.map +1 -1
- package/dist/features/event-emitter.d.ts +4 -4
- package/dist/features/event-emitter.d.ts.map +1 -1
- package/dist/features/event-emitter.js.map +1 -1
- package/dist/features/serial-queue.js +2 -2
- package/dist/features/serial-queue.js.map +1 -1
- package/dist/index.d.ts +13 -13
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +27 -13
- package/dist/index.js.map +1 -1
- package/dist/types/date-only.js +2 -2
- package/dist/types/date-only.js.map +1 -1
- package/dist/types/date-time.js +2 -2
- package/dist/types/date-time.js.map +1 -1
- package/dist/types/time.js +2 -2
- package/dist/types/time.js.map +1 -1
- package/dist/types/uuid.d.ts +2 -2
- package/dist/types/uuid.d.ts.map +1 -1
- package/dist/types/uuid.js +1 -1
- package/dist/types/uuid.js.map +1 -1
- package/dist/utils/bytes.d.ts +10 -10
- package/dist/utils/bytes.d.ts.map +1 -1
- package/dist/utils/bytes.js +10 -10
- package/dist/utils/bytes.js.map +1 -1
- package/dist/utils/date-format.d.ts +1 -1
- package/dist/utils/date-format.d.ts.map +1 -1
- package/dist/utils/date-format.js +2 -2
- package/dist/utils/date-format.js.map +1 -1
- package/dist/utils/error.d.ts +1 -1
- package/dist/utils/error.d.ts.map +1 -1
- package/dist/utils/error.js +2 -2
- package/dist/utils/error.js.map +1 -1
- package/dist/utils/json.d.ts +4 -2
- package/dist/utils/json.d.ts.map +1 -1
- package/dist/utils/json.js +9 -9
- package/dist/utils/json.js.map +1 -1
- package/dist/utils/num.d.ts +10 -10
- package/dist/utils/num.d.ts.map +1 -1
- package/dist/utils/num.js +11 -11
- package/dist/utils/num.js.map +1 -1
- package/dist/utils/obj.d.ts +40 -40
- package/dist/utils/obj.d.ts.map +1 -1
- package/dist/utils/obj.js +102 -99
- package/dist/utils/obj.js.map +1 -1
- package/dist/utils/path.d.ts +3 -3
- package/dist/utils/path.d.ts.map +1 -1
- package/dist/utils/path.js +6 -6
- package/dist/utils/path.js.map +1 -1
- package/dist/utils/primitive.d.ts +1 -1
- package/dist/utils/primitive.d.ts.map +1 -1
- package/dist/utils/primitive.js +2 -2
- package/dist/utils/primitive.js.map +1 -1
- package/dist/utils/str.d.ts +16 -16
- package/dist/utils/str.d.ts.map +1 -1
- package/dist/utils/str.js +16 -16
- package/dist/utils/str.js.map +1 -1
- package/dist/utils/transferable.d.ts +3 -3
- package/dist/utils/transferable.d.ts.map +1 -1
- package/dist/utils/transferable.js +10 -10
- package/dist/utils/transferable.js.map +1 -1
- package/dist/utils/wait.d.ts +2 -2
- package/dist/utils/wait.d.ts.map +1 -1
- package/dist/utils/wait.js +5 -5
- package/dist/utils/wait.js.map +1 -1
- package/dist/utils/xml.d.ts +2 -2
- package/dist/utils/xml.d.ts.map +1 -1
- package/dist/utils/xml.js +4 -4
- package/dist/utils/xml.js.map +1 -1
- package/dist/{zip/sd-zip.d.ts → utils/zip.d.ts} +1 -1
- package/dist/utils/zip.d.ts.map +1 -0
- package/dist/{zip/sd-zip.js → utils/zip.js} +1 -1
- package/dist/{zip/sd-zip.js.map → utils/zip.js.map} +1 -1
- package/package.json +1 -1
- package/src/extensions/arr-ext.helpers.ts +86 -0
- package/src/extensions/arr-ext.ts +22 -170
- package/src/extensions/arr-ext.types.ts +76 -48
- package/src/extensions/map-ext.ts +3 -3
- package/src/extensions/set-ext.ts +2 -2
- package/src/features/event-emitter.ts +6 -6
- package/src/features/serial-queue.ts +2 -2
- package/src/index.ts +16 -16
- package/src/types/date-only.ts +2 -2
- package/src/types/date-time.ts +2 -2
- package/src/types/time.ts +2 -2
- package/src/types/uuid.ts +2 -2
- package/src/utils/bytes.ts +15 -15
- package/src/utils/date-format.ts +1 -1
- package/src/utils/error.ts +1 -1
- package/src/utils/json.ts +9 -7
- package/src/utils/num.ts +15 -15
- package/src/utils/obj.ts +119 -116
- package/src/utils/path.ts +3 -3
- package/src/utils/primitive.ts +1 -1
- package/src/utils/str.ts +16 -16
- package/src/utils/transferable.ts +9 -9
- package/src/utils/wait.ts +3 -3
- package/src/utils/xml.ts +2 -2
- package/tests/extensions/array-extension.spec.ts +7 -5
- package/tests/types/uuid.spec.ts +4 -4
- package/tests/utils/bytes-utils.spec.ts +42 -49
- package/tests/utils/date-format.spec.ts +89 -88
- package/tests/utils/debounce-queue.spec.ts +3 -1
- package/tests/utils/json.spec.ts +61 -68
- package/tests/utils/number.spec.ts +41 -46
- package/tests/utils/object.spec.ts +120 -139
- package/tests/utils/path.spec.ts +19 -19
- package/tests/utils/primitive.spec.ts +12 -12
- package/tests/utils/string.spec.ts +66 -74
- package/tests/utils/transferable.spec.ts +55 -62
- package/tests/utils/wait.spec.ts +10 -10
- package/tests/utils/xml.spec.ts +25 -25
- package/dist/zip/sd-zip.d.ts.map +0 -1
- /package/src/{zip/sd-zip.ts → utils/zip.ts} +0 -0
- /package/tests/{zip/sd-zip.spec.ts → utils/zip.spec.ts} +0 -0
package/tests/utils/json.spec.ts
CHANGED
|
@@ -1,12 +1,5 @@
|
|
|
1
1
|
import { describe, it, expect } from "vitest";
|
|
2
|
-
import {
|
|
3
|
-
jsonStringify as stringify,
|
|
4
|
-
jsonParse as parse,
|
|
5
|
-
DateTime,
|
|
6
|
-
DateOnly,
|
|
7
|
-
Time,
|
|
8
|
-
Uuid,
|
|
9
|
-
} from "@simplysm/core-common";
|
|
2
|
+
import { json, DateTime, DateOnly, Time, Uuid } from "@simplysm/core-common";
|
|
10
3
|
|
|
11
4
|
describe("JsonConvert", () => {
|
|
12
5
|
//#region stringify
|
|
@@ -14,8 +7,8 @@ describe("JsonConvert", () => {
|
|
|
14
7
|
describe("stringify()", () => {
|
|
15
8
|
it("Serializes Date with __type__", () => {
|
|
16
9
|
const date = new Date("2024-03-15T10:30:00.000Z");
|
|
17
|
-
const
|
|
18
|
-
const parsed = JSON.parse(
|
|
10
|
+
const str = json.stringify(date);
|
|
11
|
+
const parsed = JSON.parse(str);
|
|
19
12
|
|
|
20
13
|
expect(parsed.__type__).toBe("Date");
|
|
21
14
|
expect(parsed.data).toBe("2024-03-15T10:30:00.000Z");
|
|
@@ -23,8 +16,8 @@ describe("JsonConvert", () => {
|
|
|
23
16
|
|
|
24
17
|
it("Serializes DateTime with __type__", () => {
|
|
25
18
|
const dt = new DateTime(2024, 3, 15, 10, 30);
|
|
26
|
-
const
|
|
27
|
-
const parsed = JSON.parse(
|
|
19
|
+
const str = json.stringify(dt);
|
|
20
|
+
const parsed = JSON.parse(str);
|
|
28
21
|
|
|
29
22
|
expect(parsed.__type__).toBe("DateTime");
|
|
30
23
|
expect(typeof parsed.data).toBe("string");
|
|
@@ -32,24 +25,24 @@ describe("JsonConvert", () => {
|
|
|
32
25
|
|
|
33
26
|
it("Serializes DateOnly with __type__", () => {
|
|
34
27
|
const d = new DateOnly(2024, 3, 15);
|
|
35
|
-
const
|
|
36
|
-
const parsed = JSON.parse(
|
|
28
|
+
const str = json.stringify(d);
|
|
29
|
+
const parsed = JSON.parse(str);
|
|
37
30
|
|
|
38
31
|
expect(parsed.__type__).toBe("DateOnly");
|
|
39
32
|
});
|
|
40
33
|
|
|
41
34
|
it("Serializes Time with __type__", () => {
|
|
42
35
|
const t = new Time(10, 30, 45);
|
|
43
|
-
const
|
|
44
|
-
const parsed = JSON.parse(
|
|
36
|
+
const str = json.stringify(t);
|
|
37
|
+
const parsed = JSON.parse(str);
|
|
45
38
|
|
|
46
39
|
expect(parsed.__type__).toBe("Time");
|
|
47
40
|
});
|
|
48
41
|
|
|
49
42
|
it("Serializes Uuid with __type__", () => {
|
|
50
43
|
const uuid = new Uuid("12345678-9abc-def0-1234-56789abcdef0");
|
|
51
|
-
const
|
|
52
|
-
const parsed = JSON.parse(
|
|
44
|
+
const str = json.stringify(uuid);
|
|
45
|
+
const parsed = JSON.parse(str);
|
|
53
46
|
|
|
54
47
|
expect(parsed.__type__).toBe("Uuid");
|
|
55
48
|
expect(parsed.data).toBe("12345678-9abc-def0-1234-56789abcdef0");
|
|
@@ -57,8 +50,8 @@ describe("JsonConvert", () => {
|
|
|
57
50
|
|
|
58
51
|
it("Serializes Set with __type__", () => {
|
|
59
52
|
const set = new Set([1, 2, 3]);
|
|
60
|
-
const
|
|
61
|
-
const parsed = JSON.parse(
|
|
53
|
+
const str = json.stringify(set);
|
|
54
|
+
const parsed = JSON.parse(str);
|
|
62
55
|
|
|
63
56
|
expect(parsed.__type__).toBe("Set");
|
|
64
57
|
expect(parsed.data).toEqual([1, 2, 3]);
|
|
@@ -69,8 +62,8 @@ describe("JsonConvert", () => {
|
|
|
69
62
|
["a", 1],
|
|
70
63
|
["b", 2],
|
|
71
64
|
]);
|
|
72
|
-
const
|
|
73
|
-
const parsed = JSON.parse(
|
|
65
|
+
const str = json.stringify(map);
|
|
66
|
+
const parsed = JSON.parse(str);
|
|
74
67
|
|
|
75
68
|
expect(parsed.__type__).toBe("Map");
|
|
76
69
|
expect(parsed.data).toEqual([
|
|
@@ -81,8 +74,8 @@ describe("JsonConvert", () => {
|
|
|
81
74
|
|
|
82
75
|
it("Serializes Error with __type__", () => {
|
|
83
76
|
const error = new Error("test error");
|
|
84
|
-
const
|
|
85
|
-
const parsed = JSON.parse(
|
|
77
|
+
const str = json.stringify(error);
|
|
78
|
+
const parsed = JSON.parse(str);
|
|
86
79
|
|
|
87
80
|
expect(parsed.__type__).toBe("Error");
|
|
88
81
|
expect(parsed.data.message).toBe("test error");
|
|
@@ -96,8 +89,8 @@ describe("JsonConvert", () => {
|
|
|
96
89
|
error.detail = { key: "value" };
|
|
97
90
|
(error as Error & { cause: Error }).cause = cause;
|
|
98
91
|
|
|
99
|
-
const
|
|
100
|
-
const parsed = JSON.parse(
|
|
92
|
+
const str = json.stringify(error);
|
|
93
|
+
const parsed = JSON.parse(str);
|
|
101
94
|
|
|
102
95
|
expect(parsed.__type__).toBe("Error");
|
|
103
96
|
expect(parsed.data.message).toBe("test error");
|
|
@@ -109,21 +102,21 @@ describe("JsonConvert", () => {
|
|
|
109
102
|
|
|
110
103
|
it("Hides Uint8Array with redactBytes option", () => {
|
|
111
104
|
const obj = { data: new TextEncoder().encode("hello") };
|
|
112
|
-
const
|
|
113
|
-
const parsed = JSON.parse(
|
|
105
|
+
const str = json.stringify(obj, { redactBytes: true });
|
|
106
|
+
const parsed = JSON.parse(str);
|
|
114
107
|
|
|
115
108
|
expect(parsed.data.data).toBe("__hidden__");
|
|
116
109
|
});
|
|
117
110
|
|
|
118
111
|
it("Transforms values with replacer option", () => {
|
|
119
112
|
const obj = { a: 1, b: 2, c: 3 };
|
|
120
|
-
const
|
|
113
|
+
const str = json.stringify(obj, {
|
|
121
114
|
replacer: (key, value) => {
|
|
122
115
|
if (key === "b") return undefined;
|
|
123
116
|
return value;
|
|
124
117
|
},
|
|
125
118
|
});
|
|
126
|
-
const parsed = JSON.parse(
|
|
119
|
+
const parsed = JSON.parse(str);
|
|
127
120
|
|
|
128
121
|
expect(parsed.a).toBe(1);
|
|
129
122
|
expect(parsed.b).toBeUndefined();
|
|
@@ -145,8 +138,8 @@ describe("JsonConvert", () => {
|
|
|
145
138
|
const promises = Array.from({ length: 100 }, (_, i) =>
|
|
146
139
|
Promise.resolve().then(() => {
|
|
147
140
|
const obj = createTestObject(i);
|
|
148
|
-
const
|
|
149
|
-
const parsed = parse<typeof obj>(
|
|
141
|
+
const str = json.stringify(obj);
|
|
142
|
+
const parsed = json.parse<typeof obj>(str);
|
|
150
143
|
|
|
151
144
|
// Verify all Dates restored correctly
|
|
152
145
|
expect(parsed.date).toBeInstanceOf(Date);
|
|
@@ -175,8 +168,8 @@ describe("JsonConvert", () => {
|
|
|
175
168
|
dates: [new Date("2024-01-01T00:00:00.000Z"), new Date("2024-12-31T23:59:59.000Z")],
|
|
176
169
|
};
|
|
177
170
|
|
|
178
|
-
const
|
|
179
|
-
const parsed = parse<typeof obj>(
|
|
171
|
+
const str = json.stringify(obj);
|
|
172
|
+
const parsed = json.parse<typeof obj>(str);
|
|
180
173
|
|
|
181
174
|
expect(parsed.level1.level2.level3.date).toBeInstanceOf(Date);
|
|
182
175
|
expect(parsed.level1.level2.level3.date.toISOString()).toBe("2024-06-15T12:00:00.000Z");
|
|
@@ -189,7 +182,7 @@ describe("JsonConvert", () => {
|
|
|
189
182
|
|
|
190
183
|
// Call stringify
|
|
191
184
|
const date = new Date("2024-03-15T10:30:00.000Z");
|
|
192
|
-
stringify({ date });
|
|
185
|
+
json.stringify({ date });
|
|
193
186
|
|
|
194
187
|
// Verify toJSON not changed
|
|
195
188
|
expect(Date.prototype.toJSON).toBe(originalToJSON);
|
|
@@ -199,15 +192,15 @@ describe("JsonConvert", () => {
|
|
|
199
192
|
const obj: Record<string, unknown> = { name: "test" };
|
|
200
193
|
obj["self"] = obj; // circular reference
|
|
201
194
|
|
|
202
|
-
expect(() => stringify(obj)).toThrow(TypeError);
|
|
203
|
-
expect(() => stringify(obj)).toThrow("Converting circular structure to JSON");
|
|
195
|
+
expect(() => json.stringify(obj)).toThrow(TypeError);
|
|
196
|
+
expect(() => json.stringify(obj)).toThrow("Converting circular structure to JSON");
|
|
204
197
|
});
|
|
205
198
|
|
|
206
199
|
it("Detects circular reference in array", () => {
|
|
207
200
|
const arr: unknown[] = [1, 2];
|
|
208
201
|
arr.push(arr); // array circular reference
|
|
209
202
|
|
|
210
|
-
expect(() => stringify(arr)).toThrow("Converting circular structure to JSON");
|
|
203
|
+
expect(() => json.stringify(arr)).toThrow("Converting circular structure to JSON");
|
|
211
204
|
});
|
|
212
205
|
|
|
213
206
|
it("Serializes custom object with toJSON method", () => {
|
|
@@ -218,8 +211,8 @@ describe("JsonConvert", () => {
|
|
|
218
211
|
},
|
|
219
212
|
};
|
|
220
213
|
|
|
221
|
-
const
|
|
222
|
-
expect(
|
|
214
|
+
const str = json.stringify(obj);
|
|
215
|
+
expect(str).toBe('"$100"');
|
|
223
216
|
});
|
|
224
217
|
|
|
225
218
|
it("Recursively processes toJSON if returning object", () => {
|
|
@@ -230,8 +223,8 @@ describe("JsonConvert", () => {
|
|
|
230
223
|
},
|
|
231
224
|
};
|
|
232
225
|
|
|
233
|
-
const
|
|
234
|
-
const parsed = JSON.parse(
|
|
226
|
+
const str = json.stringify(obj);
|
|
227
|
+
const parsed = JSON.parse(str);
|
|
235
228
|
|
|
236
229
|
expect(parsed.converted).toBe(true);
|
|
237
230
|
expect(parsed.date.__type__).toBe("Date");
|
|
@@ -244,12 +237,12 @@ describe("JsonConvert", () => {
|
|
|
244
237
|
|
|
245
238
|
describe("parse()", () => {
|
|
246
239
|
it("Converts null to undefined", () => {
|
|
247
|
-
expect(parse("null")).toBe(undefined);
|
|
240
|
+
expect(json.parse("null")).toBe(undefined);
|
|
248
241
|
});
|
|
249
242
|
|
|
250
243
|
it("Restores Date", () => {
|
|
251
|
-
const
|
|
252
|
-
const result = parse(
|
|
244
|
+
const str = '{"__type__":"Date","data":"2024-03-15T10:30:00.000Z"}';
|
|
245
|
+
const result = json.parse(str);
|
|
253
246
|
|
|
254
247
|
expect(result).toBeInstanceOf(Date);
|
|
255
248
|
expect((result as Date).toISOString()).toBe("2024-03-15T10:30:00.000Z");
|
|
@@ -257,8 +250,8 @@ describe("JsonConvert", () => {
|
|
|
257
250
|
|
|
258
251
|
it("Restores DateTime", () => {
|
|
259
252
|
const dt = new DateTime(2024, 3, 15, 10, 30);
|
|
260
|
-
const
|
|
261
|
-
const result = parse(
|
|
253
|
+
const str = json.stringify(dt);
|
|
254
|
+
const result = json.parse(str);
|
|
262
255
|
|
|
263
256
|
expect(result).toBeInstanceOf(DateTime);
|
|
264
257
|
expect((result as DateTime).year).toBe(2024);
|
|
@@ -268,8 +261,8 @@ describe("JsonConvert", () => {
|
|
|
268
261
|
|
|
269
262
|
it("Restores DateOnly", () => {
|
|
270
263
|
const d = new DateOnly(2024, 3, 15);
|
|
271
|
-
const
|
|
272
|
-
const result = parse(
|
|
264
|
+
const str = json.stringify(d);
|
|
265
|
+
const result = json.parse(str);
|
|
273
266
|
|
|
274
267
|
expect(result).toBeInstanceOf(DateOnly);
|
|
275
268
|
expect((result as DateOnly).year).toBe(2024);
|
|
@@ -277,32 +270,32 @@ describe("JsonConvert", () => {
|
|
|
277
270
|
|
|
278
271
|
it("Restores Time", () => {
|
|
279
272
|
const t = new Time(10, 30, 45);
|
|
280
|
-
const
|
|
281
|
-
const result = parse(
|
|
273
|
+
const str = json.stringify(t);
|
|
274
|
+
const result = json.parse(str);
|
|
282
275
|
|
|
283
276
|
expect(result).toBeInstanceOf(Time);
|
|
284
277
|
expect((result as Time).hour).toBe(10);
|
|
285
278
|
});
|
|
286
279
|
|
|
287
280
|
it("Restores Uuid", () => {
|
|
288
|
-
const
|
|
289
|
-
const result = parse(
|
|
281
|
+
const str = '{"__type__":"Uuid","data":"12345678-9abc-def0-1234-56789abcdef0"}';
|
|
282
|
+
const result = json.parse(str);
|
|
290
283
|
|
|
291
284
|
expect(result).toBeInstanceOf(Uuid);
|
|
292
285
|
expect((result as Uuid).toString()).toBe("12345678-9abc-def0-1234-56789abcdef0");
|
|
293
286
|
});
|
|
294
287
|
|
|
295
288
|
it("Restores Set", () => {
|
|
296
|
-
const
|
|
297
|
-
const result = parse(
|
|
289
|
+
const str = '{"__type__":"Set","data":[1,2,3]}';
|
|
290
|
+
const result = json.parse(str);
|
|
298
291
|
|
|
299
292
|
expect(result).toBeInstanceOf(Set);
|
|
300
293
|
expect(Array.from(result as Set<number>)).toEqual([1, 2, 3]);
|
|
301
294
|
});
|
|
302
295
|
|
|
303
296
|
it("Restores Map", () => {
|
|
304
|
-
const
|
|
305
|
-
const result = parse(
|
|
297
|
+
const str = '{"__type__":"Map","data":[["a",1],["b",2]]}';
|
|
298
|
+
const result = json.parse(str);
|
|
306
299
|
|
|
307
300
|
expect(result).toBeInstanceOf(Map);
|
|
308
301
|
expect((result as Map<string, number>).get("a")).toBe(1);
|
|
@@ -319,8 +312,8 @@ describe("JsonConvert", () => {
|
|
|
319
312
|
error.detail = { key: "value" };
|
|
320
313
|
error.cause = cause;
|
|
321
314
|
|
|
322
|
-
const
|
|
323
|
-
const result = parse<typeof error>(
|
|
315
|
+
const str = json.stringify(error);
|
|
316
|
+
const result = json.parse<typeof error>(str);
|
|
324
317
|
|
|
325
318
|
expect(result).toBeInstanceOf(Error);
|
|
326
319
|
expect(result.message).toBe("test error");
|
|
@@ -331,8 +324,8 @@ describe("JsonConvert", () => {
|
|
|
331
324
|
});
|
|
332
325
|
|
|
333
326
|
it("Restores Uint8Array", () => {
|
|
334
|
-
const
|
|
335
|
-
const result = parse(
|
|
327
|
+
const str = '{"__type__":"Uint8Array","data":"68656c6c6f"}';
|
|
328
|
+
const result = json.parse(str);
|
|
336
329
|
|
|
337
330
|
expect(result).toBeInstanceOf(Uint8Array);
|
|
338
331
|
expect(new TextDecoder().decode(result as Uint8Array)).toBe("hello");
|
|
@@ -350,8 +343,8 @@ describe("JsonConvert", () => {
|
|
|
350
343
|
bytes: new TextEncoder().encode("hello"),
|
|
351
344
|
};
|
|
352
345
|
|
|
353
|
-
const
|
|
354
|
-
const result = parse<typeof original>(
|
|
346
|
+
const str = json.stringify(original);
|
|
347
|
+
const result = json.parse<typeof original>(str);
|
|
355
348
|
|
|
356
349
|
expect(result.date).toBeInstanceOf(Date);
|
|
357
350
|
expect(result.dateTime).toBeInstanceOf(DateTime);
|
|
@@ -365,23 +358,23 @@ describe("JsonConvert", () => {
|
|
|
365
358
|
|
|
366
359
|
it("Data serialized with redactBytes throws error on parse", () => {
|
|
367
360
|
const obj = { data: new TextEncoder().encode("hello") };
|
|
368
|
-
const
|
|
361
|
+
const str = json.stringify(obj, { redactBytes: true });
|
|
369
362
|
|
|
370
363
|
// "__hidden__" is data serialized with redactBytes option, so parse throws error
|
|
371
|
-
expect(() => parse<typeof obj>(
|
|
364
|
+
expect(() => json.parse<typeof obj>(str)).toThrow(
|
|
372
365
|
"Uint8Array serialized with redactBytes option cannot be restored via parse",
|
|
373
366
|
);
|
|
374
367
|
});
|
|
375
368
|
|
|
376
369
|
it("Invalid JSON throws error", () => {
|
|
377
|
-
expect(() => parse("invalid json")).toThrow("JSON parsing error");
|
|
370
|
+
expect(() => json.parse("invalid json")).toThrow("JSON parsing error");
|
|
378
371
|
});
|
|
379
372
|
|
|
380
373
|
it("In DEV mode, full JSON included in error message", () => {
|
|
381
374
|
const longJson = "x".repeat(2000);
|
|
382
375
|
|
|
383
376
|
try {
|
|
384
|
-
parse(longJson);
|
|
377
|
+
json.parse(longJson);
|
|
385
378
|
expect.fail("Error should be thrown");
|
|
386
379
|
} catch (err) {
|
|
387
380
|
const message = (err as Error).message;
|
|
@@ -394,7 +387,7 @@ describe("JsonConvert", () => {
|
|
|
394
387
|
const shortJson = "invalid";
|
|
395
388
|
|
|
396
389
|
try {
|
|
397
|
-
parse(shortJson);
|
|
390
|
+
json.parse(shortJson);
|
|
398
391
|
expect.fail("Error should be thrown");
|
|
399
392
|
} catch (err) {
|
|
400
393
|
const message = (err as Error).message;
|
|
@@ -1,139 +1,134 @@
|
|
|
1
1
|
import { describe, it, expect } from "vitest";
|
|
2
|
-
import {
|
|
3
|
-
numParseInt as parseInt,
|
|
4
|
-
numParseRoundedInt as parseRoundedInt,
|
|
5
|
-
numParseFloat as parseFloat,
|
|
6
|
-
numFormat as format,
|
|
7
|
-
} from "@simplysm/core-common";
|
|
2
|
+
import { num } from "@simplysm/core-common";
|
|
8
3
|
|
|
9
4
|
describe("number utils", () => {
|
|
10
|
-
//#region
|
|
5
|
+
//#region parseInt
|
|
11
6
|
|
|
12
|
-
describe("
|
|
7
|
+
describe("parseInt()", () => {
|
|
13
8
|
it("Parses negative string", () => {
|
|
14
|
-
expect(parseInt("-123")).toBe(-123);
|
|
9
|
+
expect(num.parseInt("-123")).toBe(-123);
|
|
15
10
|
});
|
|
16
11
|
|
|
17
12
|
it("Floating string returns only integer part", () => {
|
|
18
|
-
expect(parseInt("123.45")).toBe(123);
|
|
13
|
+
expect(num.parseInt("123.45")).toBe(123);
|
|
19
14
|
});
|
|
20
15
|
|
|
21
16
|
it("Number type discards decimal places", () => {
|
|
22
|
-
expect(parseInt(123.7)).toBe(123);
|
|
23
|
-
expect(parseInt(123.3)).toBe(123);
|
|
17
|
+
expect(num.parseInt(123.7)).toBe(123);
|
|
18
|
+
expect(num.parseInt(123.3)).toBe(123);
|
|
24
19
|
});
|
|
25
20
|
|
|
26
21
|
it("Removes non-numeric characters then parses", () => {
|
|
27
|
-
expect(parseInt("$1,234")).toBe(1234);
|
|
28
|
-
expect(parseInt("abc123")).toBe(123);
|
|
22
|
+
expect(num.parseInt("$1,234")).toBe(1234);
|
|
23
|
+
expect(num.parseInt("abc123")).toBe(123);
|
|
29
24
|
});
|
|
30
25
|
|
|
31
26
|
it("Handles minus sign between characters", () => {
|
|
32
|
-
expect(parseInt("abc-123def")).toBe(-123);
|
|
33
|
-
expect(parseInt("abc-456def")).toBe(-456);
|
|
27
|
+
expect(num.parseInt("abc-123def")).toBe(-123);
|
|
28
|
+
expect(num.parseInt("abc-456def")).toBe(-456);
|
|
34
29
|
});
|
|
35
30
|
|
|
36
31
|
it("Empty string returns undefined", () => {
|
|
37
|
-
expect(parseInt("")).toBe(undefined);
|
|
32
|
+
expect(num.parseInt("")).toBe(undefined);
|
|
38
33
|
});
|
|
39
34
|
|
|
40
35
|
it("String with no digits returns undefined", () => {
|
|
41
|
-
expect(parseInt("abc")).toBe(undefined);
|
|
36
|
+
expect(num.parseInt("abc")).toBe(undefined);
|
|
42
37
|
});
|
|
43
38
|
|
|
44
39
|
it("Non-string type returns undefined", () => {
|
|
45
|
-
expect(parseInt(null)).toBe(undefined);
|
|
46
|
-
expect(parseInt(undefined)).toBe(undefined);
|
|
47
|
-
expect(parseInt({})).toBe(undefined);
|
|
40
|
+
expect(num.parseInt(null)).toBe(undefined);
|
|
41
|
+
expect(num.parseInt(undefined)).toBe(undefined);
|
|
42
|
+
expect(num.parseInt({})).toBe(undefined);
|
|
48
43
|
});
|
|
49
44
|
});
|
|
50
45
|
|
|
51
46
|
//#endregion
|
|
52
47
|
|
|
53
|
-
//#region
|
|
48
|
+
//#region parseRoundedInt
|
|
54
49
|
|
|
55
|
-
describe("
|
|
50
|
+
describe("parseRoundedInt()", () => {
|
|
56
51
|
it("Rounds floating string to integer", () => {
|
|
57
|
-
expect(parseRoundedInt("123.5")).toBe(124);
|
|
58
|
-
expect(parseRoundedInt("123.4")).toBe(123);
|
|
52
|
+
expect(num.parseRoundedInt("123.5")).toBe(124);
|
|
53
|
+
expect(num.parseRoundedInt("123.4")).toBe(123);
|
|
59
54
|
});
|
|
60
55
|
|
|
61
56
|
it("Rounds number type", () => {
|
|
62
|
-
expect(parseRoundedInt(123.7)).toBe(124);
|
|
57
|
+
expect(num.parseRoundedInt(123.7)).toBe(124);
|
|
63
58
|
});
|
|
64
59
|
|
|
65
60
|
it("Returns undefined if not parseable", () => {
|
|
66
|
-
expect(parseRoundedInt("abc")).toBe(undefined);
|
|
61
|
+
expect(num.parseRoundedInt("abc")).toBe(undefined);
|
|
67
62
|
});
|
|
68
63
|
});
|
|
69
64
|
|
|
70
65
|
//#endregion
|
|
71
66
|
|
|
72
|
-
//#region
|
|
67
|
+
//#region parseFloat
|
|
73
68
|
|
|
74
|
-
describe("
|
|
69
|
+
describe("parseFloat()", () => {
|
|
75
70
|
it("Parses negative floating string", () => {
|
|
76
|
-
expect(parseFloat("-123.45")).toBe(-123.45);
|
|
71
|
+
expect(num.parseFloat("-123.45")).toBe(-123.45);
|
|
77
72
|
});
|
|
78
73
|
|
|
79
74
|
it("Number type returned as-is", () => {
|
|
80
|
-
expect(parseFloat(123.45)).toBe(123.45);
|
|
75
|
+
expect(num.parseFloat(123.45)).toBe(123.45);
|
|
81
76
|
});
|
|
82
77
|
|
|
83
78
|
it("Removes non-numeric characters then parses", () => {
|
|
84
|
-
expect(parseFloat("$1,234.56")).toBe(1234.56);
|
|
79
|
+
expect(num.parseFloat("$1,234.56")).toBe(1234.56);
|
|
85
80
|
});
|
|
86
81
|
|
|
87
82
|
it("Empty string returns undefined", () => {
|
|
88
|
-
expect(parseFloat("")).toBe(undefined);
|
|
83
|
+
expect(num.parseFloat("")).toBe(undefined);
|
|
89
84
|
});
|
|
90
85
|
|
|
91
86
|
it("String with no digits returns undefined", () => {
|
|
92
|
-
expect(parseFloat("abc")).toBe(undefined);
|
|
87
|
+
expect(num.parseFloat("abc")).toBe(undefined);
|
|
93
88
|
});
|
|
94
89
|
|
|
95
90
|
it("Non-string type returns undefined", () => {
|
|
96
|
-
expect(parseFloat(null)).toBe(undefined);
|
|
97
|
-
expect(parseFloat(undefined)).toBe(undefined);
|
|
91
|
+
expect(num.parseFloat(null)).toBe(undefined);
|
|
92
|
+
expect(num.parseFloat(undefined)).toBe(undefined);
|
|
98
93
|
});
|
|
99
94
|
});
|
|
100
95
|
|
|
101
96
|
//#endregion
|
|
102
97
|
|
|
103
|
-
//#region
|
|
98
|
+
//#region format
|
|
104
99
|
|
|
105
|
-
describe("
|
|
100
|
+
describe("format()", () => {
|
|
106
101
|
it("Applies thousands separator", () => {
|
|
107
|
-
expect(format(1234567)).toBe("1,234,567");
|
|
102
|
+
expect(num.format(1234567)).toBe("1,234,567");
|
|
108
103
|
});
|
|
109
104
|
|
|
110
105
|
it("Applies thousands separator to float", () => {
|
|
111
|
-
const result = format(1234567.89);
|
|
106
|
+
const result = num.format(1234567.89);
|
|
112
107
|
expect(result).toContain("1,234,567");
|
|
113
108
|
});
|
|
114
109
|
|
|
115
110
|
it("Specifies maximum decimal places", () => {
|
|
116
|
-
expect(format(123.456, { max: 2 })).toBe("123.46");
|
|
111
|
+
expect(num.format(123.456, { max: 2 })).toBe("123.46");
|
|
117
112
|
});
|
|
118
113
|
|
|
119
114
|
it("Specifies minimum decimal places", () => {
|
|
120
|
-
expect(format(123, { min: 2 })).toBe("123.00");
|
|
115
|
+
expect(num.format(123, { min: 2 })).toBe("123.00");
|
|
121
116
|
});
|
|
122
117
|
|
|
123
118
|
it("Specifies both max and min decimal places", () => {
|
|
124
|
-
expect(format(123.4, { max: 3, min: 2 })).toBe("123.40");
|
|
119
|
+
expect(num.format(123.4, { max: 3, min: 2 })).toBe("123.40");
|
|
125
120
|
});
|
|
126
121
|
|
|
127
122
|
it("Undefined returns undefined", () => {
|
|
128
|
-
expect(format(undefined)).toBe(undefined);
|
|
123
|
+
expect(num.format(undefined)).toBe(undefined);
|
|
129
124
|
});
|
|
130
125
|
|
|
131
126
|
it("Formats zero", () => {
|
|
132
|
-
expect(format(0)).toBe("0");
|
|
127
|
+
expect(num.format(0)).toBe("0");
|
|
133
128
|
});
|
|
134
129
|
|
|
135
130
|
it("Formats negative number", () => {
|
|
136
|
-
expect(format(-1234567)).toBe("-1,234,567");
|
|
131
|
+
expect(num.format(-1234567)).toBe("-1,234,567");
|
|
137
132
|
});
|
|
138
133
|
});
|
|
139
134
|
|