Mirror: 🎩 A tiny but capable push & pull stream library for TypeScript and Flow

fix: Add missing filter overload to allow for type narrowing (#149)

Changed files
+16 -5
.changeset
src
+5
.changeset/khaki-lemons-argue.md
···
···
+
---
+
'wonka': patch
+
---
+
+
Add missing overload definition for `filter`, which allows types to be narrowed, e.g. by specifying a type predicate return type.
+4 -2
src/__tests__/operators.test.ts
···
passesAsyncSequence(noop);
it('prevents emissions for which a predicate fails', () => {
-
const { source, next } = sources.makeSubject();
const fn = vi.fn();
-
sinks.forEach(fn)(operators.filter(x => !!x)(source));
next(false);
expect(fn).not.toHaveBeenCalled();
···
passesAsyncSequence(noop);
it('prevents emissions for which a predicate fails', () => {
+
const { source, next } = sources.makeSubject<boolean>();
const fn = vi.fn();
+
sinks.forEach((x: true) => {
+
fn(x);
+
})(operators.filter((x): x is true => !!x)(source));
next(false);
expect(fn).not.toHaveBeenCalled();
+7 -3
src/operators.ts
···
-
import { Source, Sink, Operator, SignalKind, TalkbackKind, TalkbackFn } from './types';
import { push, start, talkbackPlaceholder } from './helpers';
import { fromArray } from './sources';
···
* );
* ```
*/
-
export function filter<T>(predicate: (value: T) => boolean): Operator<T, T> {
return source => sink => {
let talkback = talkbackPlaceholder;
source(signal => {
···
} else if (!predicate(signal[0])) {
talkback(TalkbackKind.Pull);
} else {
-
sink(signal);
}
});
};
}
/** Maps emitted values using the passed mapping function.
*
···
+
import { Push, Source, Sink, Operator, SignalKind, TalkbackKind, TalkbackFn } from './types';
import { push, start, talkbackPlaceholder } from './helpers';
import { fromArray } from './sources';
···
* );
* ```
*/
+
function filter<In, Out extends In>(predicate: (value: In) => value is Out): Operator<In, Out>;
+
function filter<T>(predicate: (value: T) => boolean): Operator<T, T>;
+
function filter<In, Out>(predicate: (value: In) => boolean): Operator<In, Out> {
return source => sink => {
let talkback = talkbackPlaceholder;
source(signal => {
···
} else if (!predicate(signal[0])) {
talkback(TalkbackKind.Pull);
} else {
+
sink(signal as Push<any>);
}
});
};
}
+
+
export { filter };
/** Maps emitted values using the passed mapping function.
*