Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions packages/util/src/@prototypes/array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
async function fromAsync<T>(
iterableOrArrayLike: AsyncIterable<T> | Iterable<T | PromiseLike<T>> | ArrayLike<T | PromiseLike<T>>,
) {
const array: T[] = [];
for await (const element of await iterableOrArrayLike as AsyncGenerator<PromiseLike<T>>) {
array.push(await element);
}
return array;
}

async function mappedFromAsync<T, U>(
iterableOrArrayLike: AsyncIterable<T> | Iterable<T | PromiseLike<T>> | ArrayLike<T | PromiseLike<T>>,
mapFn: (value: Awaited<T>, index: number) => U,
) {
const array: U[] = [];
for await (const element of await iterableOrArrayLike as AsyncGenerator<PromiseLike<T>>) {
array.push(mapFn(await element, array.length));
}
return array;
}

/**
* Designed for Node.js 20 compatibility
*/
Array.fromAsync ??= <T, U>(
iterableOrArrayLike: AsyncIterable<T> | Iterable<T | PromiseLike<T>> | ArrayLike<T | PromiseLike<T>>,
mapFn?: (value: Awaited<T>, index: number) => U,
) => typeof mapFn === "function"
? mappedFromAsync(iterableOrArrayLike, mapFn)
: fromAsync(iterableOrArrayLike);
1 change: 1 addition & 0 deletions packages/util/src/@prototypes/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "./array";
3 changes: 1 addition & 2 deletions packages/util/src/glob/fsGlob.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { type Dirent } from "fs";
import { stat } from "fs/promises";
import { join } from "path";
import { asyncGeneratorToArray } from "../utils/array";
import { DISCLOUD_IGNORE_FILENAME } from "./constants";
import { Ignore } from "./ignore";
import type { FSGlobOptions, FSGlobOptionsWithFileTypes, FSGlobOptionsWithoutFileTypes } from "./types";
Expand All @@ -10,7 +9,7 @@ export function fsGlob(pattern: string | string[], options?: FSGlobOptionsWithou
export function fsGlob(pattern: string | string[], options?: FSGlobOptionsWithFileTypes): Promise<Dirent[]>
export function fsGlob(pattern: string | string[], options?: FSGlobOptions): Promise<Array<string | Dirent>>
export function fsGlob(pattern: string | string[], options?: any): Promise<unknown> {
return asyncGeneratorToArray(fsGlobIterate(pattern, options));
return Array.fromAsync(fsGlobIterate(pattern, options));
}

export function fsGlobIterate(pattern: string | string[], options?: FSGlobOptionsWithoutFileTypes): AsyncGenerator<string>
Expand Down
3 changes: 1 addition & 2 deletions packages/util/src/glob/glob.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import * as globMudule from "glob";
import { type } from "os";
import { asyncGeneratorToArray } from "../utils/array";
import { DISCLOUD_IGNORE_FILENAME } from "./constants";
import { Ignore } from "./ignore";

export function glob(pattern: string | string[], cwd?: string) {
return asyncGeneratorToArray(globIterate(pattern, cwd));
return Array.fromAsync(globIterate(pattern, cwd));
}

export async function* globIterate(pattern: string | string[], cwd?: string) {
Expand Down
3 changes: 1 addition & 2 deletions packages/util/src/glob/ignore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { globIterate } from "glob";
import { globifyGitIgnore } from "globify-gitignore";
import { dirname } from "path";
import { joinWithRoot } from "../path";
import { asyncGeneratorToArray } from "../utils/array";

export class Ignore {
static async globify(content: string, directory?: string, absolute?: boolean) {
Expand Down Expand Up @@ -58,7 +57,7 @@ export class Ignore {
) { }

getIgnorePatterns(cwd?: string) {
return asyncGeneratorToArray(this._getIgnoreIterate(cwd)).then((value) => value.flat());
return Array.fromAsync(this._getIgnoreIterate(cwd)).then((value) => value.flat());
}

protected async* _getIgnoreIterate(cwd?: string) {
Expand Down
1 change: 1 addition & 0 deletions packages/util/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import "source-map-support/register";
import "./@prototypes";

export * from "./BitField";
export * from "./config";
Expand Down
16 changes: 0 additions & 16 deletions packages/util/src/utils/array.ts

This file was deleted.

Loading