Skip to content

Commit

Permalink
smart-filter
Browse files Browse the repository at this point in the history
  • Loading branch information
uriva committed Jan 8, 2025
1 parent 18177dd commit a30a1db
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/composition.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { reverse } from "./array.ts";
import { ComposeMany } from "./composeTyping.ts";
import type { ComposeMany } from "./composeTyping.ts";
import { not } from "./operator.ts";
import { isPromise } from "./promise.ts";
import { reduce } from "./reduce.ts";
Expand Down
4 changes: 4 additions & 0 deletions src/filter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@ Deno.test("async filter", async () => {
});

const _nums: number[] = remove((x: number) => x > 0)([1, 2, 3]);

// @ts-expect-error should preserve typing information
const _strings: string[] = remove((x: number) => x > 0)([1, 2, 3]);

const _strings_promise: Promise<number[]> = remove((x: number) =>
Promise.resolve(x > 0)
)([1, 2, 3]);

const _predicateTyping: 1[] = filter((x: number) => x == 1)([1, 2, 3]);

Deno.test("async filter", async () => {
assertEquals(
await remove((arg: number) => wrapPromise(arg % 2 === 0))([
Expand Down
14 changes: 9 additions & 5 deletions src/filter.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import type { Func, IsAsync, ParamOf } from "./typing.ts";
import { complement, pipe } from "./composition.ts";
import { head, second } from "./array.ts";

import { map } from "./map.ts";
import { complement, pipe } from "./composition.ts";
import { pairRight } from "./juxt.ts";
import { map } from "./map.ts";
import { isPromise } from "./promise.ts";
import type { Func, IsAsync, ParamOf } from "./typing.ts";

type ConstrainedTyping<F extends Func> = F extends
((value: ParamOf<F>) => value is infer S) ? S[]
: ParamOf<F>[];

export const filter = <F extends Func>(f: F): (
_: ParamOf<F>[],
) => true extends IsAsync<F> ? Promise<ParamOf<F>[]> : ParamOf<F>[] =>
) => true extends IsAsync<F> ? Promise<ConstrainedTyping<F>>
: ConstrainedTyping<F> =>
// @ts-expect-error typing head is hard.
pipe(
map(pairRight(f)),
Expand Down
5 changes: 2 additions & 3 deletions src/juxt.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { allmap, anymap, concat, zip } from "./array.ts";
import { identity, pipe } from "./composition.ts";
import { isPromise } from "./promise.ts";
import { map } from "./map.ts";
import type {
AnyAsync,
Func,
Expand All @@ -8,9 +10,6 @@ import type {
Union,
} from "./typing.ts";

import { map } from "./map.ts";
import { isPromise } from "./index.ts";

type Results<Functions extends Func[]> = {
[i in keyof Functions]: ReturnTypeUnwrapped<Functions[i]>;
};
Expand Down
1 change: 0 additions & 1 deletion src/map.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { IsAsync, Unary, UnaryFnUntyped } from "./typing.ts";

import { errorBoundry, pipe } from "./composition.ts";
import { reduce } from "./reduce.ts";
import { isPromise } from "./promise.ts";
Expand Down
21 changes: 7 additions & 14 deletions src/mapping.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { head, second, wrapArray } from "./array.ts";
import { applyTo, identity, pipe } from "./composition.ts";
import { filter } from "./filter.ts";
import { stack } from "./juxt.ts";
import { map } from "./map.ts";
import { reduce } from "./reduce.ts";
import type {
AsyncFunction,
ElementOf,
Func,
IsAsync,
Expand All @@ -8,13 +13,6 @@ import type {
ReturnTypeUnwrapped,
Unary,
} from "./typing.ts";
import { applyTo, identity, pipe } from "./composition.ts";
import { head, second, wrapArray } from "./array.ts";

import { filter } from "./filter.ts";
import { map } from "./map.ts";
import { reduce } from "./reduce.ts";
import { stack } from "./juxt.ts";

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

Expand Down Expand Up @@ -115,12 +113,7 @@ const onEntries = <

export const entryMap = pipe(map, onEntries);

export const entryFilter = <
Function extends (
// deno-lint-ignore no-explicit-any
((kv: [any, any]) => any)
),
>(f: Function) => onEntries(filter(f));
export const entryFilter = pipe(filter, onEntries);

type RecordKey = string | number | symbol;
type EntryMap<
Expand Down

0 comments on commit a30a1db

Please sign in to comment.