@stacksjs/utils 0.39.1 → 0.41.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/base.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export declare const toString: (v: any) => any;
2
+ export declare const loop: (times: number, callback: any) => void;
package/dist/base.mjs ADDED
@@ -0,0 +1,4 @@
1
+ export const toString = (v) => Object.prototype.toString.call(v);
2
+ export const loop = (times, callback) => {
3
+ [...Array(times)].forEach((item, i) => callback(i));
4
+ };
@@ -0,0 +1,18 @@
1
+ import type { Fn, Nullable } from '@stacksjs/types';
2
+ /**
3
+ * Call every function in an array
4
+ */
5
+ export declare function batchInvoke(functions: Nullable<Fn>[]): void;
6
+ /**
7
+ * Pass the value through the callback, and return the value
8
+ *
9
+ * @example
10
+ * ```
11
+ * function createUser(name: string): User {
12
+ * return tap(new User, user => {
13
+ * user.name = name
14
+ * })
15
+ * }
16
+ * ```
17
+ */
18
+ export declare function tap<T>(value: T, callback: (value: T) => void): T;
@@ -0,0 +1,7 @@
1
+ export function batchInvoke(functions) {
2
+ functions.forEach((fn) => fn && fn());
3
+ }
4
+ export function tap(value, callback) {
5
+ callback(value);
6
+ return value;
7
+ }
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Type guard to filter out null-ish values
3
+ *
4
+ * @category Guards
5
+ * @example array.filter(notNullish)
6
+ */
7
+ export declare function notNullish<T>(v: T | null | undefined): v is NonNullable<T>;
8
+ /**
9
+ * Type guard to filter out null values
10
+ *
11
+ * @category Guards
12
+ * @example array.filter(noNull)
13
+ */
14
+ export declare function noNull<T>(v: T | null): v is Exclude<T, null>;
15
+ /**
16
+ * Type guard to filter out null-ish values
17
+ *
18
+ * @category Guards
19
+ * @example array.filter(notUndefined)
20
+ */
21
+ export declare function notUndefined<T>(v: T): v is Exclude<T, undefined>;
22
+ /**
23
+ * Type guard to filter out falsy values
24
+ *
25
+ * @category Guards
26
+ * @example array.filter(isTruthy)
27
+ */
28
+ export declare function isTruthy<T>(v: T): v is NonNullable<T>;
@@ -0,0 +1,12 @@
1
+ export function notNullish(v) {
2
+ return v != null;
3
+ }
4
+ export function noNull(v) {
5
+ return v !== null;
6
+ }
7
+ export function notUndefined(v) {
8
+ return v !== void 0;
9
+ }
10
+ export function isTruthy(v) {
11
+ return Boolean(v);
12
+ }
package/dist/index.d.ts CHANGED
@@ -1,4 +1,11 @@
1
+ export * from './base';
1
2
  export * from './helpers';
3
+ export * from './promise';
4
+ export * from './function';
5
+ export * from './guards';
6
+ export * from './is';
7
+ export * from './p';
8
+ export * from './vendor';
2
9
  export * from '@vueuse/core';
3
10
  export * from '@vueuse/math';
4
11
  export * from '@vueuse/head';
package/dist/index.mjs CHANGED
@@ -1,4 +1,11 @@
1
+ export * from "./base.mjs";
1
2
  export * from "./helpers.mjs";
3
+ export * from "./promise.mjs";
4
+ export * from "./function.mjs";
5
+ export * from "./guards.mjs";
6
+ export * from "./is.mjs";
7
+ export * from "./p.mjs";
8
+ export * from "./vendor.mjs";
2
9
  export * from "@vueuse/core";
3
10
  export * from "@vueuse/math";
4
11
  export * from "@vueuse/head";
