From 8dc1e5e4cd24e3e7ae1e7a8f52f1538e8a9d1ee3 Mon Sep 17 00:00:00 2001 From: David Philipson Date: Mon, 10 Feb 2020 10:55:52 -0800 Subject: [PATCH] Support type guards in filter-like functions --- src/chain.ts | 19 +++++++++++++++++++ src/reducers.ts | 4 ++++ src/transducers.ts | 13 ++++++++++++- 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/chain.ts b/src/chain.ts index dfdffc9..5c8e0df 100644 --- a/src/chain.ts +++ b/src/chain.ts @@ -51,6 +51,7 @@ export interface TransformChain { dedupe(): TransformChain; drop(n: number): TransformChain; dropWhile(pred: (item: T) => boolean): TransformChain; + filter(pred: (item: T) => item is U): TransformChain; filter(pred: (item: T) => boolean): TransformChain; flatMap(f: (item: T) => Iterable): TransformChain; flatten: T extends Iterable ? () => TransformChain : void; @@ -59,10 +60,14 @@ export interface TransformChain { mapIndexed(f: (item: T, index: number) => U): TransformChain; partitionAll(n: number): TransformChain; partitionBy(pred: (item: T) => any): TransformChain; + remove( + pred: (item: T) => item is U, + ): TransformChain>; remove(pred: (item: T) => boolean): TransformChain; removeAbsent(): TransformChain>; take(n: number): TransformChain; takeNth(n: number): TransformChain; + takeWhile(pred: (item: T) => item is U): TransformChain; takeWhile(pred: (item: T) => boolean): TransformChain; reduce( @@ -76,6 +81,7 @@ export interface TransformChain { average: T extends number ? () => number | null : void; count(): number; every(pred: (item: T) => boolean): boolean; + find(pred: (item: T) => item is U): U | null; find(pred: (item: T) => boolean): T | null; first(): T | null; forEach(f: (item: T) => void): void; @@ -120,6 +126,9 @@ export interface TransducerBuilder { dedupe(): TransducerBuilder; drop(n: number): TransducerBuilder; dropWhile(pred: (item: T) => boolean): TransducerBuilder; + filter( + pred: (item: T) => item is U, + ): TransducerBuilder; filter(pred: (item: T) => boolean): TransducerBuilder; flatMap(f: (item: T) => Iterable): TransducerBuilder; flatten: T extends Iterable @@ -132,10 +141,16 @@ export interface TransducerBuilder { ): TransducerBuilder; partitionAll(n: number): TransducerBuilder; partitionBy(pred: (item: T) => boolean): TransducerBuilder; + remove( + pred: (item: T) => item is U, + ): TransducerBuilder>; remove(pred: (item: T) => boolean): TransducerBuilder; removeAbsent(): TransducerBuilder>; take(n: number): TransducerBuilder; takeNth(n: number): TransducerBuilder; + takeWhile( + pred: (item: T) => item is U, + ): TransducerBuilder; takeWhile(pred: (item: T) => boolean): TransducerBuilder; build(): Transducer; @@ -222,6 +237,10 @@ class TransducerChain implements CombinedBuilder { return this.compose(partitionBy(f)); } + public remove( + pred: (item: T) => item is U, + ): CombinedBuilder>; + public remove(pred: (item: T) => boolean): CombinedBuilder; public remove(pred: (item: T) => boolean): CombinedBuilder { return this.compose(remove(pred)); } diff --git a/src/reducers.ts b/src/reducers.ts index 6008289..46ca554 100644 --- a/src/reducers.ts +++ b/src/reducers.ts @@ -25,6 +25,10 @@ export function every(pred: (item: T) => boolean): Transformer { return remove(pred)(isEmpty()); } +export function find( + pred: (item: T) => item is U, +): Transformer; +export function find(pred: (item: T) => boolean): Transformer; export function find(pred: (item: T) => boolean): Transformer { return filter(pred)(first()); } diff --git a/src/transducers.ts b/src/transducers.ts index 76b4302..781cda3 100644 --- a/src/transducers.ts +++ b/src/transducers.ts @@ -172,7 +172,10 @@ class Filter return this.pred(input) ? this.xf[STEP](result, input) : result; } } - +export function filter( + pred: (item: T) => item is U, +): Transducer; +export function filter(pred: (item: T) => boolean): Transducer; export function filter(pred: (item: T) => boolean): Transducer { return xf => new Filter(xf, pred); } @@ -473,6 +476,10 @@ class Take } } +export function remove( + pred: (item: T) => item is U, +): Transducer>; +export function remove(pred: (item: T) => boolean): Transducer; export function remove(pred: (item: T) => boolean): Transducer { return filter(item => !pred(item)); } @@ -550,6 +557,10 @@ class TakeWhile } } +export function takeWhile( + pred: (item: T) => item is U, +): Transducer; +export function takeWhile(pred: (item: T) => boolean): Transducer; export function takeWhile(pred: (item: T) => boolean): Transducer { return xf => new TakeWhile(xf, pred); }