Skip to content

Commit a30a1db

Browse files
committed
smart-filter
1 parent 18177dd commit a30a1db

File tree

6 files changed

+23
-24
lines changed

6 files changed

+23
-24
lines changed

src/composition.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { reverse } from "./array.ts";
2-
import { ComposeMany } from "./composeTyping.ts";
2+
import type { ComposeMany } from "./composeTyping.ts";
33
import { not } from "./operator.ts";
44
import { isPromise } from "./promise.ts";
55
import { reduce } from "./reduce.ts";

src/filter.test.ts

+4
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,16 @@ Deno.test("async filter", async () => {
1919
});
2020

2121
const _nums: number[] = remove((x: number) => x > 0)([1, 2, 3]);
22+
2223
// @ts-expect-error should preserve typing information
2324
const _strings: string[] = remove((x: number) => x > 0)([1, 2, 3]);
25+
2426
const _strings_promise: Promise<number[]> = remove((x: number) =>
2527
Promise.resolve(x > 0)
2628
)([1, 2, 3]);
2729

30+
const _predicateTyping: 1[] = filter((x: number) => x == 1)([1, 2, 3]);
31+
2832
Deno.test("async filter", async () => {
2933
assertEquals(
3034
await remove((arg: number) => wrapPromise(arg % 2 === 0))([

src/filter.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1-
import type { Func, IsAsync, ParamOf } from "./typing.ts";
2-
import { complement, pipe } from "./composition.ts";
31
import { head, second } from "./array.ts";
4-
5-
import { map } from "./map.ts";
2+
import { complement, pipe } from "./composition.ts";
63
import { pairRight } from "./juxt.ts";
4+
import { map } from "./map.ts";
75
import { isPromise } from "./promise.ts";
6+
import type { Func, IsAsync, ParamOf } from "./typing.ts";
7+
8+
type ConstrainedTyping<F extends Func> = F extends
9+
((value: ParamOf<F>) => value is infer S) ? S[]
10+
: ParamOf<F>[];
811

912
export const filter = <F extends Func>(f: F): (
1013
_: ParamOf<F>[],
11-
) => true extends IsAsync<F> ? Promise<ParamOf<F>[]> : ParamOf<F>[] =>
14+
) => true extends IsAsync<F> ? Promise<ConstrainedTyping<F>>
15+
: ConstrainedTyping<F> =>
1216
// @ts-expect-error typing head is hard.
1317
pipe(
1418
map(pairRight(f)),

src/juxt.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { allmap, anymap, concat, zip } from "./array.ts";
22
import { identity, pipe } from "./composition.ts";
3+
import { isPromise } from "./promise.ts";
4+
import { map } from "./map.ts";
35
import type {
46
AnyAsync,
57
Func,
@@ -8,9 +10,6 @@ import type {
810
Union,
911
} from "./typing.ts";
1012

11-
import { map } from "./map.ts";
12-
import { isPromise } from "./index.ts";
13-
1413
type Results<Functions extends Func[]> = {
1514
[i in keyof Functions]: ReturnTypeUnwrapped<Functions[i]>;
1615
};

src/map.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type { IsAsync, Unary, UnaryFnUntyped } from "./typing.ts";
2-
32
import { errorBoundry, pipe } from "./composition.ts";
43
import { reduce } from "./reduce.ts";
54
import { isPromise } from "./promise.ts";

src/mapping.ts

+7-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
import { head, second, wrapArray } from "./array.ts";
2+
import { applyTo, identity, pipe } from "./composition.ts";
3+
import { filter } from "./filter.ts";
4+
import { stack } from "./juxt.ts";
5+
import { map } from "./map.ts";
6+
import { reduce } from "./reduce.ts";
17
import type {
2-
AsyncFunction,
38
ElementOf,
49
Func,
510
IsAsync,
@@ -8,13 +13,6 @@ import type {
813
ReturnTypeUnwrapped,
914
Unary,
1015
} from "./typing.ts";
11-
import { applyTo, identity, pipe } from "./composition.ts";
12-
import { head, second, wrapArray } from "./array.ts";
13-
14-
import { filter } from "./filter.ts";
15-
import { map } from "./map.ts";
16-
import { reduce } from "./reduce.ts";
17-
import { stack } from "./juxt.ts";
1816

1917
export const wrapObject = <V>(key: string) => (value: V) => ({ [key]: value });
2018

@@ -115,12 +113,7 @@ const onEntries = <
115113

116114
export const entryMap = pipe(map, onEntries);
117115

118-
export const entryFilter = <
119-
Function extends (
120-
// deno-lint-ignore no-explicit-any
121-
((kv: [any, any]) => any)
122-
),
123-
>(f: Function) => onEntries(filter(f));
116+
export const entryFilter = pipe(filter, onEntries);
124117

125118
type RecordKey = string | number | symbol;
126119
type EntryMap<

0 commit comments

Comments
 (0)