@oscarpalmer/atoms 0.193.1 → 0.194.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/array/difference.mjs +2 -2
- package/dist/array/intersection.mjs +2 -2
- package/dist/array/match.mjs +13 -13
- package/dist/array/slice.mjs +5 -5
- package/dist/array/sort.d.mts +5 -1
- package/dist/array/sort.mjs +4 -2
- package/dist/array/union.mjs +2 -2
- package/dist/beacon.d.mts +8 -1
- package/dist/beacon.mjs +52 -60
- package/dist/color/constants.d.mts +1 -1
- package/dist/color/constants.mjs +3 -5
- package/dist/color/instance.mjs +34 -41
- package/dist/color/misc/format.d.mts +3 -2
- package/dist/color/misc/format.mjs +4 -1
- package/dist/color/misc/is.d.mts +9 -1
- package/dist/color/misc/is.mjs +11 -1
- package/dist/color/misc/state.d.mts +6 -6
- package/dist/color/misc/state.mjs +11 -14
- package/dist/color/models.d.mts +23 -68
- package/dist/function/assert.mjs +2 -2
- package/dist/function/limit.d.mts +0 -6
- package/dist/function/limit.mjs +2 -2
- package/dist/function/memoize.mjs +19 -32
- package/dist/function/once.d.mts +0 -3
- package/dist/function/once.mjs +16 -13
- package/dist/function/retry.d.mts +1 -24
- package/dist/function/retry.mjs +1 -1
- package/dist/function/work.mjs +13 -11
- package/dist/herald.d.mts +30 -31
- package/dist/herald.mjs +74 -56
- package/dist/index.d.mts +100 -185
- package/dist/index.mjs +680 -702
- package/dist/internal/abort.d.mts +10 -0
- package/dist/internal/abort.mjs +14 -0
- package/dist/internal/array/chunk.mjs +2 -2
- package/dist/internal/array/find.mjs +2 -2
- package/dist/internal/array/sets.d.mts +4 -4
- package/dist/internal/array/sets.mjs +7 -7
- package/dist/internal/function/timer.mjs +19 -15
- package/dist/internal/number.mjs +7 -7
- package/dist/internal/result.mjs +3 -3
- package/dist/internal/sized.mjs +4 -4
- package/dist/internal/string.mjs +4 -4
- package/dist/internal/subscription.d.mts +24 -8
- package/dist/internal/subscription.mjs +49 -36
- package/dist/internal/value/equal.mjs +6 -6
- package/dist/internal/value/misc.mjs +14 -14
- package/dist/internal/value/set.mjs +2 -2
- package/dist/logger.mjs +15 -39
- package/dist/promise/constants.d.mts +17 -0
- package/dist/promise/constants.mjs +14 -0
- package/dist/promise/delay.mjs +7 -10
- package/dist/promise/helpers.mjs +1 -1
- package/dist/promise/index.mjs +14 -17
- package/dist/promise/misc.d.mts +2 -1
- package/dist/promise/misc.mjs +8 -7
- package/dist/promise/models.d.mts +4 -16
- package/dist/promise/models.mjs +4 -13
- package/dist/promise/timed.mjs +9 -10
- package/dist/query.mjs +15 -15
- package/dist/queue.mjs +103 -116
- package/dist/random.mjs +7 -7
- package/dist/result/index.d.mts +0 -3
- package/dist/result/index.mjs +1 -1
- package/dist/result/match.d.mts +0 -3
- package/dist/result/match.mjs +4 -4
- package/dist/result/misc.mjs +2 -2
- package/dist/result/work/flow.d.mts +0 -3
- package/dist/result/work/flow.mjs +5 -5
- package/dist/result/work/pipe.d.mts +0 -3
- package/dist/result/work/pipe.mjs +3 -3
- package/dist/string/case.mjs +20 -20
- package/dist/string/fuzzy.mjs +23 -18
- package/dist/string/index.mjs +7 -7
- package/dist/string/normalize.mjs +14 -14
- package/dist/string/template.mjs +2 -2
- package/dist/value/clone.mjs +10 -10
- package/dist/value/smush.mjs +2 -2
- package/package.json +13 -4
package/dist/queue.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { createAborter } from "./internal/abort.mjs";
|
|
1
2
|
import { getNumberOrDefault } from "./internal/number.mjs";
|
|
2
3
|
//#region src/queue.ts
|
|
3
4
|
/**
|
|
@@ -6,11 +7,47 @@ import { getNumberOrDefault } from "./internal/number.mjs";
|
|
|
6
7
|
var QueueError = class extends Error {
|
|
7
8
|
constructor(message) {
|
|
8
9
|
super(message);
|
|
9
|
-
this.name =
|
|
10
|
+
this.name = QUEUE_ERROR_NAME;
|
|
10
11
|
}
|
|
11
12
|
};
|
|
13
|
+
function addToQueue(instance, state, parameters, signal) {
|
|
14
|
+
if (instance.full) throw new QueueError(QUEUE_MESSAGE_MAXIMUM);
|
|
15
|
+
const abortSignal = signal instanceof AbortSignal ? signal : void 0;
|
|
16
|
+
if (abortSignal?.aborted ?? false) throw new Error(abortSignal?.reason);
|
|
17
|
+
const id = identify(state);
|
|
18
|
+
let rejector;
|
|
19
|
+
let resolver;
|
|
20
|
+
const promise = new Promise((resolve, reject) => {
|
|
21
|
+
rejector = reject;
|
|
22
|
+
resolver = resolve;
|
|
23
|
+
});
|
|
24
|
+
const aborter = createAborter(abortSignal, () => rejector(abortSignal?.reason));
|
|
25
|
+
state.items.push({
|
|
26
|
+
aborter,
|
|
27
|
+
id,
|
|
28
|
+
parameters,
|
|
29
|
+
promise,
|
|
30
|
+
key: state.key,
|
|
31
|
+
reject: rejector,
|
|
32
|
+
resolve: resolver
|
|
33
|
+
});
|
|
34
|
+
if (state.options.autostart) run(state);
|
|
35
|
+
return {
|
|
36
|
+
id,
|
|
37
|
+
promise
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
function clearQueue(state) {
|
|
41
|
+
const items = state.items.splice(0);
|
|
42
|
+
const { length } = items;
|
|
43
|
+
for (let index = 0; index < length; index += 1) {
|
|
44
|
+
const { aborter, reject } = items[index];
|
|
45
|
+
aborter?.cancel();
|
|
46
|
+
reject(new QueueError(QUEUE_MESSAGE_CLEAR));
|
|
47
|
+
}
|
|
48
|
+
}
|
|
12
49
|
function createQueue(callback, options, key) {
|
|
13
|
-
if (typeof callback !== "function") throw new TypeError(
|
|
50
|
+
if (typeof callback !== "function") throw new TypeError(QUEUE_MESSAGE_CALLBACK);
|
|
14
51
|
const state = {
|
|
15
52
|
callback,
|
|
16
53
|
key,
|
|
@@ -22,71 +59,16 @@ function createQueue(callback, options, key) {
|
|
|
22
59
|
runners: 0
|
|
23
60
|
};
|
|
24
61
|
const instance = {
|
|
25
|
-
add: (parameters, signal) =>
|
|
26
|
-
|
|
27
|
-
const abortSignal = signal instanceof AbortSignal ? signal : void 0;
|
|
28
|
-
if (abortSignal?.aborted ?? false) throw new Error(abortSignal.reason);
|
|
29
|
-
const id = identify(state);
|
|
30
|
-
let rejector;
|
|
31
|
-
let resolver;
|
|
32
|
-
const promise = new Promise((resolve, reject) => {
|
|
33
|
-
rejector = reject;
|
|
34
|
-
resolver = resolve;
|
|
35
|
-
});
|
|
36
|
-
const aborter = abortSignal == null ? void 0 : () => rejector(abortSignal.reason);
|
|
37
|
-
signal?.addEventListener(EVENT_NAME, aborter, EVENT_OPTIONS);
|
|
38
|
-
state.items.push({
|
|
39
|
-
id,
|
|
40
|
-
parameters,
|
|
41
|
-
promise,
|
|
42
|
-
abort: aborter,
|
|
43
|
-
key: state.key,
|
|
44
|
-
reject: rejector,
|
|
45
|
-
resolve: resolver,
|
|
46
|
-
signal: abortSignal
|
|
47
|
-
});
|
|
48
|
-
if (state.options.autostart) run(state);
|
|
49
|
-
return {
|
|
50
|
-
id,
|
|
51
|
-
promise
|
|
52
|
-
};
|
|
53
|
-
},
|
|
54
|
-
clear: () => {
|
|
55
|
-
const items = state.items.splice(0);
|
|
56
|
-
const { length } = items;
|
|
57
|
-
for (let index = 0; index < length; index += 1) {
|
|
58
|
-
const { abort, reject, signal } = items[index];
|
|
59
|
-
reject(new QueueError(MESSAGE_CLEAR));
|
|
60
|
-
signal?.removeEventListener(EVENT_NAME, abort);
|
|
61
|
-
}
|
|
62
|
-
},
|
|
62
|
+
add: (parameters, signal) => addToQueue(instance, state, parameters, signal),
|
|
63
|
+
clear: () => clearQueue(state),
|
|
63
64
|
pause: () => {
|
|
64
65
|
state.paused = true;
|
|
65
66
|
},
|
|
66
|
-
remove: (id) =>
|
|
67
|
-
|
|
68
|
-
if (index > -1) {
|
|
69
|
-
const { abort, reject, signal } = state.items.splice(index, 1)[0];
|
|
70
|
-
reject(new QueueError(MESSAGE_REMOVE));
|
|
71
|
-
signal?.removeEventListener(EVENT_NAME, abort);
|
|
72
|
-
}
|
|
73
|
-
},
|
|
74
|
-
resume: () => {
|
|
75
|
-
if (state.paused) {
|
|
76
|
-
const handled = state.handled.splice(0);
|
|
77
|
-
const { length } = handled;
|
|
78
|
-
for (let index = 0; index < length; index += 1) handled[index]();
|
|
79
|
-
}
|
|
80
|
-
state.paused = false;
|
|
81
|
-
const length = Math.min(state.options.concurrency, state.items.length);
|
|
82
|
-
for (let index = 0; index < length; index += 1) run(state);
|
|
83
|
-
}
|
|
67
|
+
remove: (id) => removeQueued(state, id),
|
|
68
|
+
resume: () => resumeQueue(state)
|
|
84
69
|
};
|
|
85
70
|
Object.defineProperties(instance, {
|
|
86
|
-
[
|
|
87
|
-
enumerable: false,
|
|
88
|
-
value: NAME_QUEUE
|
|
89
|
-
},
|
|
71
|
+
[QUEUE_PROPERTY]: { value: QUEUE_NAME_QUEUE },
|
|
90
72
|
active: {
|
|
91
73
|
enumerable: true,
|
|
92
74
|
get: () => state.runners > 0
|
|
@@ -125,12 +107,6 @@ function createQueue(callback, options, key) {
|
|
|
125
107
|
function getBooleanOrDefault(value, defaultValue) {
|
|
126
108
|
return typeof value === "boolean" ? value : defaultValue;
|
|
127
109
|
}
|
|
128
|
-
function getItems(state) {
|
|
129
|
-
const size = {};
|
|
130
|
-
const queues = state.queues.entries();
|
|
131
|
-
for (const [key, queue] of queues) size[key] = queue.size;
|
|
132
|
-
return size;
|
|
133
|
-
}
|
|
134
110
|
function getOptions(input) {
|
|
135
111
|
const options = typeof input === "object" && input != null ? input : {};
|
|
136
112
|
return {
|
|
@@ -140,7 +116,7 @@ function getOptions(input) {
|
|
|
140
116
|
};
|
|
141
117
|
}
|
|
142
118
|
function getQueue(state, key, add) {
|
|
143
|
-
if (typeof key !== "string" || key.trim().length === 0) throw new TypeError(
|
|
119
|
+
if (typeof key !== "string" || key.trim().length === 0) throw new TypeError(QUEUE_MESSAGE_KEY);
|
|
144
120
|
let queue = state.queues.get(key);
|
|
145
121
|
if (queue == null && add === true) {
|
|
146
122
|
queue = createQueue(state.callback, state.options, key);
|
|
@@ -148,6 +124,12 @@ function getQueue(state, key, add) {
|
|
|
148
124
|
}
|
|
149
125
|
return queue;
|
|
150
126
|
}
|
|
127
|
+
function getQueueItems(state) {
|
|
128
|
+
const size = {};
|
|
129
|
+
const queues = state.queues.entries();
|
|
130
|
+
for (const [key, queue] of queues) size[key] = queue.size;
|
|
131
|
+
return size;
|
|
132
|
+
}
|
|
151
133
|
function getStatus(state, status) {
|
|
152
134
|
const queues = state.queues.entries();
|
|
153
135
|
const result = [];
|
|
@@ -155,8 +137,8 @@ function getStatus(state, status) {
|
|
|
155
137
|
return result;
|
|
156
138
|
}
|
|
157
139
|
function handleQueuedResult(item, error, result) {
|
|
158
|
-
item.
|
|
159
|
-
if (item.signal?.aborted ?? false) {
|
|
140
|
+
item.aborter?.cancel();
|
|
141
|
+
if (item.aborter?.signal?.aborted ?? false) {
|
|
160
142
|
item.reject();
|
|
161
143
|
return;
|
|
162
144
|
}
|
|
@@ -185,7 +167,7 @@ function identify(state) {
|
|
|
185
167
|
* @returns `true` if the value is a keyed queue, otherwise `false`
|
|
186
168
|
*/
|
|
187
169
|
function isKeyedQueue(value) {
|
|
188
|
-
return isQueueInstance(
|
|
170
|
+
return isQueueInstance(QUEUE_NAME_KEYED, value);
|
|
189
171
|
}
|
|
190
172
|
/**
|
|
191
173
|
* Is the value a queue?
|
|
@@ -194,13 +176,13 @@ function isKeyedQueue(value) {
|
|
|
194
176
|
* @returns `true` if the value is a queue, otherwise `false`
|
|
195
177
|
*/
|
|
196
178
|
function isQueue(value) {
|
|
197
|
-
return isQueueInstance(
|
|
179
|
+
return isQueueInstance(QUEUE_NAME_QUEUE, value);
|
|
198
180
|
}
|
|
199
181
|
function isQueueInstance(name, value) {
|
|
200
|
-
return typeof value === "object" && value != null && value[
|
|
182
|
+
return typeof value === "object" && value != null && value[QUEUE_PROPERTY] === name;
|
|
201
183
|
}
|
|
202
184
|
function keyedQueue(callback, options) {
|
|
203
|
-
if (typeof callback !== "function") throw new TypeError(
|
|
185
|
+
if (typeof callback !== "function") throw new TypeError(QUEUE_MESSAGE_CALLBACK);
|
|
204
186
|
const state = {
|
|
205
187
|
callback,
|
|
206
188
|
options: getOptions(options),
|
|
@@ -208,26 +190,17 @@ function keyedQueue(callback, options) {
|
|
|
208
190
|
};
|
|
209
191
|
const instance = {
|
|
210
192
|
add: (key, parameters, signal) => getQueue(state, key, true).add(parameters, signal),
|
|
211
|
-
clear: (key) =>
|
|
212
|
-
handleQueues(state, HANDLE_CLEAR, key);
|
|
213
|
-
},
|
|
193
|
+
clear: (key) => handleQueues(state, QUEUE_HANDLE_CLEAR, key),
|
|
214
194
|
get: (key) => getQueue(state, key),
|
|
215
|
-
pause: (key) =>
|
|
216
|
-
handleQueues(state, HANDLE_PAUSE, key);
|
|
217
|
-
},
|
|
195
|
+
pause: (key) => handleQueues(state, QUEUE_HANDLE_PAUSE, key),
|
|
218
196
|
remove: (key, id) => removeQueue(state, key, id),
|
|
219
|
-
resume: (key) =>
|
|
220
|
-
handleQueues(state, HANDLE_RESUME, key);
|
|
221
|
-
}
|
|
197
|
+
resume: (key) => handleQueues(state, QUEUE_HANDLE_RESUME, key)
|
|
222
198
|
};
|
|
223
199
|
Object.defineProperties(instance, {
|
|
224
|
-
[
|
|
225
|
-
enumerable: false,
|
|
226
|
-
value: NAME_KEYED
|
|
227
|
-
},
|
|
200
|
+
[QUEUE_PROPERTY]: { value: QUEUE_NAME_KEYED },
|
|
228
201
|
active: {
|
|
229
202
|
enumerable: true,
|
|
230
|
-
get: () => getStatus(state,
|
|
203
|
+
get: () => getStatus(state, QUEUE_STATUS_ACTIVE)
|
|
231
204
|
},
|
|
232
205
|
autostart: {
|
|
233
206
|
enumerable: true,
|
|
@@ -239,15 +212,15 @@ function keyedQueue(callback, options) {
|
|
|
239
212
|
},
|
|
240
213
|
empty: {
|
|
241
214
|
enumerable: true,
|
|
242
|
-
get: () => getStatus(state,
|
|
215
|
+
get: () => getStatus(state, QUEUE_STATUS_EMPTY)
|
|
243
216
|
},
|
|
244
217
|
full: {
|
|
245
218
|
enumerable: true,
|
|
246
|
-
get: () => getStatus(state,
|
|
219
|
+
get: () => getStatus(state, QUEUE_STATUS_FULL)
|
|
247
220
|
},
|
|
248
221
|
items: {
|
|
249
222
|
enumerable: true,
|
|
250
|
-
get: () =>
|
|
223
|
+
get: () => getQueueItems(state)
|
|
251
224
|
},
|
|
252
225
|
keys: {
|
|
253
226
|
enumerable: true,
|
|
@@ -259,7 +232,7 @@ function keyedQueue(callback, options) {
|
|
|
259
232
|
},
|
|
260
233
|
paused: {
|
|
261
234
|
enumerable: true,
|
|
262
|
-
get: () => getStatus(state,
|
|
235
|
+
get: () => getStatus(state, QUEUE_STATUS_PAUSED)
|
|
263
236
|
},
|
|
264
237
|
queues: {
|
|
265
238
|
enumerable: true,
|
|
@@ -274,7 +247,7 @@ function queue(callback, options) {
|
|
|
274
247
|
queue.keyed = keyedQueue;
|
|
275
248
|
function removeQueue(state, key, id) {
|
|
276
249
|
if (key == null) {
|
|
277
|
-
handleQueues(state,
|
|
250
|
+
handleQueues(state, QUEUE_HANDLE_CLEAR);
|
|
278
251
|
state.queues.clear();
|
|
279
252
|
return;
|
|
280
253
|
}
|
|
@@ -287,6 +260,24 @@ function removeQueue(state, key, id) {
|
|
|
287
260
|
queue.clear();
|
|
288
261
|
state.queues.delete(key);
|
|
289
262
|
}
|
|
263
|
+
function removeQueued(state, id) {
|
|
264
|
+
const index = state.items.findIndex((item) => item.id === id);
|
|
265
|
+
if (index > -1) {
|
|
266
|
+
const { aborter, reject } = state.items.splice(index, 1)[0];
|
|
267
|
+
aborter?.cancel();
|
|
268
|
+
reject(new QueueError(QUEUE_MESSAGE_REMOVE));
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
function resumeQueue(state) {
|
|
272
|
+
if (state.paused) {
|
|
273
|
+
const handled = state.handled.splice(0);
|
|
274
|
+
const { length } = handled;
|
|
275
|
+
for (let index = 0; index < length; index += 1) handled[index]();
|
|
276
|
+
}
|
|
277
|
+
state.paused = false;
|
|
278
|
+
const length = Math.min(state.options.concurrency, state.items.length);
|
|
279
|
+
for (let index = 0; index < length; index += 1) run(state);
|
|
280
|
+
}
|
|
290
281
|
async function run(state) {
|
|
291
282
|
if (state.paused || state.runners >= state.options.concurrency) return;
|
|
292
283
|
state.runners += 1;
|
|
@@ -295,7 +286,7 @@ async function run(state) {
|
|
|
295
286
|
let error = false;
|
|
296
287
|
let result;
|
|
297
288
|
try {
|
|
298
|
-
if (!(item.signal?.aborted ?? false)) {
|
|
289
|
+
if (!(item.aborter?.signal?.aborted ?? false)) {
|
|
299
290
|
const parameters = item.key == null ? item.parameters : [item.key, ...item.parameters];
|
|
300
291
|
result = await state.callback(...parameters);
|
|
301
292
|
}
|
|
@@ -305,9 +296,7 @@ async function run(state) {
|
|
|
305
296
|
}
|
|
306
297
|
if (state.paused) {
|
|
307
298
|
const paused = item;
|
|
308
|
-
state.handled.push(() =>
|
|
309
|
-
handleQueuedResult(paused, error, result);
|
|
310
|
-
});
|
|
299
|
+
state.handled.push(() => handleQueuedResult(paused, error, result));
|
|
311
300
|
break;
|
|
312
301
|
}
|
|
313
302
|
handleQueuedResult(item, error, result);
|
|
@@ -315,23 +304,21 @@ async function run(state) {
|
|
|
315
304
|
}
|
|
316
305
|
state.runners -= 1;
|
|
317
306
|
}
|
|
318
|
-
const
|
|
319
|
-
const
|
|
320
|
-
const
|
|
321
|
-
const
|
|
322
|
-
const
|
|
323
|
-
const
|
|
324
|
-
const
|
|
325
|
-
const
|
|
326
|
-
const
|
|
327
|
-
const
|
|
328
|
-
const
|
|
329
|
-
const
|
|
330
|
-
const
|
|
331
|
-
const
|
|
332
|
-
const
|
|
333
|
-
const
|
|
334
|
-
const STATUS_FULL = "full";
|
|
335
|
-
const STATUS_PAUSED = "paused";
|
|
307
|
+
const QUEUE_ERROR_NAME = "QueueError";
|
|
308
|
+
const QUEUE_PROPERTY = "$queue";
|
|
309
|
+
const QUEUE_HANDLE_CLEAR = "clear";
|
|
310
|
+
const QUEUE_HANDLE_PAUSE = "pause";
|
|
311
|
+
const QUEUE_HANDLE_RESUME = "resume";
|
|
312
|
+
const QUEUE_MESSAGE_CALLBACK = "A Queue requires a callback function";
|
|
313
|
+
const QUEUE_MESSAGE_CLEAR = "Queue was cleared";
|
|
314
|
+
const QUEUE_MESSAGE_KEY = "Key must be a non-empty string";
|
|
315
|
+
const QUEUE_MESSAGE_MAXIMUM = "Queue has reached its maximum size";
|
|
316
|
+
const QUEUE_MESSAGE_REMOVE = "Item removed from queue";
|
|
317
|
+
const QUEUE_NAME_KEYED = "keyedQueue";
|
|
318
|
+
const QUEUE_NAME_QUEUE = "queue";
|
|
319
|
+
const QUEUE_STATUS_ACTIVE = "active";
|
|
320
|
+
const QUEUE_STATUS_EMPTY = "empty";
|
|
321
|
+
const QUEUE_STATUS_FULL = "full";
|
|
322
|
+
const QUEUE_STATUS_PAUSED = "paused";
|
|
336
323
|
//#endregion
|
|
337
324
|
export { QueueError, isKeyedQueue, isQueue, isQueueInstance, keyedQueue, queue };
|
package/dist/random.mjs
CHANGED
|
@@ -8,7 +8,7 @@ import { join } from "./internal/string.mjs";
|
|
|
8
8
|
* @returns Random boolean
|
|
9
9
|
*/
|
|
10
10
|
function getRandomBoolean() {
|
|
11
|
-
return Math.random() >
|
|
11
|
+
return Math.random() > RANDOM_BOOLEAN_MODIFIER;
|
|
12
12
|
}
|
|
13
13
|
/**
|
|
14
14
|
* Get a random string of characters with a specified length
|
|
@@ -19,7 +19,7 @@ function getRandomBoolean() {
|
|
|
19
19
|
*/
|
|
20
20
|
function getRandomCharacters(length, selection) {
|
|
21
21
|
if (typeof length !== "number" || length <= 0) return "";
|
|
22
|
-
const actual = typeof selection === "string" && selection.length > 0 ? selection :
|
|
22
|
+
const actual = typeof selection === "string" && selection.length > 0 ? selection : RANDOM_ALPHABET;
|
|
23
23
|
let characters = "";
|
|
24
24
|
for (let index = 0; index < length; index += 1) characters += actual.charAt(getRandomInteger(0, actual.length - 1));
|
|
25
25
|
return characters;
|
|
@@ -39,7 +39,7 @@ function getRandomColor(prefix) {
|
|
|
39
39
|
* @returns Random hexadecimal character from `0-9` and `A-F`
|
|
40
40
|
*/
|
|
41
41
|
function getRandomHex() {
|
|
42
|
-
return
|
|
42
|
+
return RANDOM_HEX_CHARACTERS[getRandomInteger(0, RANDOM_HEX_MAXIMUM)];
|
|
43
43
|
}
|
|
44
44
|
/**
|
|
45
45
|
* Get a random item from an array
|
|
@@ -57,9 +57,9 @@ function getRandomItems(array, amount) {
|
|
|
57
57
|
if (amount === 1) return [array[getRandomInteger(0, array.length - 1)]];
|
|
58
58
|
return typeof amount !== "number" || amount <= 0 || amount >= array.length ? shuffle(array) : shuffle(array).slice(0, amount);
|
|
59
59
|
}
|
|
60
|
-
const
|
|
61
|
-
const
|
|
62
|
-
const
|
|
63
|
-
const
|
|
60
|
+
const RANDOM_ALPHABET = "abcdefghijklmnopqrstuvwxyz";
|
|
61
|
+
const RANDOM_BOOLEAN_MODIFIER = .5;
|
|
62
|
+
const RANDOM_HEX_CHARACTERS = "0123456789ABCDEF";
|
|
63
|
+
const RANDOM_HEX_MAXIMUM = 15;
|
|
64
64
|
//#endregion
|
|
65
65
|
export { getRandomBoolean, getRandomCharacters, getRandomColor, getRandomFloatingNumber as getRandomFloat, getRandomHex, getRandomInteger, getRandomItem, getRandomItems };
|
package/dist/result/index.d.mts
CHANGED
|
@@ -53,8 +53,5 @@ declare function attempt<Value, E>(callback: () => Value, error: E): ExtendedRes
|
|
|
53
53
|
* @returns Callback result
|
|
54
54
|
*/
|
|
55
55
|
declare function attempt<Value>(callback: () => Value): Result<Value, Error>;
|
|
56
|
-
declare namespace attempt {
|
|
57
|
-
var async: typeof asyncAttempt;
|
|
58
|
-
}
|
|
59
56
|
//#endregion
|
|
60
57
|
export { asyncAttempt, attempt };
|
package/dist/result/index.mjs
CHANGED
|
@@ -17,6 +17,6 @@ function attempt(callback, err) {
|
|
|
17
17
|
return getError(err ?? thrown, err == null ? void 0 : thrown);
|
|
18
18
|
}
|
|
19
19
|
}
|
|
20
|
-
attempt
|
|
20
|
+
Object.defineProperty(attempt, "async", { value: asyncAttempt });
|
|
21
21
|
//#endregion
|
|
22
22
|
export { asyncAttempt, attempt };
|
package/dist/result/match.d.mts
CHANGED
|
@@ -38,8 +38,5 @@ declare function matchResult<Value, Returned, E = Error>(result: AnyResult<Value
|
|
|
38
38
|
* @returns Matched value
|
|
39
39
|
*/
|
|
40
40
|
declare function matchResult<Value, Returned, E = Error>(result: AnyResult<Value, E> | (() => AnyResult<Value, E>), ok: ResultMatch<Value, Returned, E>['ok'], error: ResultMatch<Value, Returned, E>['error']): Returned;
|
|
41
|
-
declare namespace matchResult {
|
|
42
|
-
var async: typeof asyncMatchResult;
|
|
43
|
-
}
|
|
44
41
|
//#endregion
|
|
45
42
|
export { asyncMatchResult, matchResult };
|
package/dist/result/match.mjs
CHANGED
|
@@ -5,7 +5,7 @@ async function asyncMatchResult(result, first, error) {
|
|
|
5
5
|
if (typeof result === "function") value = await result();
|
|
6
6
|
else if (result instanceof Promise) value = await result;
|
|
7
7
|
else value = result;
|
|
8
|
-
if (!isResult(value)) throw new Error(
|
|
8
|
+
if (!isResult(value)) throw new Error(MATCH_MESSAGE);
|
|
9
9
|
const hasObj = typeof first === "object" && first !== null;
|
|
10
10
|
const okHandler = hasObj ? first.ok : first;
|
|
11
11
|
const errorHandler = hasObj ? first.error : error;
|
|
@@ -14,14 +14,14 @@ async function asyncMatchResult(result, first, error) {
|
|
|
14
14
|
}
|
|
15
15
|
function matchResult(result, first, error) {
|
|
16
16
|
const value = typeof result === "function" ? result() : result;
|
|
17
|
-
if (!isResult(value)) throw new Error(
|
|
17
|
+
if (!isResult(value)) throw new Error(MATCH_MESSAGE);
|
|
18
18
|
const hasObj = typeof first === "object" && first !== null;
|
|
19
19
|
const okHandler = hasObj ? first.ok : first;
|
|
20
20
|
const errorHandler = hasObj ? first.error : error;
|
|
21
21
|
if (isOk(value)) return okHandler(value.value);
|
|
22
22
|
return errorHandler(value.error, value.original);
|
|
23
23
|
}
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
const MATCH_MESSAGE = "`result.match` expected a Result or a function that returns a Result";
|
|
25
|
+
Object.defineProperty(matchResult, "async", { value: asyncMatchResult });
|
|
26
26
|
//#endregion
|
|
27
27
|
export { asyncMatchResult, matchResult };
|
package/dist/result/misc.mjs
CHANGED
|
@@ -33,12 +33,12 @@ function ok(value) {
|
|
|
33
33
|
*/
|
|
34
34
|
async function toPromise(result) {
|
|
35
35
|
const actual = typeof result === "function" ? result() : result;
|
|
36
|
-
if (!isResult(actual)) return Promise.reject(new Error(
|
|
36
|
+
if (!isResult(actual)) return Promise.reject(new Error(RESULT_MESSAGE_PROMISE));
|
|
37
37
|
return isOk(actual) ? Promise.resolve(actual.value) : Promise.reject(actual.error);
|
|
38
38
|
}
|
|
39
39
|
function unwrap(value, defaultValue) {
|
|
40
40
|
return isOk(value) ? value.value : defaultValue;
|
|
41
41
|
}
|
|
42
|
-
const
|
|
42
|
+
const RESULT_MESSAGE_PROMISE = "toPromise expected to receive a Result";
|
|
43
43
|
//#endregion
|
|
44
44
|
export { error, getError, isError, isOk, isResult, ok, toPromise, unwrap };
|
|
@@ -177,8 +177,5 @@ declare function attemptFlow<First extends GenericCallback>(first: First, ...fns
|
|
|
177
177
|
* @returns _Flow_ function
|
|
178
178
|
*/
|
|
179
179
|
declare function attemptFlow(...fns: GenericCallback[]): (...args: unknown[]) => Result<unknown>;
|
|
180
|
-
declare namespace attemptFlow {
|
|
181
|
-
var async: typeof attemptAsyncFlow;
|
|
182
|
-
}
|
|
183
180
|
//#endregion
|
|
184
181
|
export { AttemptFlow, AttemptFlowPromise, attemptAsyncFlow, attemptFlow };
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { isError, isOk } from "../../internal/result.mjs";
|
|
2
|
-
import { flow } from "../../function/work.mjs";
|
|
3
|
-
import { attempt } from "../index.mjs";
|
|
2
|
+
import { asyncFlow, flow } from "../../function/work.mjs";
|
|
3
|
+
import { asyncAttempt, attempt } from "../index.mjs";
|
|
4
4
|
//#region src/result/work/flow.ts
|
|
5
5
|
function attemptAsyncFlow(...fns) {
|
|
6
6
|
let Flow;
|
|
7
|
-
return (...args) =>
|
|
8
|
-
Flow ??=
|
|
7
|
+
return (...args) => asyncAttempt(() => {
|
|
8
|
+
Flow ??= asyncFlow(...fns);
|
|
9
9
|
return Flow(...args.map((value) => {
|
|
10
10
|
if (isError(value)) throw value.error;
|
|
11
11
|
return isOk(value) ? value.value : value;
|
|
@@ -22,6 +22,6 @@ function attemptFlow(...fns) {
|
|
|
22
22
|
}));
|
|
23
23
|
});
|
|
24
24
|
}
|
|
25
|
-
attemptFlow
|
|
25
|
+
Object.defineProperty(attemptFlow, "async", { value: attemptAsyncFlow });
|
|
26
26
|
//#endregion
|
|
27
27
|
export { attemptAsyncFlow, attemptFlow };
|
|
@@ -178,8 +178,5 @@ declare function attemptPipe<Initial, First, Second, Third, Fourth, Fifth, Sixth
|
|
|
178
178
|
* @returns _Piped_ result
|
|
179
179
|
*/
|
|
180
180
|
declare function attemptPipe<Initial>(initial: GenericCallback | Initial | Result<Initial>, first?: (value: UnwrapValue<Initial>) => unknown, ...seconds: Array<(value: unknown) => unknown>): WrapValue<Initial>;
|
|
181
|
-
declare namespace attemptPipe {
|
|
182
|
-
var async: typeof attemptAsyncPipe;
|
|
183
|
-
}
|
|
184
181
|
//#endregion
|
|
185
182
|
export { attemptAsyncPipe, attemptPipe };
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { isError, isOk } from "../../internal/result.mjs";
|
|
2
2
|
import { asyncPipe, pipe } from "../../function/work.mjs";
|
|
3
|
-
import { attempt } from "../index.mjs";
|
|
3
|
+
import { asyncAttempt, attempt } from "../index.mjs";
|
|
4
4
|
//#region src/result/work/pipe.ts
|
|
5
5
|
async function attemptAsyncPipe(initial, first, ...seconds) {
|
|
6
|
-
return
|
|
6
|
+
return asyncAttempt(() => {
|
|
7
7
|
if (isError(initial)) throw initial.error;
|
|
8
8
|
const value = typeof initial === "function" ? initial() : isOk(initial) ? initial.value : initial;
|
|
9
9
|
if (first == null) return value;
|
|
@@ -18,6 +18,6 @@ function attemptPipe(initial, first, ...seconds) {
|
|
|
18
18
|
return pipe(value, ...[first, ...seconds]);
|
|
19
19
|
});
|
|
20
20
|
}
|
|
21
|
-
attemptPipe
|
|
21
|
+
Object.defineProperty(attemptPipe, "async", { value: attemptAsyncPipe });
|
|
22
22
|
//#endregion
|
|
23
23
|
export { attemptAsyncPipe, attemptPipe };
|
package/dist/string/case.mjs
CHANGED
|
@@ -8,7 +8,7 @@ import { memoize } from "../function/memoize.mjs";
|
|
|
8
8
|
* @returns Camel-cased string
|
|
9
9
|
*/
|
|
10
10
|
function camelCase(value) {
|
|
11
|
-
return toCase(
|
|
11
|
+
return toCase(STRING_CASE_CAMEL, value, true, false);
|
|
12
12
|
}
|
|
13
13
|
/**
|
|
14
14
|
* Capitalize the first letter of a string _(and lowercase the rest)_
|
|
@@ -28,7 +28,7 @@ function capitalize(value) {
|
|
|
28
28
|
* @returns Kebab-cased string
|
|
29
29
|
*/
|
|
30
30
|
function kebabCase(value) {
|
|
31
|
-
return toCase(
|
|
31
|
+
return toCase(STRING_CASE_KEBAB, value, false, false);
|
|
32
32
|
}
|
|
33
33
|
/**
|
|
34
34
|
* Convert a string to lower case
|
|
@@ -48,7 +48,7 @@ function lowerCase(value) {
|
|
|
48
48
|
* @returns Pascal-cased string
|
|
49
49
|
*/
|
|
50
50
|
function pascalCase(value) {
|
|
51
|
-
return toCase(
|
|
51
|
+
return toCase(STRING_CASE_PASCAL, value, true, true);
|
|
52
52
|
}
|
|
53
53
|
/**
|
|
54
54
|
* Convert a string to snake case _(this_is_snake_case)_
|
|
@@ -57,7 +57,7 @@ function pascalCase(value) {
|
|
|
57
57
|
* @returns Snake-cased string
|
|
58
58
|
*/
|
|
59
59
|
function snakeCase(value) {
|
|
60
|
-
return toCase(
|
|
60
|
+
return toCase(STRING_CASE_SNAKE, value, false, false);
|
|
61
61
|
}
|
|
62
62
|
/**
|
|
63
63
|
* Convert a string to title case _(Capitalizing Every Word)_
|
|
@@ -86,7 +86,7 @@ function toCaseCallback(value) {
|
|
|
86
86
|
const partsLength = parts.length;
|
|
87
87
|
const cased = [];
|
|
88
88
|
for (let partIndex = 0; partIndex < partsLength; partIndex += 1) {
|
|
89
|
-
const items = parts[partIndex].replace(
|
|
89
|
+
const items = parts[partIndex].replace(STRING_EXPRESSION_ACRONYM, (full, one, two, three) => three === STRING_S ? full : `${one}-${two}${three}`).replace(STRING_EXPRESSION_CAMEL_CASE, STRING_REPLACEMENT_CAMEL_CASE).split("-");
|
|
90
90
|
const itemsLength = items.length;
|
|
91
91
|
const partResult = [];
|
|
92
92
|
let itemCount = 0;
|
|
@@ -112,23 +112,23 @@ function upperCase(value) {
|
|
|
112
112
|
memoizedUpperCase ??= memoize((v) => v.toLocaleUpperCase());
|
|
113
113
|
return memoizedUpperCase.run(value);
|
|
114
114
|
}
|
|
115
|
-
const
|
|
116
|
-
const
|
|
117
|
-
const
|
|
118
|
-
const
|
|
119
|
-
const
|
|
120
|
-
const
|
|
121
|
-
const
|
|
122
|
-
const
|
|
123
|
-
const
|
|
124
|
-
const
|
|
125
|
-
const
|
|
115
|
+
const STRING_CASE_CAMEL = "camel";
|
|
116
|
+
const STRING_CASE_KEBAB = "kebab";
|
|
117
|
+
const STRING_CASE_PASCAL = "pascal";
|
|
118
|
+
const STRING_CASE_SNAKE = "snake";
|
|
119
|
+
const STRING_DELIMTER_EMPTY = "";
|
|
120
|
+
const STRING_DELIMITER_HYPHEN = "-";
|
|
121
|
+
const STRING_DELIMITER_UNDERSCORE = "_";
|
|
122
|
+
const STRING_EXPRESSION_CAMEL_CASE = /(\p{Ll})(\p{Lu})/gu;
|
|
123
|
+
const STRING_EXPRESSION_ACRONYM = /(\p{Lu}*)(\p{Lu})(\p{Ll}+)/gu;
|
|
124
|
+
const STRING_REPLACEMENT_CAMEL_CASE = "$1-$2";
|
|
125
|
+
const STRING_S = "s";
|
|
126
126
|
const caseMemoizers = {};
|
|
127
127
|
const delimiters = {
|
|
128
|
-
[
|
|
129
|
-
[
|
|
130
|
-
[
|
|
131
|
-
[
|
|
128
|
+
[STRING_CASE_CAMEL]: STRING_DELIMTER_EMPTY,
|
|
129
|
+
[STRING_CASE_KEBAB]: STRING_DELIMITER_HYPHEN,
|
|
130
|
+
[STRING_CASE_PASCAL]: STRING_DELIMTER_EMPTY,
|
|
131
|
+
[STRING_CASE_SNAKE]: STRING_DELIMITER_UNDERSCORE
|
|
132
132
|
};
|
|
133
133
|
let memoizedCapitalize;
|
|
134
134
|
let memoizedLowerCase;
|