@stacksjs/utils 0.59.8 → 0.59.10
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/index.d.ts +0 -1
- package/dist/index.js +5 -78
- package/package.json +1 -2
- package/src/index.ts +0 -1
- package/dist/p.d.ts +0 -41
- package/src/p.ts +0 -119
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -351,78 +351,6 @@ useSum,
|
|
|
351
351
|
useTrunc
|
|
352
352
|
} from "@vueuse/math";
|
|
353
353
|
var clamp = (n, min, max) => Math.min(max, Math.max(min, n));
|
|
354
|
-
// src/p.ts
|
|
355
|
-
import pLimit from "p-limit";
|
|
356
|
-
function p3(items, options) {
|
|
357
|
-
return new PInstance(items, options);
|
|
358
|
-
}
|
|
359
|
-
var VOID = Symbol("p-void");
|
|
360
|
-
|
|
361
|
-
class PInstance extends Promise {
|
|
362
|
-
items;
|
|
363
|
-
options;
|
|
364
|
-
promises = new Set;
|
|
365
|
-
get promise() {
|
|
366
|
-
let batch;
|
|
367
|
-
const items = [...Array.from(this.items), ...Array.from(this.promises)];
|
|
368
|
-
if (this.options?.concurrency) {
|
|
369
|
-
const limit = pLimit(this.options.concurrency);
|
|
370
|
-
batch = Promise.all(items.map((p4) => limit(() => p4)));
|
|
371
|
-
} else {
|
|
372
|
-
batch = Promise.all(items);
|
|
373
|
-
}
|
|
374
|
-
return batch.then((l) => l.filter((i) => i !== VOID));
|
|
375
|
-
}
|
|
376
|
-
constructor(items = [], options) {
|
|
377
|
-
super(() => {
|
|
378
|
-
});
|
|
379
|
-
this.items = items;
|
|
380
|
-
this.options = options;
|
|
381
|
-
}
|
|
382
|
-
add(...args) {
|
|
383
|
-
args.forEach((i) => {
|
|
384
|
-
this.promises.add(i);
|
|
385
|
-
});
|
|
386
|
-
}
|
|
387
|
-
map(fn) {
|
|
388
|
-
return new PInstance(Array.from(this.items).map(async (i, idx) => {
|
|
389
|
-
const v = await i;
|
|
390
|
-
if (v === VOID)
|
|
391
|
-
return VOID;
|
|
392
|
-
return fn(v, idx);
|
|
393
|
-
}), this.options);
|
|
394
|
-
}
|
|
395
|
-
filter(fn) {
|
|
396
|
-
return new PInstance(Array.from(this.items).map(async (i, idx) => {
|
|
397
|
-
const v = await i;
|
|
398
|
-
const r = await fn(v, idx);
|
|
399
|
-
if (!r)
|
|
400
|
-
return VOID;
|
|
401
|
-
return v;
|
|
402
|
-
}), this.options);
|
|
403
|
-
}
|
|
404
|
-
forEach(fn) {
|
|
405
|
-
return this.map(fn).then();
|
|
406
|
-
}
|
|
407
|
-
reduce(fn, initialValue) {
|
|
408
|
-
return this.promise.then((array) => array.reduce(fn, initialValue));
|
|
409
|
-
}
|
|
410
|
-
clear() {
|
|
411
|
-
this.promises.clear();
|
|
412
|
-
}
|
|
413
|
-
then(fn) {
|
|
414
|
-
const p4 = this.promise;
|
|
415
|
-
if (fn)
|
|
416
|
-
return p4.then(fn);
|
|
417
|
-
return p4;
|
|
418
|
-
}
|
|
419
|
-
catch(fn) {
|
|
420
|
-
return this.promise.catch(fn);
|
|
421
|
-
}
|
|
422
|
-
finally(fn) {
|
|
423
|
-
return this.promise.finally(fn);
|
|
424
|
-
}
|
|
425
|
-
}
|
|
426
354
|
// src/promise.ts
|
|
427
355
|
import {remove} from "@stacksjs/arrays";
|
|
428
356
|
function createSingletonPromise(fn) {
|
|
@@ -444,12 +372,12 @@ function createPromiseLock() {
|
|
|
444
372
|
const locks = [];
|
|
445
373
|
return {
|
|
446
374
|
async run(fn) {
|
|
447
|
-
const
|
|
448
|
-
locks.push(
|
|
375
|
+
const p3 = fn();
|
|
376
|
+
locks.push(p3);
|
|
449
377
|
try {
|
|
450
|
-
return await
|
|
378
|
+
return await p3;
|
|
451
379
|
} finally {
|
|
452
|
-
remove(locks,
|
|
380
|
+
remove(locks, p3);
|
|
453
381
|
}
|
|
454
382
|
},
|
|
455
383
|
async wait() {
|
|
@@ -902,7 +830,7 @@ function prettyBytes(number, options) {
|
|
|
902
830
|
return prefix + numberString + separator + unit;
|
|
903
831
|
}
|
|
904
832
|
// package.json
|
|
905
|
-
var version = "0.59.
|
|
833
|
+
var version = "0.59.10";
|
|
906
834
|
export {
|
|
907
835
|
wordChar,
|
|
908
836
|
wordBoundary,
|
|
@@ -1139,7 +1067,6 @@ export {
|
|
|
1139
1067
|
pausableWatch,
|
|
1140
1068
|
parseYaml,
|
|
1141
1069
|
packageManager,
|
|
1142
|
-
p3 as p,
|
|
1143
1070
|
originRequestFunctionHash,
|
|
1144
1071
|
or,
|
|
1145
1072
|
oneOrMore,
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stacksjs/utils",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.59.
|
|
4
|
+
"version": "0.59.10",
|
|
5
5
|
"description": "The Stacks helper functions.",
|
|
6
6
|
"author": "Chris Breuer",
|
|
7
7
|
"license": "MIT",
|
|
@@ -98,7 +98,6 @@
|
|
|
98
98
|
"kolorist": "1.8.0",
|
|
99
99
|
"macroable": "^7.0.2",
|
|
100
100
|
"magic-regexp": "^0.7.0",
|
|
101
|
-
"p-limit": "^5.0.0",
|
|
102
101
|
"perfect-debounce": "^1.0.0",
|
|
103
102
|
"pretty-bytes": "^6.1.1",
|
|
104
103
|
"yaml": "^2.4.1"
|
package/src/index.ts
CHANGED
package/dist/p.d.ts
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
interface POptions {
|
|
2
|
-
/**
|
|
3
|
-
* How many promises are resolved at the same time.
|
|
4
|
-
*/
|
|
5
|
-
concurrency?: number | undefined;
|
|
6
|
-
}
|
|
7
|
-
declare class PInstance<T = any> extends Promise<Awaited<T>[]> {
|
|
8
|
-
items: Iterable<T>;
|
|
9
|
-
options?: POptions | undefined;
|
|
10
|
-
private promises;
|
|
11
|
-
get promise(): Promise<Awaited<T>[]>;
|
|
12
|
-
constructor(items?: Iterable<T>, options?: POptions | undefined);
|
|
13
|
-
add(...args: (T | Promise<T>)[]): void;
|
|
14
|
-
map<U>(fn: (value: Awaited<T>, index: number) => U): PInstance<Promise<U>>;
|
|
15
|
-
filter(fn: (value: Awaited<T>, index: number) => boolean | Promise<boolean>): PInstance<Promise<T>>;
|
|
16
|
-
forEach(fn: (value: Awaited<T>, index: number) => void): Promise<void>;
|
|
17
|
-
reduce<U>(fn: (previousValue: U, currentValue: Awaited<T>, currentIndex: number, array: Awaited<T>[]) => U, initialValue: U): Promise<U>;
|
|
18
|
-
clear(): void;
|
|
19
|
-
then(fn?: () => PromiseLike<any>): Promise<any>;
|
|
20
|
-
catch(fn?: (err: unknown) => PromiseLike<any>): Promise<any>;
|
|
21
|
-
finally(fn?: () => void): Promise<Awaited<T>[]>;
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* Utility for managing multiple promises.
|
|
25
|
-
*
|
|
26
|
-
* @see https://github.com/antfu/utils/tree/main/docs/p.md
|
|
27
|
-
* @category Promise
|
|
28
|
-
* @example
|
|
29
|
-
* ```
|
|
30
|
-
* import { p } from '@stacksjs/utils'
|
|
31
|
-
*
|
|
32
|
-
* const items = [1, 2, 3, 4, 5]
|
|
33
|
-
*
|
|
34
|
-
* await p(items)
|
|
35
|
-
* .map(async i => await multiply(i, 3))
|
|
36
|
-
* .filter(async i => await isEven(i))
|
|
37
|
-
* // [6, 12]
|
|
38
|
-
* ```
|
|
39
|
-
*/
|
|
40
|
-
export declare function p<T = any>(items?: Iterable<T>, options?: POptions): PInstance<T>;
|
|
41
|
-
export {};
|
package/src/p.ts
DELETED
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
import pLimit from 'p-limit'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Internal marker for filtered items
|
|
5
|
-
*/
|
|
6
|
-
const VOID = Symbol('p-void')
|
|
7
|
-
|
|
8
|
-
interface POptions {
|
|
9
|
-
/**
|
|
10
|
-
* How many promises are resolved at the same time.
|
|
11
|
-
*/
|
|
12
|
-
concurrency?: number | undefined
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
class PInstance<T = any> extends Promise<Awaited<T>[]> {
|
|
16
|
-
private promises = new Set<T | Promise<T>>()
|
|
17
|
-
|
|
18
|
-
get promise(): Promise<Awaited<T>[]> {
|
|
19
|
-
let batch
|
|
20
|
-
const items = [...Array.from(this.items), ...Array.from(this.promises)]
|
|
21
|
-
|
|
22
|
-
if (this.options?.concurrency) {
|
|
23
|
-
const limit = pLimit(this.options.concurrency)
|
|
24
|
-
batch = Promise.all(items.map(p => limit(() => p)))
|
|
25
|
-
}
|
|
26
|
-
else {
|
|
27
|
-
batch = Promise.all(items)
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
return batch.then(l => l.filter((i: any) => i !== VOID))
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
constructor(public items: Iterable<T> = [], public options?: POptions) {
|
|
34
|
-
super(() => {})
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
add(...args: (T | Promise<T>)[]) {
|
|
38
|
-
args.forEach((i) => {
|
|
39
|
-
this.promises.add(i)
|
|
40
|
-
})
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
map<U>(fn: (value: Awaited<T>, index: number) => U): PInstance<Promise<U>> {
|
|
44
|
-
return new PInstance(
|
|
45
|
-
Array.from(this.items)
|
|
46
|
-
.map(async (i, idx) => {
|
|
47
|
-
const v = await i
|
|
48
|
-
if ((v as any) === VOID)
|
|
49
|
-
return VOID as unknown as U
|
|
50
|
-
return fn(v, idx)
|
|
51
|
-
}),
|
|
52
|
-
this.options,
|
|
53
|
-
)
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
filter(fn: (value: Awaited<T>, index: number) => boolean | Promise<boolean>): PInstance<Promise<T>> {
|
|
57
|
-
return new PInstance(
|
|
58
|
-
Array.from(this.items)
|
|
59
|
-
.map(async (i, idx) => {
|
|
60
|
-
const v = await i
|
|
61
|
-
const r = await fn(v, idx)
|
|
62
|
-
if (!r)
|
|
63
|
-
return VOID as unknown as T
|
|
64
|
-
return v
|
|
65
|
-
}),
|
|
66
|
-
this.options,
|
|
67
|
-
)
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
forEach(fn: (value: Awaited<T>, index: number) => void): Promise<void> {
|
|
71
|
-
return this.map(fn).then()
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
reduce<U>(fn: (previousValue: U, currentValue: Awaited<T>, currentIndex: number, array: Awaited<T>[]) => U, initialValue: U): Promise<U> {
|
|
75
|
-
return this.promise.then(array => array.reduce(fn, initialValue))
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
clear() {
|
|
79
|
-
this.promises.clear()
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
then(fn?: () => PromiseLike<any>) {
|
|
83
|
-
const p = this.promise
|
|
84
|
-
|
|
85
|
-
if (fn)
|
|
86
|
-
return p.then(fn)
|
|
87
|
-
|
|
88
|
-
return p
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
catch(fn?: (err: unknown) => PromiseLike<any>) {
|
|
92
|
-
return this.promise.catch(fn)
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
finally(fn?: () => void) {
|
|
96
|
-
return this.promise.finally(fn)
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* Utility for managing multiple promises.
|
|
102
|
-
*
|
|
103
|
-
* @see https://github.com/antfu/utils/tree/main/docs/p.md
|
|
104
|
-
* @category Promise
|
|
105
|
-
* @example
|
|
106
|
-
* ```
|
|
107
|
-
* import { p } from '@stacksjs/utils'
|
|
108
|
-
*
|
|
109
|
-
* const items = [1, 2, 3, 4, 5]
|
|
110
|
-
*
|
|
111
|
-
* await p(items)
|
|
112
|
-
* .map(async i => await multiply(i, 3))
|
|
113
|
-
* .filter(async i => await isEven(i))
|
|
114
|
-
* // [6, 12]
|
|
115
|
-
* ```
|
|
116
|
-
*/
|
|
117
|
-
export function p<T = any>(items?: Iterable<T>, options?: POptions): PInstance<T> {
|
|
118
|
-
return new PInstance(items, options)
|
|
119
|
-
}
|