dropcap 1.1.0 → 1.4.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/chunk-5N6O5FSZ.js +53 -0
- package/dist/chunk-5N6O5FSZ.js.map +1 -0
- package/dist/chunk-6X6FFIZN.js +9 -0
- package/dist/chunk-6X6FFIZN.js.map +1 -0
- package/dist/chunk-IEKJYSB2.js +38 -0
- package/dist/chunk-IEKJYSB2.js.map +1 -0
- package/dist/chunk-IL3H2YKT.js +20 -0
- package/dist/chunk-IL3H2YKT.js.map +1 -0
- package/dist/chunk-IQMQQI6W.js +35 -0
- package/dist/chunk-IQMQQI6W.js.map +1 -0
- package/dist/chunk-NIQOLOOJ.js +44 -0
- package/dist/chunk-NIQOLOOJ.js.map +1 -0
- package/dist/chunk-OURJLKNI.js +16 -0
- package/dist/chunk-OURJLKNI.js.map +1 -0
- package/dist/chunk-RXAMNVZW.js +9 -0
- package/dist/chunk-RXAMNVZW.js.map +1 -0
- package/dist/chunk-SDOWUYJK.js +1 -0
- package/dist/chunk-SDOWUYJK.js.map +1 -0
- package/dist/chunk-WBZCJMFI.js +14 -0
- package/dist/chunk-WBZCJMFI.js.map +1 -0
- package/dist/chunk-YPFVC6GE.js +29 -0
- package/dist/chunk-YPFVC6GE.js.map +1 -0
- package/dist/idb/idb.d.ts +45 -0
- package/dist/idb/idb.js +200 -0
- package/dist/idb/idb.js.map +1 -0
- package/dist/types/types.js +1 -0
- package/dist/types/types.js.map +1 -0
- package/dist/utils/utils-color-hash.js +4 -39
- package/dist/utils/utils-color-hash.js.map +1 -0
- package/dist/utils/utils-create-log.js +5 -51
- package/dist/utils/utils-create-log.js.map +1 -0
- package/dist/utils/utils-ensure-array.js +4 -4
- package/dist/utils/utils-ensure-array.js.map +1 -0
- package/dist/utils/utils-is.js +4 -30
- package/dist/utils/utils-is.js.map +1 -0
- package/dist/utils/utils-link.d.ts +3 -0
- package/dist/utils/utils-link.js +8 -0
- package/dist/utils/utils-link.js.map +1 -0
- package/dist/utils/utils-queue.d.ts +1 -1
- package/dist/utils/utils-queue.js +6 -86
- package/dist/utils/utils-queue.js.map +1 -0
- package/dist/utils/utils-safe.js +6 -50
- package/dist/utils/utils-safe.js.map +1 -0
- package/dist/utils/utils-unique.js +4 -11
- package/dist/utils/utils-unique.js.map +1 -0
- package/dist/utils/utils-unit.js +6 -81
- package/dist/utils/utils-unit.js.map +1 -0
- package/dist/utils/utils-wait.js +4 -4
- package/dist/utils/utils-wait.js.map +1 -0
- package/dist/utils/utils.d.ts +1 -0
- package/dist/utils/utils.js +34 -199
- package/dist/utils/utils.js.map +1 -0
- package/package.json +4 -6
- package/src/types/types.ts +0 -5
- package/src/utils/utils-color-hash.ts +0 -55
- package/src/utils/utils-create-log.ts +0 -16
- package/src/utils/utils-ensure-array.ts +0 -3
- package/src/utils/utils-is.ts +0 -32
- package/src/utils/utils-queue.ts +0 -58
- package/src/utils/utils-safe.ts +0 -23
- package/src/utils/utils-unique.ts +0 -11
- package/src/utils/utils-unit.ts +0 -35
- package/src/utils/utils-wait.ts +0 -3
- package/src/utils/utils.ts +0 -13
package/dist/utils/utils.js
CHANGED
|
@@ -1,202 +1,35 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
return x <= 0.03928 ? x / 12.92 : ((x + 0.055) / 1.055) ** 2.4;
|
|
34
|
-
};
|
|
35
|
-
return 0.2126 * c(r) + 0.7152 * c(g) + 0.0722 * c(b);
|
|
36
|
-
}
|
|
37
|
-
function contrast(l1, l2) {
|
|
38
|
-
return (Math.max(l1, l2) + 0.05) / (Math.min(l1, l2) + 0.05);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
// src/utils/utils-create-log.ts
|
|
42
|
-
function createLog(name) {
|
|
43
|
-
const color = colorHash(name);
|
|
44
|
-
const log = print.bind(null, "log", name, color);
|
|
45
|
-
log.warn = print.bind(null, "warn", name, color);
|
|
46
|
-
log.error = print.bind(null, "error", name, color);
|
|
47
|
-
return log;
|
|
48
|
-
}
|
|
49
|
-
function print(type, name, color, ...args) {
|
|
50
|
-
console[type](`%c[${name}]`, `color: ${color}`, ...args);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
// src/utils/utils-ensure-array.ts
|
|
54
|
-
function ensureArray(value) {
|
|
55
|
-
return Array.isArray(value) ? value : [value];
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// src/utils/utils-is.ts
|
|
59
|
-
var is = {
|
|
60
|
-
null: (v) => v === null,
|
|
61
|
-
undefined: (v) => v === void 0,
|
|
62
|
-
boolean: (v) => typeof v === "boolean",
|
|
63
|
-
number: (v) => typeof v === "number",
|
|
64
|
-
string: (v) => typeof v === "string",
|
|
65
|
-
symbol: (v) => typeof v === "symbol",
|
|
66
|
-
function: (v) => typeof v === "function",
|
|
67
|
-
object: (v) => Object.prototype.toString.call(v) === "[object Object]",
|
|
68
|
-
array: (v) => Array.isArray(v),
|
|
69
|
-
set: (v) => v instanceof Set,
|
|
70
|
-
map: (v) => v instanceof Map,
|
|
71
|
-
blob: (v) => v instanceof Blob,
|
|
72
|
-
date: (v) => v instanceof Date,
|
|
73
|
-
error: (v) => v instanceof Error,
|
|
74
|
-
regex: (v) => v instanceof RegExp,
|
|
75
|
-
promise: (v) => v instanceof Promise,
|
|
76
|
-
uint8Array: (v) => v instanceof Uint8Array,
|
|
77
|
-
uint16Array: (v) => v instanceof Uint16Array,
|
|
78
|
-
uint32Array: (v) => v instanceof Uint32Array,
|
|
79
|
-
nan: (v) => Number.isNaN(v),
|
|
80
|
-
numeric: (v) => !is.nan(Number(v)),
|
|
81
|
-
integer: (v) => Number.isInteger(v),
|
|
82
|
-
absent: (v) => v === null || v === void 0,
|
|
83
|
-
present: (v) => !is.absent(v),
|
|
84
|
-
primitive: (v) => v !== Object(v),
|
|
85
|
-
collection: (v) => is.array(v) || is.object(v),
|
|
86
|
-
compound: (v) => v !== null && typeof v === "object"
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
// src/utils/utils-safe.ts
|
|
90
|
-
async function safe(effect) {
|
|
91
|
-
try {
|
|
92
|
-
const result = is.function(effect) ? await effect() : await effect;
|
|
93
|
-
return [result, null];
|
|
94
|
-
} catch (e) {
|
|
95
|
-
const error = e instanceof Error ? e : new Error(String(e));
|
|
96
|
-
return [null, error];
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
function safeSync(effect) {
|
|
100
|
-
try {
|
|
101
|
-
const result = effect();
|
|
102
|
-
return [result, null];
|
|
103
|
-
} catch (e) {
|
|
104
|
-
const error = e instanceof Error ? e : new Error(String(e));
|
|
105
|
-
return [null, error];
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
// src/utils/utils-queue.ts
|
|
110
|
-
var Queue = class {
|
|
111
|
-
name;
|
|
112
|
-
tasks = [];
|
|
113
|
-
running = false;
|
|
114
|
-
constructor(name = null) {
|
|
115
|
-
this.name = name;
|
|
116
|
-
}
|
|
117
|
-
async add(fn) {
|
|
118
|
-
const task = { fn, result$: Promise.withResolvers() };
|
|
119
|
-
this.tasks.push(task);
|
|
120
|
-
this.start();
|
|
121
|
-
return await task.result$.promise;
|
|
122
|
-
}
|
|
123
|
-
wrap(fn, thisValue = null) {
|
|
124
|
-
return (async (...args) => {
|
|
125
|
-
return await this.add(() => fn.call(thisValue, ...args));
|
|
126
|
-
});
|
|
127
|
-
}
|
|
128
|
-
async wait() {
|
|
129
|
-
const lastTask = this.tasks.at(-1);
|
|
130
|
-
if (lastTask) await safe(lastTask.result$.promise);
|
|
131
|
-
}
|
|
132
|
-
isEmpty() {
|
|
133
|
-
return this.tasks.length === 0;
|
|
134
|
-
}
|
|
135
|
-
async start() {
|
|
136
|
-
if (this.running) return;
|
|
137
|
-
if (this.isEmpty()) return;
|
|
138
|
-
this.running = true;
|
|
139
|
-
while (this.tasks.length > 0) {
|
|
140
|
-
const task = this.tasks[0];
|
|
141
|
-
if (!task) break;
|
|
142
|
-
const [result, error] = await safe(task.fn);
|
|
143
|
-
this.tasks.shift();
|
|
144
|
-
if (error) {
|
|
145
|
-
task.result$.reject(error);
|
|
146
|
-
} else {
|
|
147
|
-
task.result$.resolve(result);
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
this.running = false;
|
|
151
|
-
}
|
|
152
|
-
};
|
|
153
|
-
|
|
154
|
-
// src/utils/utils-unique.ts
|
|
155
|
-
function unique(items, keyFn) {
|
|
156
|
-
if (!keyFn) return [...new Set(items)];
|
|
157
|
-
const seen = /* @__PURE__ */ new Set();
|
|
158
|
-
return items.filter((item) => {
|
|
159
|
-
const key = keyFn(item);
|
|
160
|
-
if (seen.has(key)) return false;
|
|
161
|
-
seen.add(key);
|
|
162
|
-
return true;
|
|
163
|
-
});
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
// src/utils/utils-unit.ts
|
|
167
|
-
var Unit = class {
|
|
168
|
-
$;
|
|
169
|
-
log = createLog(this.constructor.name);
|
|
170
|
-
#parent = null;
|
|
171
|
-
constructor(parent) {
|
|
172
|
-
this.$ = parent?.$ ?? this;
|
|
173
|
-
this.#parent = parent ?? null;
|
|
174
|
-
}
|
|
175
|
-
closest(Ancestor) {
|
|
176
|
-
let cursor = this.#parent;
|
|
177
|
-
while (cursor) {
|
|
178
|
-
if (cursor instanceof Ancestor) return cursor;
|
|
179
|
-
cursor = cursor.#parent;
|
|
180
|
-
}
|
|
181
|
-
return null;
|
|
182
|
-
}
|
|
183
|
-
never(message) {
|
|
184
|
-
const details = message ? `: ${message}` : "";
|
|
185
|
-
const error = new Error(`[${this.constructor.name}] This should never happen${details}`);
|
|
186
|
-
Error.captureStackTrace(error, this.never);
|
|
187
|
-
return error;
|
|
188
|
-
}
|
|
189
|
-
Error(message) {
|
|
190
|
-
const error = new Error(`[${this.constructor.name}] ${message}`);
|
|
191
|
-
Error.captureStackTrace(error, this.Error);
|
|
192
|
-
return error;
|
|
193
|
-
}
|
|
194
|
-
};
|
|
195
|
-
|
|
196
|
-
// src/utils/utils-wait.ts
|
|
197
|
-
async function wait(ms) {
|
|
198
|
-
await new Promise((r) => setTimeout(r, ms));
|
|
199
|
-
}
|
|
1
|
+
import "../chunk-SDOWUYJK.js";
|
|
2
|
+
import {
|
|
3
|
+
unique
|
|
4
|
+
} from "../chunk-OURJLKNI.js";
|
|
5
|
+
import {
|
|
6
|
+
Unit
|
|
7
|
+
} from "../chunk-IEKJYSB2.js";
|
|
8
|
+
import {
|
|
9
|
+
wait
|
|
10
|
+
} from "../chunk-RXAMNVZW.js";
|
|
11
|
+
import {
|
|
12
|
+
createLog
|
|
13
|
+
} from "../chunk-IL3H2YKT.js";
|
|
14
|
+
import {
|
|
15
|
+
colorHash
|
|
16
|
+
} from "../chunk-NIQOLOOJ.js";
|
|
17
|
+
import {
|
|
18
|
+
ensureArray
|
|
19
|
+
} from "../chunk-6X6FFIZN.js";
|
|
20
|
+
import {
|
|
21
|
+
link
|
|
22
|
+
} from "../chunk-WBZCJMFI.js";
|
|
23
|
+
import {
|
|
24
|
+
Queue
|
|
25
|
+
} from "../chunk-5N6O5FSZ.js";
|
|
26
|
+
import {
|
|
27
|
+
safe,
|
|
28
|
+
safeSync
|
|
29
|
+
} from "../chunk-YPFVC6GE.js";
|
|
30
|
+
import {
|
|
31
|
+
is
|
|
32
|
+
} from "../chunk-IQMQQI6W.js";
|
|
200
33
|
export {
|
|
201
34
|
Queue,
|
|
202
35
|
Unit,
|
|
@@ -204,8 +37,10 @@ export {
|
|
|
204
37
|
createLog,
|
|
205
38
|
ensureArray,
|
|
206
39
|
is,
|
|
40
|
+
link,
|
|
207
41
|
safe,
|
|
208
42
|
safeSync,
|
|
209
43
|
unique,
|
|
210
44
|
wait
|
|
211
45
|
};
|
|
46
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dropcap",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "imkost",
|
|
@@ -12,13 +12,11 @@
|
|
|
12
12
|
"release": "sh -c 'npm version ${1:-minor} && npm run build && npm publish' --"
|
|
13
13
|
},
|
|
14
14
|
"exports": {
|
|
15
|
+
"./idb": "./dist/idb/idb.js",
|
|
15
16
|
"./utils": "./dist/utils/utils.js",
|
|
16
|
-
"./types": "./dist/types/types.js"
|
|
17
|
-
"./utils/ts": "./src/utils/utils.ts",
|
|
18
|
-
"./types/ts": "./src/types/types.ts"
|
|
17
|
+
"./types": "./dist/types/types.js"
|
|
19
18
|
},
|
|
20
19
|
"files": [
|
|
21
|
-
"dist"
|
|
22
|
-
"src"
|
|
20
|
+
"dist"
|
|
23
21
|
]
|
|
24
22
|
}
|
package/src/types/types.ts
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
export type Rgb = [number, number, number]
|
|
2
|
-
|
|
3
|
-
export function colorHash(str: string) {
|
|
4
|
-
// Calculate hash (djb2 xor) as 32‑bit unsigned
|
|
5
|
-
let hash = 5381
|
|
6
|
-
for (let i = 0; i < str.length; i++) {
|
|
7
|
-
hash = (hash * 33) ^ str.charCodeAt(i)
|
|
8
|
-
}
|
|
9
|
-
hash >>>= 0
|
|
10
|
-
|
|
11
|
-
// Calculate hue. Golden‑ratio scramble gives maximum spread for 'near' strings.
|
|
12
|
-
const hue = Math.floor(((hash * 0.618033988749895) % 1) * 360)
|
|
13
|
-
|
|
14
|
-
// Start with vivid mid‑tone
|
|
15
|
-
const sat = 70
|
|
16
|
-
let light = 50
|
|
17
|
-
|
|
18
|
-
// Adjust lightness until it reads on both white and black backgrounds.
|
|
19
|
-
for (let i = 0; i < 20; i++) {
|
|
20
|
-
const rgb = hsl2rgb(hue, sat, light)
|
|
21
|
-
const l = lum(rgb)
|
|
22
|
-
const cW = contrast(1, l) // Versus white
|
|
23
|
-
const cB = contrast(0, l) // Versus black
|
|
24
|
-
if (cW >= 4.5 && cB >= 4.5) break // Good on both
|
|
25
|
-
light += cW < cB ? -5 : 5 // Move toward darker or lighter
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
// Convert to hex
|
|
29
|
-
const hex = hsl2rgb(hue, sat, light)
|
|
30
|
-
.map(v => v.toString(16).padStart(2, '0'))
|
|
31
|
-
.join('')
|
|
32
|
-
|
|
33
|
-
return `#${hex}`
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
function hsl2rgb(hh: number, ss: number, ll: number): Rgb {
|
|
37
|
-
ss /= 100
|
|
38
|
-
ll /= 100
|
|
39
|
-
const k = (n: number) => (n + hh / 30) % 12
|
|
40
|
-
const a = ss * Math.min(ll, 1 - ll)
|
|
41
|
-
const f = (n: number) => ll - a * Math.max(-1, Math.min(k(n) - 3, Math.min(9 - k(n), 1)))
|
|
42
|
-
return [f(0), f(8), f(4)].map(v => Math.round(v * 255)) as Rgb
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
function lum([r, g, b]: Rgb) {
|
|
46
|
-
const c = (x: number) => {
|
|
47
|
-
x /= 255
|
|
48
|
-
return x <= 0.03928 ? x / 12.92 : ((x + 0.055) / 1.055) ** 2.4
|
|
49
|
-
}
|
|
50
|
-
return 0.2126 * c(r) + 0.7152 * c(g) + 0.0722 * c(b)
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
function contrast(l1: number, l2: number) {
|
|
54
|
-
return (Math.max(l1, l2) + 0.05) / (Math.min(l1, l2) + 0.05)
|
|
55
|
-
}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import type { Fn } from '../types/types.js'
|
|
2
|
-
import { colorHash } from './utils-color-hash.js'
|
|
3
|
-
|
|
4
|
-
export type Log = Fn<void> & { warn: Fn<void>; error: Fn<void> }
|
|
5
|
-
|
|
6
|
-
export function createLog(name: string) {
|
|
7
|
-
const color = colorHash(name)
|
|
8
|
-
const log = print.bind(null, 'log', name, color) as Log
|
|
9
|
-
log.warn = print.bind(null, 'warn', name, color) as Log['warn']
|
|
10
|
-
log.error = print.bind(null, 'error', name, color) as Log['error']
|
|
11
|
-
return log
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
function print(type: 'log' | 'warn' | 'error', name: string, color: string, ...args: unknown[]) {
|
|
15
|
-
console[type](`%c[${name}]`, `color: ${color}`, ...args)
|
|
16
|
-
}
|
package/src/utils/utils-is.ts
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import type { Fn, Obj, Arr } from '../types/types.js'
|
|
2
|
-
|
|
3
|
-
export const is = {
|
|
4
|
-
null: (v: unknown) => v === null,
|
|
5
|
-
undefined: (v: unknown) => v === undefined,
|
|
6
|
-
boolean: (v: unknown) => typeof v === 'boolean',
|
|
7
|
-
number: (v: unknown) => typeof v === 'number',
|
|
8
|
-
string: (v: unknown) => typeof v === 'string',
|
|
9
|
-
symbol: (v: unknown) => typeof v === 'symbol',
|
|
10
|
-
function: (v: unknown): v is Fn => typeof v === 'function',
|
|
11
|
-
object: (v: unknown): v is Obj => Object.prototype.toString.call(v) === '[object Object]',
|
|
12
|
-
array: (v: unknown): v is Arr => Array.isArray(v),
|
|
13
|
-
set: (v: unknown) => v instanceof Set,
|
|
14
|
-
map: (v: unknown) => v instanceof Map,
|
|
15
|
-
blob: (v: unknown) => v instanceof Blob,
|
|
16
|
-
date: (v: unknown) => v instanceof Date,
|
|
17
|
-
error: (v: unknown) => v instanceof Error,
|
|
18
|
-
regex: (v: unknown) => v instanceof RegExp,
|
|
19
|
-
promise: (v: unknown) => v instanceof Promise,
|
|
20
|
-
uint8Array: (v: unknown) => v instanceof Uint8Array,
|
|
21
|
-
uint16Array: (v: unknown) => v instanceof Uint16Array,
|
|
22
|
-
uint32Array: (v: unknown) => v instanceof Uint32Array,
|
|
23
|
-
|
|
24
|
-
nan: (v: unknown) => Number.isNaN(v),
|
|
25
|
-
numeric: (v: unknown) => !is.nan(Number(v)),
|
|
26
|
-
integer: (v: unknown): v is number => Number.isInteger(v),
|
|
27
|
-
absent: (v: unknown) => v === null || v === undefined,
|
|
28
|
-
present: <T>(v: T): v is NonNullable<T> => !is.absent(v),
|
|
29
|
-
primitive: (v: unknown) => v !== Object(v),
|
|
30
|
-
collection: (v: unknown) => is.array(v) || is.object(v),
|
|
31
|
-
compound: (v: unknown) => v !== null && typeof v === 'object',
|
|
32
|
-
}
|
package/src/utils/utils-queue.ts
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import { safe } from './utils-safe.js'
|
|
2
|
-
import type { AsyncFn } from '../types/types.js'
|
|
3
|
-
|
|
4
|
-
type TaskFn<T> = () => Promise<T>
|
|
5
|
-
type Task<T = any> = { fn: TaskFn<T>; result$: PromiseWithResolvers<T> }
|
|
6
|
-
|
|
7
|
-
export class Queue {
|
|
8
|
-
name: string | null
|
|
9
|
-
tasks: Task[] = []
|
|
10
|
-
private running = false
|
|
11
|
-
|
|
12
|
-
constructor(name: string | null = null) {
|
|
13
|
-
this.name = name
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
async add<T>(fn: TaskFn<T>): Promise<T> {
|
|
17
|
-
const task: Task<T> = { fn, result$: Promise.withResolvers<T>() }
|
|
18
|
-
this.tasks.push(task)
|
|
19
|
-
this.start()
|
|
20
|
-
return await task.result$.promise
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
wrap<T extends AsyncFn>(fn: T, thisValue: unknown = null) {
|
|
24
|
-
return (async (...args: Parameters<T>) => {
|
|
25
|
-
return await this.add(() => fn.call(thisValue, ...args))
|
|
26
|
-
}) as T
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
async wait() {
|
|
30
|
-
const lastTask = this.tasks.at(-1)
|
|
31
|
-
if (lastTask) await safe(lastTask.result$.promise)
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
isEmpty() {
|
|
35
|
-
return this.tasks.length === 0
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
private async start() {
|
|
39
|
-
if (this.running) return
|
|
40
|
-
if (this.isEmpty()) return
|
|
41
|
-
|
|
42
|
-
this.running = true
|
|
43
|
-
|
|
44
|
-
while (this.tasks.length > 0) {
|
|
45
|
-
const task = this.tasks[0]
|
|
46
|
-
if (!task) break
|
|
47
|
-
const [result, error] = await safe(task.fn)
|
|
48
|
-
this.tasks.shift()
|
|
49
|
-
if (error) {
|
|
50
|
-
task.result$.reject(error)
|
|
51
|
-
} else {
|
|
52
|
-
task.result$.resolve(result)
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
this.running = false
|
|
57
|
-
}
|
|
58
|
-
}
|
package/src/utils/utils-safe.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { is } from './utils-is.js'
|
|
2
|
-
|
|
3
|
-
export async function safe<T>(
|
|
4
|
-
effect: (() => T | Promise<T>) | Promise<T>,
|
|
5
|
-
): Promise<[T, null] | [null, Error]> {
|
|
6
|
-
try {
|
|
7
|
-
const result = is.function(effect) ? await effect() : await effect
|
|
8
|
-
return [result, null]
|
|
9
|
-
} catch (e) {
|
|
10
|
-
const error = e instanceof Error ? e : new Error(String(e))
|
|
11
|
-
return [null, error]
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export function safeSync<T>(effect: () => T): [T, null] | [null, Error] {
|
|
16
|
-
try {
|
|
17
|
-
const result = effect()
|
|
18
|
-
return [result, null]
|
|
19
|
-
} catch (e) {
|
|
20
|
-
const error = e instanceof Error ? e : new Error(String(e))
|
|
21
|
-
return [null, error]
|
|
22
|
-
}
|
|
23
|
-
}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
export function unique<T = unknown>(items: T[], keyFn?: (item: T) => unknown) {
|
|
2
|
-
if (!keyFn) return [...new Set(items)]
|
|
3
|
-
|
|
4
|
-
const seen = new Set()
|
|
5
|
-
return items.filter(item => {
|
|
6
|
-
const key = keyFn(item)
|
|
7
|
-
if (seen.has(key)) return false
|
|
8
|
-
seen.add(key)
|
|
9
|
-
return true
|
|
10
|
-
})
|
|
11
|
-
}
|
package/src/utils/utils-unit.ts
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import type { Cls } from '../types/types.js'
|
|
2
|
-
import { createLog } from './utils-create-log.js'
|
|
3
|
-
|
|
4
|
-
export class Unit<TRoot = unknown> {
|
|
5
|
-
$: TRoot
|
|
6
|
-
log = createLog(this.constructor.name)
|
|
7
|
-
#parent: Unit<TRoot> | null = null
|
|
8
|
-
|
|
9
|
-
constructor(parent?: Unit<TRoot> | null) {
|
|
10
|
-
this.$ = (parent?.$ ?? this) as TRoot
|
|
11
|
-
this.#parent = parent ?? null
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
closest<T extends Unit<TRoot>>(Ancestor: Cls<T>): T | null {
|
|
15
|
-
let cursor = this.#parent
|
|
16
|
-
while (cursor) {
|
|
17
|
-
if (cursor instanceof Ancestor) return cursor as T
|
|
18
|
-
cursor = cursor.#parent
|
|
19
|
-
}
|
|
20
|
-
return null
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
never(message?: string) {
|
|
24
|
-
const details = message ? `: ${message}` : ''
|
|
25
|
-
const error = new Error(`[${this.constructor.name}] This should never happen${details}`)
|
|
26
|
-
Error.captureStackTrace(error, this.never)
|
|
27
|
-
return error
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
Error(message?: string) {
|
|
31
|
-
const error = new Error(`[${this.constructor.name}] ${message}`)
|
|
32
|
-
Error.captureStackTrace(error, this.Error)
|
|
33
|
-
return error
|
|
34
|
-
}
|
|
35
|
-
}
|
package/src/utils/utils-wait.ts
DELETED
package/src/utils/utils.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { colorHash } from './utils-color-hash.js'
|
|
2
|
-
import { createLog, type Log } from './utils-create-log.js'
|
|
3
|
-
import { ensureArray } from './utils-ensure-array.js'
|
|
4
|
-
import { is } from './utils-is.js'
|
|
5
|
-
import { Queue } from './utils-queue.js'
|
|
6
|
-
import { safe, safeSync } from './utils-safe.js'
|
|
7
|
-
import { unique } from './utils-unique.js'
|
|
8
|
-
import { Unit } from './utils-unit.js'
|
|
9
|
-
import { wait } from './utils-wait.js'
|
|
10
|
-
|
|
11
|
-
export type { Log }
|
|
12
|
-
|
|
13
|
-
export { colorHash, createLog, ensureArray, is, Queue, safe, safeSync, unique, Unit, wait }
|