package/dist/is.d.ts ADDED
@@ -0,0 +1 @@
1
+ export declare const isBrowser: boolean;
package/dist/is.mjs ADDED
@@ -0,0 +1 @@
1
+ export const isBrowser = typeof window !== "undefined";
package/dist/p.d.ts ADDED
@@ -0,0 +1,41 @@
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 '@antfu/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/dist/p.mjs ADDED
@@ -0,0 +1,75 @@
1
+ import pLimit from "p-limit";
2
+ const VOID = Symbol("p-void");
3
+ class PInstance extends Promise {
4
+ constructor(items = [], options) {
5
+ super(() => {
6
+ });
7
+ this.items = items;
8
+ this.options = options;
9
+ this.promises = /* @__PURE__ */ new Set();
10
+ }
11
+ get promise() {
12
+ let batch;
13
+ const items = [...Array.from(this.items), ...Array.from(this.promises)];
14
+ if (this.options?.concurrency) {
15
+ const limit = pLimit(this.options.concurrency);
16
+ batch = Promise.all(items.map((p2) => limit(() => p2)));
17
+ } else {
18
+ batch = Promise.all(items);
19
+ }
20
+ return batch.then((l) => l.filter((i) => i !== VOID));
21
+ }
22
+ add(...args) {
23
+ args.forEach((i) => {
24
+ this.promises.add(i);
25
+ });
26
+ }
27
+ map(fn) {
28
+ return new PInstance(
29
+ Array.from(this.items).map(async (i, idx) => {
30
+ const v = await i;
31
+ if (v === VOID)
32
+ return VOID;
33
+ return fn(v, idx);
34
+ }),
35
+ this.options
36
+ );
37
+ }
38
+ filter(fn) {
39
+ return new PInstance(
40
+ Array.from(this.items).map(async (i, idx) => {
41
+ const v = await i;
42
+ const r = await fn(v, idx);
43
+ if (!r)
44
+ return VOID;
45
+ return v;
46
+ }),
47
+ this.options
48
+ );
49
+ }
50
+ forEach(fn) {
51
+ return this.map(fn).then();
52
+ }
53
+ reduce(fn, initialValue) {
54
+ return this.promise.then((array) => array.reduce(fn, initialValue));
55
+ }
56
+ clear() {
57
+ this.promises.clear();
58
+ }
59
+ then(fn) {
60
+ const p2 = this.promise;
61
+ if (fn)
62
+ return p2.then(fn);
63
+ else
64
+ return p2;
65
+ }
66
+ catch(fn) {
67
+ return this.promise.catch(fn);
68
+ }
69
+ finally(fn) {
70
+ return this.promise.finally(fn);
71
+ }
72
+ }
73
+ export function p(items, options) {
74
+ return new PInstance(items, options);
75
+ }
@@ -0,0 +1,51 @@
1
+ import type { Fn } from '@stacksjs/types';
2
+ /**
3
+ * Promised `setTimeout`
4
+ *
5
+ * @category Promise
6
+ */
7
+ export declare function sleep(ms: number, callback?: Fn<any>): Promise<void>;
8
+ /**
9
+ * Create a promise lock
10
+ *
11
+ * @category Promise
12
+ * @example
13
+ * ```
14
+ * const lock = createPromiseLock()
15
+ *
16
+ * lock.run(async () => {
17
+ * await doSomething()
18
+ * })
19
+ *
20
+ * // in anther context:
21
+ * await lock.wait() // it will wait all tasking finished
22
+ * ```
23
+ */
24
+ export declare function createPromiseLock(): {
25
+ run<T = void>(fn: () => Promise<T>): Promise<T>;
26
+ wait(): Promise<void>;
27
+ isWaiting(): boolean;
28
+ clear(): void;
29
+ };
30
+ /**
31
+ * Promise with `resolve` and `reject` methods of itself
32
+ */
33
+ export interface ControlledPromise<T = void> extends Promise<T> {
34
+ resolve(value: T | PromiseLike<T>): void;
35
+ reject(reason?: any): void;
36
+ }
37
+ /**
38
+ * Return a Promise with `resolve` and `reject` methods
39
+ *
40
+ * @category Promise
41
+ * @example
42
+ * ```
43
+ * const promise = createControlledPromise()
44
+ *
45
+ * await promise
46
+ *
47
+ * // in anther context:
48
+ * promise.resolve(data)
49
+ * ```
50
+ */
51
+ export declare function createControlledPromise<T>(): ControlledPromise<T>;
@@ -0,0 +1,42 @@
1
+ import { remove } from "@stacksjs/arrays";
2
+ export function sleep(ms, callback) {
3
+ return new Promise(
4
+ (resolve) => setTimeout(async () => {
5
+ await callback?.();
6
+ resolve();
7
+ }, ms)
8
+ );
9
+ }
10
+ export function createPromiseLock() {
11
+ const locks = [];
12
+ return {
13
+ async run(fn) {
14
+ const p = fn();
15
+ locks.push(p);
16
+ try {
17
+ return await p;
18
+ } finally {
19
+ remove(locks, p);
20
+ }
21
+ },
22
+ async wait() {
23
+ await Promise.allSettled(locks);
24
+ },
25
+ isWaiting() {
26
+ return Boolean(locks.length);
27
+ },
28
+ clear() {
29
+ locks.length = 0;
30
+ }
31
+ };
32
+ }
33
+ export function createControlledPromise() {
34
+ let resolve, reject;
35
+ const promise = new Promise((_resolve, _reject) => {
36
+ resolve = _resolve;
37
+ reject = _reject;
38
+ });
39
+ promise.resolve = resolve;
40
+ promise.reject = reject;
41
+ return promise;
42
+ }
@@ -0,0 +1 @@
1
+ export { throttle, debounce } from 'throttle-debounce';
@@ -0,0 +1 @@
1
+ export { throttle, debounce } from "throttle-debounce";
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@stacksjs/utils",
3
3
  "type": "module",
