Skip to content

Commit

Permalink
fix: flat type in fx (#267)
Browse files Browse the repository at this point in the history
* fix: flat type

* chore: ignore lint
  • Loading branch information
ppeeou authored Apr 15, 2024
1 parent 59e6a87 commit 4a490a0
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 deletions.
13 changes: 9 additions & 4 deletions src/Lazy/fx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import join from "../join";
import reduce from "../reduce";
import some from "../some";
import type Cast from "../types/Cast";
import type { DeepFlat } from "../types/DeepFlat";
import type IterableInfer from "../types/IterableInfer";
import type Key from "../types/Key";
import type { SyncReducer } from "../types/Reducer";
Expand Down Expand Up @@ -71,8 +72,10 @@ class FxAsyncIterable<A> {
*
* see {@link https://fxts.dev/docs/flat | flat}
*/
flat(depth?: number) {
return new FxAsyncIterable(flat(this.asyncIterable, depth));
flat<T extends number = 1>(depth?: T) {
return new FxAsyncIterable(
flat(this.asyncIterable, depth),
) as FxAsyncIterable<DeepFlat<A, T>>;
}

/**
Expand Down Expand Up @@ -326,8 +329,10 @@ export class FxIterable<A> {
*
* see {@link https://fxts.dev/docs/flat | flat}
*/
flat(depth?: number) {
return new FxIterable(flat(this.iterable, depth));
flat<T extends number = 1>(depth?: T) {
const res = flat(this.iterable, depth);

return new FxIterable(res) as FxIterable<DeepFlat<A, T>>;
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/unicodeToArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const rsSymbol = `(?:${[
].join("|")})`;

/** Used to match [string symbols](https://mathiasbynens.be/notes/javascript-unicode). */
// eslint-disable-next-line no-misleading-character-class
const reUnicode = RegExp(`${rsFitz}(?=${rsFitz})|${rsSymbol + rsSeq}`, "g");

/**
Expand Down
45 changes: 44 additions & 1 deletion type-check/Lazy/flat.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { flat, pipe, range, toAsync } from "../../src";
import { flat, fx, pipe, range, toArray, toAsync } from "../../src";
import * as Test from "../../src/types/Test";

const { checks, check } = Test;
Expand Down Expand Up @@ -29,6 +29,33 @@ const res16 = flat(
})(),
);

const res17 = fx([1, 2]).flat().toArray();
// prettier-ignore
const res18 = fx([1, [2]]).flat().toArray();
// prettier-ignore
const res19 = fx([1, [2, [3]]]).flat().toArray();
// prettier-ignore
const res20 = fx([1, [2, [3]]]).flat(2).toArray();

const res21 = fx(["a", "b"]).flat().toArray();
// prettier-ignore
const res22 = fx(['a', ['b']]).flat().toArray();
// prettier-ignore
const res23 = fx(['a', ['b', ['c']]]).flat().toArray();
// prettier-ignore
const res24 = fx(['a', ['b', ['c']]]).flat(2).toArray();

// prettier-ignore
const res25 = fx(['a', 'b']).toAsync().flat().toArray();
// prettier-ignore
const res26 = fx(['a', ['b']]).toAsync().flat().toArray();

// prettier-ignore
const res27 = fx(['a', ['b', ['c']]]).toAsync().flat().toArray();
const res28 = pipe(["a", ["b", ["c"]]], toAsync, flat, toArray);
// prettier-ignore
const res29 = fx(['a', ['b', ['c']]]).toAsync().flat(2).toArray();

checks([
check<typeof res1, IterableIterator<never>, Test.Pass>(),
check<typeof res2, IterableIterator<number>, Test.Pass>(),
Expand All @@ -49,4 +76,20 @@ checks([
check<typeof res14, AsyncIterableIterator<number>, Test.Pass>(),
check<typeof res15, AsyncIterableIterator<number | number[]>, Test.Pass>(), // prettier-ignore
check<typeof res16, IterableIterator<number | AsyncIterableIterator<number>>, Test.Pass>(), // prettier-ignore

check<typeof res17, number[], Test.Pass>(),
check<typeof res18, number[], Test.Pass>(),
check<typeof res19, (number | number[])[], Test.Pass>(),
check<typeof res20, number[], Test.Pass>(),

check<typeof res21, string[], Test.Pass>(),
check<typeof res22, string[], Test.Pass>(),
check<typeof res23, (string | string[])[], Test.Pass>(),
check<typeof res24, string[], Test.Pass>(),

check<typeof res25, Promise<string[]>, Test.Pass>(),
check<typeof res26, Promise<string[]>, Test.Pass>(),
check<typeof res27, Promise<(string | string[])[]>, Test.Pass>(),
check<typeof res28, Promise<(string | string[])[]>, Test.Pass>(),
check<typeof res29, Promise<string[]>, Test.Pass>(),
]);
2 changes: 2 additions & 0 deletions type-check/pipe.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,11 @@ const Code = {
} as const;

type Code = (typeof Code)[keyof typeof Code];
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const res18 = pipe({ code: Code.None }, (_: { code: Code }): Code => Code.None);
const res19 = pipe(
{ code: Code.None },
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async (_: { code: Code }): Promise<Code> => Code.None,
);

Expand Down

0 comments on commit 4a490a0

Please sign in to comment.