Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support Promise object tofx #289

Closed
wants to merge 1 commit into from
Closed
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
58 changes: 53 additions & 5 deletions src/Lazy/fx.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isAsyncIterable, isIterable } from "../_internal/utils";
import { isAsyncIterable, isIterable, isPromise } from "../_internal/utils";
import consume from "../consume";
import each from "../each";
import every from "../every";
Expand Down Expand Up @@ -593,6 +593,8 @@ export class FxIterable<A> {
}
}

type NonPromise<T> = T extends Promise<unknown> ? never : T;

/**
* `fx` allows functions provided by existing `fxts` to be used in a method chaining.
* Not all functions are provided as methods and can be connected through `chain` if necessary.
Expand Down Expand Up @@ -621,18 +623,64 @@ export class FxIterable<A> {
* .toArray(); // [11, 12, 13, 14]
* ```
*/
function fx<T extends Iterable<unknown> | AsyncIterable<unknown>>(
function fx<
T extends
| Promise<Iterable<unknown>>
| Iterable<unknown>
| AsyncIterable<unknown>,
>(
a: T,
): T extends Iterable<unknown>
? FxIterable<IterableInfer<T>>
: FxAsyncIterable<IterableInfer<T>> {
: T extends Promise<Iterable<infer U>>
? FxAsyncIterable<U>
: FxAsyncIterable<IterableInfer<NonPromise<T>>> {
if (isAsyncIterable(a)) {
return new FxAsyncIterable(a) as any;
} else if (isIterable(a)) {
return new FxIterable(a) as any;
} else if (isPromise(a)) {
let started = false;
let itered = false;
let iter: any;

const next = async (): Promise<{
done: boolean;
value: any;
}> => {
if (!started) {
iter = await a;
started = true;
}

if (itered) {
return iter.next();
}

if (isIterable(iter)) {
iter = iter[Symbol.iterator]();
itered = true;
return next();
} else {
return { done: true, value: iter };
}
};

const asyncIter = {
[Symbol.asyncIterator]() {
return this;
},
async next() {
return next();
},
};

return new FxAsyncIterable(asyncIter) as any;
} else {
throw new TypeError(
`'fx' must be type of Iterable or AsyncIterable or Promise`,
);
}

throw new TypeError(`'fx' must be type of Iterable or AsyncIterable`);
}

export default fx;
30 changes: 29 additions & 1 deletion test/Lazy/fx.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fx, size, toArray, toAsync, uniq } from "../../src";
import { delay, fx, size, toArray, toAsync, uniq } from "../../src";

describe("fx", function () {
describe("sync", function () {
Expand Down Expand Up @@ -58,5 +58,33 @@ describe("fx", function () {

expect(arrSize).toEqual(3);
});

it("handle Promise object", async function () {
const res = await fx(Promise.resolve([1, 2, 3, 4])).toArray();
expect(res).toEqual([1, 2, 3, 4]);
});

it("handle Promise.all object", async function () {
const res1 = fx(
Promise.all([
Promise.resolve(1),
Promise.resolve(2),
Promise.resolve(3),
Promise.resolve(4),
]),
);
expect(await res1.toArray()).toEqual([1, 2, 3, 4]);

const res2 = await fx(
Promise.all([
delay(1000, 1),
Promise.resolve(2),
delay(1000, 3),
Promise.resolve(4),
]),
).toArray();

expect(res2).toEqual([1, 2, 3, 4]);
}, 1050);
});
});
5 changes: 5 additions & 0 deletions type-check/Lazy/fx.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ const res7 = fx(toAsync([1, 2, 3]))
const res8 = [...fx([1, 2, 3])];
const res9 = [...fx("abc")];

const res10 = fx(Promise.resolve([1]));
const res11 = fx(Promise.all([Promise.resolve(1)]));

checks([
check<typeof res1, Cast<Iterable<number>, typeof res1>, Test.Pass>(),
check<typeof res2, number[], Test.Pass>(),
Expand All @@ -34,4 +37,6 @@ checks([
check<typeof res7, Promise<number[]>, Test.Pass>(),
check<typeof res8, number[], Test.Pass>(),
check<typeof res9, string[], Test.Pass>(),
check<typeof res10, Cast<AsyncIterable<number>, typeof res10>, Test.Pass>(),
check<typeof res11, Cast<AsyncIterable<number>, typeof res11>, Test.Pass>(),
]);
Loading