4
- "version": "0.39.1",
5
- "packageManager": "pnpm@7.15.0",
4
+ "version": "0.41.0",
5
+ "packageManager": "pnpm@7.16.0",
6
6
  "description": "The Stacks helper functions.",
7
7
  "author": "Chris Breuer",
8
8
  "license": "MIT",
@@ -38,29 +38,31 @@
38
38
  ],
39
39
  "engines": {
40
40
  "node": ">=v18.12.1",
41
- "pnpm": ">=7.15.0"
41
+ "pnpm": ">=7.16.0"
42
42
  },
43
43
  "peerDependencies": {
44
- "@stacksjs/cli": "0.39.1",
45
- "@stacksjs/config": "0.39.1",
46
- "@stacksjs/lint": "0.39.1",
47
- "@stacksjs/path": "0.39.1",
48
- "@stacksjs/storage": "0.39.1",
49
- "@stacksjs/types": "0.39.1",
44
+ "@stacksjs/cli": "0.41.0",
45
+ "@stacksjs/config": "0.41.0",
46
+ "@stacksjs/lint": "0.41.0",
47
+ "@stacksjs/path": "0.41.0",
48
+ "@stacksjs/storage": "0.41.0",
49
+ "@stacksjs/types": "0.41.0",
50
50
  "@vueuse/core": "^9.5.0",
51
- "@vueuse/head": "^0.9.8",
51
+ "@vueuse/head": "^1.0.13",
52
52
  "@vueuse/math": "^9.5.0",
53
53
  "@vueuse/shared": "^9.5.0",
54
- "defu": "^6.1.0",
54
+ "defu": "^6.1.1",
55
55
  "detect-indent": "^7.0.1",
56
56
  "detect-newline": "^4.0.0",
57
57
  "esno": "^0.16.3",
58
58
  "fast-glob": "^3.2.12",
59
59
  "kolorist": "1.6.0",
60
- "rimraf": "^3.0.2"
60
+ "p-limit": "^4.0.0",
61
+ "rimraf": "^3.0.2",
62
+ "throttle-debounce": "^5.0.0"
61
63
  },
62
64
  "devDependencies": {
63
- "mkdist": "^0.4.0",
65
+ "mkdist": "^1.0.0",
64
66
  "typescript": "^4.8.4"
65
67
  },
66
68
  "scripts": {