@simplysm/core-common 13.0.69 → 13.0.71
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 +66 -267
- package/dist/common.types.d.ts +14 -14
- package/dist/errors/argument-error.d.ts +10 -10
- package/dist/errors/argument-error.d.ts.map +1 -1
- package/dist/errors/argument-error.js +2 -2
- package/dist/errors/argument-error.js.map +1 -1
- package/dist/errors/not-implemented-error.d.ts +8 -8
- package/dist/errors/not-implemented-error.js +2 -2
- package/dist/errors/not-implemented-error.js.map +1 -1
- package/dist/errors/sd-error.d.ts +10 -10
- package/dist/errors/sd-error.d.ts.map +1 -1
- package/dist/errors/timeout-error.d.ts +10 -10
- package/dist/errors/timeout-error.js +3 -3
- package/dist/errors/timeout-error.js.map +1 -1
- package/dist/extensions/arr-ext.d.ts +2 -2
- package/dist/extensions/arr-ext.helpers.d.ts +8 -8
- package/dist/extensions/arr-ext.helpers.js +1 -1
- package/dist/extensions/arr-ext.helpers.js.map +1 -1
- package/dist/extensions/arr-ext.js +13 -13
- package/dist/extensions/arr-ext.js.map +1 -1
- package/dist/extensions/arr-ext.types.d.ts +57 -57
- package/dist/extensions/arr-ext.types.d.ts.map +1 -1
- package/dist/extensions/map-ext.d.ts +16 -16
- package/dist/extensions/set-ext.d.ts +11 -11
- package/dist/features/debounce-queue.d.ts +17 -15
- package/dist/features/debounce-queue.d.ts.map +1 -1
- package/dist/features/debounce-queue.js +6 -6
- package/dist/features/debounce-queue.js.map +1 -1
- package/dist/features/event-emitter.d.ts +20 -20
- package/dist/features/event-emitter.js +17 -17
- package/dist/features/serial-queue.d.ts +11 -11
- package/dist/features/serial-queue.js +5 -5
- package/dist/features/serial-queue.js.map +1 -1
- package/dist/globals.d.ts +4 -4
- package/dist/types/date-only.d.ts +64 -64
- package/dist/types/date-only.d.ts.map +1 -1
- package/dist/types/date-only.js +63 -63
- package/dist/types/date-time.d.ts +37 -37
- package/dist/types/date-time.d.ts.map +1 -1
- package/dist/types/date-time.js +54 -37
- package/dist/types/date-time.js.map +1 -1
- package/dist/types/lazy-gc-map.d.ts +26 -26
- package/dist/types/lazy-gc-map.d.ts.map +1 -1
- package/dist/types/lazy-gc-map.js +26 -26
- package/dist/types/lazy-gc-map.js.map +1 -1
- package/dist/types/time.d.ts +25 -25
- package/dist/types/time.d.ts.map +1 -1
- package/dist/types/time.js +25 -25
- package/dist/types/time.js.map +1 -1
- package/dist/types/uuid.d.ts +11 -11
- package/dist/types/uuid.d.ts.map +1 -1
- package/dist/types/uuid.js +12 -12
- package/dist/types/uuid.js.map +1 -1
- package/dist/utils/bytes.d.ts +17 -17
- package/dist/utils/bytes.js +4 -4
- package/dist/utils/bytes.js.map +1 -1
- package/dist/utils/date-format.d.ts +45 -45
- package/dist/utils/date-format.js +1 -1
- package/dist/utils/date-format.js.map +1 -1
- package/dist/utils/error.d.ts +4 -4
- package/dist/utils/json.d.ts +17 -17
- package/dist/utils/json.js +3 -3
- package/dist/utils/json.js.map +1 -1
- package/dist/utils/num.d.ts +23 -23
- package/dist/utils/obj.d.ts +111 -111
- package/dist/utils/obj.d.ts.map +1 -1
- package/dist/utils/obj.js +3 -3
- package/dist/utils/obj.js.map +1 -1
- package/dist/utils/path.d.ts +10 -10
- package/dist/utils/primitive.d.ts +5 -5
- package/dist/utils/primitive.js +1 -1
- package/dist/utils/primitive.js.map +1 -1
- package/dist/utils/str.d.ts +46 -46
- package/dist/utils/str.d.ts.map +1 -1
- package/dist/utils/str.js +5 -5
- package/dist/utils/str.js.map +1 -1
- package/dist/utils/template-strings.d.ts +26 -26
- package/dist/utils/transferable.d.ts +18 -18
- package/dist/utils/transferable.js +1 -1
- package/dist/utils/transferable.js.map +1 -1
- package/dist/utils/wait.d.ts +9 -9
- package/dist/utils/xml.d.ts +13 -13
- package/dist/utils/xml.d.ts.map +1 -1
- package/dist/utils/xml.js +1 -0
- package/dist/utils/xml.js.map +1 -1
- package/dist/zip/sd-zip.d.ts +22 -22
- package/dist/zip/sd-zip.js +16 -16
- package/package.json +4 -4
- package/src/common.types.ts +17 -17
- package/src/errors/argument-error.ts +15 -15
- package/src/errors/not-implemented-error.ts +9 -9
- package/src/errors/sd-error.ts +12 -12
- package/src/errors/timeout-error.ts +12 -12
- package/src/extensions/arr-ext.helpers.ts +10 -10
- package/src/extensions/arr-ext.ts +57 -57
- package/src/extensions/arr-ext.types.ts +59 -59
- package/src/extensions/map-ext.ts +16 -16
- package/src/extensions/set-ext.ts +11 -11
- package/src/features/debounce-queue.ts +21 -19
- package/src/features/event-emitter.ts +25 -25
- package/src/features/serial-queue.ts +13 -13
- package/src/globals.ts +4 -4
- package/src/index.ts +1 -1
- package/src/types/date-only.ts +83 -83
- package/src/types/date-time.ts +64 -44
- package/src/types/lazy-gc-map.ts +45 -45
- package/src/types/time.ts +34 -34
- package/src/types/uuid.ts +17 -17
- package/src/utils/bytes.ts +35 -35
- package/src/utils/date-format.ts +65 -65
- package/src/utils/error.ts +4 -4
- package/src/utils/json.ts +39 -39
- package/src/utils/num.ts +23 -23
- package/src/utils/obj.ts +138 -138
- package/src/utils/path.ts +10 -10
- package/src/utils/primitive.ts +6 -6
- package/src/utils/str.ts +260 -261
- package/src/utils/template-strings.ts +29 -29
- package/src/utils/transferable.ts +284 -284
- package/src/utils/wait.ts +10 -10
- package/src/utils/xml.ts +20 -19
- package/src/zip/sd-zip.ts +25 -25
- package/tests/errors/errors.spec.ts +80 -0
- package/tests/extensions/array-extension.spec.ts +796 -0
- package/tests/extensions/map-extension.spec.ts +147 -0
- package/tests/extensions/set-extension.spec.ts +74 -0
- package/tests/types/date-only.spec.ts +638 -0
- package/tests/types/date-time.spec.ts +391 -0
- package/tests/types/lazy-gc-map.spec.ts +692 -0
- package/tests/types/time.spec.ts +559 -0
- package/tests/types/uuid.spec.ts +74 -0
- package/tests/utils/bytes-utils.spec.ts +230 -0
- package/tests/utils/date-format.spec.ts +373 -0
- package/tests/utils/debounce-queue.spec.ts +272 -0
- package/tests/utils/json.spec.ts +486 -0
- package/tests/utils/number.spec.ts +157 -0
- package/tests/utils/object.spec.ts +829 -0
- package/tests/utils/path.spec.ts +78 -0
- package/tests/utils/primitive.spec.ts +43 -0
- package/tests/utils/sd-event-emitter.spec.ts +216 -0
- package/tests/utils/serial-queue.spec.ts +365 -0
- package/tests/utils/string.spec.ts +281 -0
- package/tests/utils/template-strings.spec.ts +57 -0
- package/tests/utils/transferable.spec.ts +703 -0
- package/tests/utils/wait.spec.ts +145 -0
- package/tests/utils/xml.spec.ts +146 -0
- package/tests/zip/sd-zip.spec.ts +238 -0
- package/docs/extensions.md +0 -503
- package/docs/features.md +0 -109
- package/docs/types.md +0 -486
- package/docs/utils.md +0 -780
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import "../../src/extensions/map-ext";
|
|
3
|
+
|
|
4
|
+
describe("Map prototype extensions", () => {
|
|
5
|
+
//#region getOrCreate
|
|
6
|
+
|
|
7
|
+
describe("getOrCreate()", () => {
|
|
8
|
+
it("Sets and returns value directly if key not exists", () => {
|
|
9
|
+
const map = new Map<string, number>();
|
|
10
|
+
|
|
11
|
+
const result = map.getOrCreate("key", 100);
|
|
12
|
+
|
|
13
|
+
expect(result).toBe(100);
|
|
14
|
+
expect(map.get("key")).toBe(100);
|
|
15
|
+
expect(map.size).toBe(1);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("Calls factory function and sets value if key not exists", () => {
|
|
19
|
+
const map = new Map<string, number[]>();
|
|
20
|
+
let factoryCalled = false;
|
|
21
|
+
|
|
22
|
+
const result = map.getOrCreate("key", () => {
|
|
23
|
+
factoryCalled = true;
|
|
24
|
+
return [1, 2, 3];
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
expect(factoryCalled).toBe(true);
|
|
28
|
+
expect(result).toEqual([1, 2, 3]);
|
|
29
|
+
expect(map.get("key")).toEqual([1, 2, 3]);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("Returns existing value if key exists, factory not called", () => {
|
|
33
|
+
const map = new Map<string, number>();
|
|
34
|
+
map.set("key", 50);
|
|
35
|
+
let factoryCalled = false;
|
|
36
|
+
|
|
37
|
+
const result = map.getOrCreate("key", () => {
|
|
38
|
+
factoryCalled = true;
|
|
39
|
+
return 100;
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
expect(factoryCalled).toBe(false);
|
|
43
|
+
expect(result).toBe(50);
|
|
44
|
+
expect(map.get("key")).toBe(50);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("Returns existing value if key exists (direct value)", () => {
|
|
48
|
+
const map = new Map<string, number>();
|
|
49
|
+
map.set("key", 50);
|
|
50
|
+
|
|
51
|
+
const result = map.getOrCreate("key", 100);
|
|
52
|
+
|
|
53
|
+
expect(result).toBe(50);
|
|
54
|
+
expect(map.size).toBe(1);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("Can set empty array as default value", () => {
|
|
58
|
+
const map = new Map<string, number[]>();
|
|
59
|
+
|
|
60
|
+
const arr = map.getOrCreate("key", []);
|
|
61
|
+
arr.push(1, 2, 3);
|
|
62
|
+
|
|
63
|
+
expect(map.get("key")).toEqual([1, 2, 3]);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("Can create complex object with factory function", () => {
|
|
67
|
+
const map = new Map<string, { count: number; items: string[] }>();
|
|
68
|
+
|
|
69
|
+
const obj = map.getOrCreate("key", () => ({ count: 0, items: [] }));
|
|
70
|
+
obj.count++;
|
|
71
|
+
obj.items.push("item1");
|
|
72
|
+
|
|
73
|
+
expect(map.get("key")).toEqual({ count: 1, items: ["item1"] });
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("Wraps function value in factory when V type is function", () => {
|
|
77
|
+
const map = new Map<string, () => number>();
|
|
78
|
+
const fn = () => 42;
|
|
79
|
+
|
|
80
|
+
// Function value must be wrapped in factory to store
|
|
81
|
+
const result = map.getOrCreate("key", () => fn);
|
|
82
|
+
|
|
83
|
+
expect(result).toBe(fn);
|
|
84
|
+
expect(result()).toBe(42);
|
|
85
|
+
expect(map.get("key")).toBe(fn);
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
//#endregion
|
|
90
|
+
|
|
91
|
+
//#region update
|
|
92
|
+
|
|
93
|
+
describe("update()", () => {
|
|
94
|
+
it("Updates value of existing key", () => {
|
|
95
|
+
const map = new Map<string, number>();
|
|
96
|
+
map.set("key", 10);
|
|
97
|
+
|
|
98
|
+
map.update("key", (v) => (v ?? 0) + 5);
|
|
99
|
+
|
|
100
|
+
expect(map.get("key")).toBe(15);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("Passes undefined for non-existing key", () => {
|
|
104
|
+
const map = new Map<string, number>();
|
|
105
|
+
let receivedValue: number | undefined;
|
|
106
|
+
|
|
107
|
+
map.update("key", (v) => {
|
|
108
|
+
receivedValue = v;
|
|
109
|
+
return 100;
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
expect(receivedValue).toBeUndefined();
|
|
113
|
+
expect(map.get("key")).toBe(100);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it("Replaces value with callback return value", () => {
|
|
117
|
+
const map = new Map<string, string>();
|
|
118
|
+
map.set("key", "hello");
|
|
119
|
+
|
|
120
|
+
map.update("key", (v) => (v ?? "") + " world");
|
|
121
|
+
|
|
122
|
+
expect(map.get("key")).toBe("hello world");
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it("Can update object value", () => {
|
|
126
|
+
const map = new Map<string, { count: number }>();
|
|
127
|
+
map.set("key", { count: 5 });
|
|
128
|
+
|
|
129
|
+
map.update("key", (v) => ({ count: (v?.count ?? 0) + 1 }));
|
|
130
|
+
|
|
131
|
+
expect(map.get("key")).toEqual({ count: 6 });
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it("Can update multiple times sequentially", () => {
|
|
135
|
+
const map = new Map<string, number>();
|
|
136
|
+
map.set("counter", 0);
|
|
137
|
+
|
|
138
|
+
map.update("counter", (v) => (v ?? 0) + 1);
|
|
139
|
+
map.update("counter", (v) => (v ?? 0) + 1);
|
|
140
|
+
map.update("counter", (v) => (v ?? 0) + 1);
|
|
141
|
+
|
|
142
|
+
expect(map.get("counter")).toBe(3);
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
//#endregion
|
|
147
|
+
});
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import "../../src/extensions/set-ext";
|
|
3
|
+
|
|
4
|
+
describe("Set prototype extensions", () => {
|
|
5
|
+
//#region adds
|
|
6
|
+
|
|
7
|
+
describe("adds()", () => {
|
|
8
|
+
it("Adds multiple items at once", () => {
|
|
9
|
+
const set = new Set([1, 2, 3]);
|
|
10
|
+
const result = set.adds(4, 5, 6);
|
|
11
|
+
|
|
12
|
+
expect(result).toBe(set); // returns original
|
|
13
|
+
expect(set.size).toBe(6);
|
|
14
|
+
expect(set.has(4)).toBe(true);
|
|
15
|
+
expect(set.has(5)).toBe(true);
|
|
16
|
+
expect(set.has(6)).toBe(true);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it("Automatically removes duplicate items", () => {
|
|
20
|
+
const set = new Set([1, 2, 3]);
|
|
21
|
+
set.adds(2, 3, 4);
|
|
22
|
+
|
|
23
|
+
expect(set.size).toBe(4); // 1, 2, 3, 4
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("Adds multiple items to empty Set", () => {
|
|
27
|
+
const set = new Set<number>();
|
|
28
|
+
set.adds(1, 2, 3);
|
|
29
|
+
|
|
30
|
+
expect(set.size).toBe(3);
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
//#endregion
|
|
35
|
+
|
|
36
|
+
//#region toggle
|
|
37
|
+
|
|
38
|
+
describe("toggle()", () => {
|
|
39
|
+
it("Adds item if not exists", () => {
|
|
40
|
+
const set = new Set([1, 2, 3]);
|
|
41
|
+
const result = set.toggle(4);
|
|
42
|
+
|
|
43
|
+
expect(result).toBe(set); // returns original
|
|
44
|
+
expect(set.has(4)).toBe(true);
|
|
45
|
+
expect(set.size).toBe(4);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("Removes item if exists", () => {
|
|
49
|
+
const set = new Set([1, 2, 3]);
|
|
50
|
+
set.toggle(2);
|
|
51
|
+
|
|
52
|
+
expect(set.has(2)).toBe(false);
|
|
53
|
+
expect(set.size).toBe(2);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("Force adds with addOrDel='add'", () => {
|
|
57
|
+
const set = new Set([1, 2, 3]);
|
|
58
|
+
set.toggle(2, "add"); // already exists but add option
|
|
59
|
+
|
|
60
|
+
expect(set.has(2)).toBe(true);
|
|
61
|
+
expect(set.size).toBe(3); // unchanged
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("Force removes with addOrDel='del'", () => {
|
|
65
|
+
const set = new Set([1, 2, 3]);
|
|
66
|
+
set.toggle(4, "del"); // not exists but del option
|
|
67
|
+
|
|
68
|
+
expect(set.has(4)).toBe(false);
|
|
69
|
+
expect(set.size).toBe(3); // unchanged
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
//#endregion
|
|
74
|
+
});
|