Skip to content

Commit

Permalink
Merge pull request #7 from dphilipson/dp/type-guards
Browse files Browse the repository at this point in the history
Support type guards in filter-like functions
  • Loading branch information
dphilipson authored Feb 10, 2020
2 parents 8d90e35 + 8dc1e5e commit 7673786
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
19 changes: 19 additions & 0 deletions src/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export interface TransformChain<T> {
dedupe(): TransformChain<T>;
drop(n: number): TransformChain<T>;
dropWhile(pred: (item: T) => boolean): TransformChain<T>;
filter<U extends T>(pred: (item: T) => item is U): TransformChain<U>;
filter(pred: (item: T) => boolean): TransformChain<T>;
flatMap<U>(f: (item: T) => Iterable<U>): TransformChain<U>;
flatten: T extends Iterable<infer U> ? () => TransformChain<U> : void;
Expand All @@ -59,10 +60,14 @@ export interface TransformChain<T> {
mapIndexed<U>(f: (item: T, index: number) => U): TransformChain<U>;
partitionAll(n: number): TransformChain<T[]>;
partitionBy(pred: (item: T) => any): TransformChain<T[]>;
remove<U extends T>(
pred: (item: T) => item is U,
): TransformChain<Exclude<T, U>>;
remove(pred: (item: T) => boolean): TransformChain<T>;
removeAbsent(): TransformChain<NonNullable<T>>;
take(n: number): TransformChain<T>;
takeNth(n: number): TransformChain<T>;
takeWhile<U extends T>(pred: (item: T) => item is U): TransformChain<U>;
takeWhile(pred: (item: T) => boolean): TransformChain<T>;

reduce<TResult>(
Expand All @@ -76,6 +81,7 @@ export interface TransformChain<T> {
average: T extends number ? () => number | null : void;
count(): number;
every(pred: (item: T) => boolean): boolean;
find<U extends T>(pred: (item: T) => item is U): U | null;
find(pred: (item: T) => boolean): T | null;
first(): T | null;
forEach(f: (item: T) => void): void;
Expand Down Expand Up @@ -120,6 +126,9 @@ export interface TransducerBuilder<TBase, T> {
dedupe(): TransducerBuilder<TBase, T>;
drop(n: number): TransducerBuilder<TBase, T>;
dropWhile(pred: (item: T) => boolean): TransducerBuilder<TBase, T>;
filter<U extends T>(
pred: (item: T) => item is U,
): TransducerBuilder<TBase, U>;
filter(pred: (item: T) => boolean): TransducerBuilder<TBase, T>;
flatMap<U>(f: (item: T) => Iterable<U>): TransducerBuilder<TBase, U>;
flatten: T extends Iterable<infer U>
Expand All @@ -132,10 +141,16 @@ export interface TransducerBuilder<TBase, T> {
): TransducerBuilder<TBase, U>;
partitionAll(n: number): TransducerBuilder<TBase, T[]>;
partitionBy(pred: (item: T) => boolean): TransducerBuilder<TBase, T[]>;
remove<U extends T>(
pred: (item: T) => item is U,
): TransducerBuilder<TBase, Exclude<T, U>>;
remove(pred: (item: T) => boolean): TransducerBuilder<TBase, T>;
removeAbsent(): TransducerBuilder<TBase, NonNullable<T>>;
take(n: number): TransducerBuilder<TBase, T>;
takeNth(n: number): TransducerBuilder<TBase, T>;
takeWhile<U extends T>(
pred: (item: T) => item is U,
): TransducerBuilder<TBase, U>;
takeWhile(pred: (item: T) => boolean): TransducerBuilder<TBase, T>;

build(): Transducer<TBase, T>;
Expand Down Expand Up @@ -222,6 +237,10 @@ class TransducerChain<TBase, T> implements CombinedBuilder<TBase, T> {
return this.compose(partitionBy(f));
}

public remove<U extends T>(
pred: (item: T) => item is U,
): CombinedBuilder<TBase, Exclude<T, U>>;
public remove(pred: (item: T) => boolean): CombinedBuilder<TBase, T>;
public remove(pred: (item: T) => boolean): CombinedBuilder<TBase, T> {
return this.compose(remove(pred));
}
Expand Down
4 changes: 4 additions & 0 deletions src/reducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ export function every<T>(pred: (item: T) => boolean): Transformer<boolean, T> {
return remove(pred)(isEmpty());
}

export function find<T, U extends T>(
pred: (item: T) => item is U,
): Transformer<U | null, T>;
export function find<T>(pred: (item: T) => boolean): Transformer<T | null, T>;
export function find<T>(pred: (item: T) => boolean): Transformer<T | null, T> {
return filter(pred)(first());
}
Expand Down
13 changes: 12 additions & 1 deletion src/transducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,10 @@ class Filter<TResult, TCompleteResult, TInput>
return this.pred(input) ? this.xf[STEP](result, input) : result;
}
}

export function filter<T, U extends T>(
pred: (item: T) => item is U,
): Transducer<T, U>;
export function filter<T>(pred: (item: T) => boolean): Transducer<T, T>;
export function filter<T>(pred: (item: T) => boolean): Transducer<T, T> {
return xf => new Filter(xf, pred);
}
Expand Down Expand Up @@ -473,6 +476,10 @@ class Take<TResult, TCompleteResult, TInput>
}
}

export function remove<T, U extends T>(
pred: (item: T) => item is U,
): Transducer<T, Exclude<T, U>>;
export function remove<T>(pred: (item: T) => boolean): Transducer<T, T>;
export function remove<T>(pred: (item: T) => boolean): Transducer<T, T> {
return filter(item => !pred(item));
}
Expand Down Expand Up @@ -550,6 +557,10 @@ class TakeWhile<TResult, TCompleteResult, TInput>
}
}

export function takeWhile<T, U extends T>(
pred: (item: T) => item is U,
): Transducer<T, U>;
export function takeWhile<T>(pred: (item: T) => boolean): Transducer<T, T>;
export function takeWhile<T>(pred: (item: T) => boolean): Transducer<T, T> {
return xf => new TakeWhile(xf, pred);
}

0 comments on commit 7673786

Please sign in to comment.