Skip to content

Commit

Permalink
filter support for inferred type predicates
Browse files Browse the repository at this point in the history
  • Loading branch information
carlansley committed Oct 23, 2024
1 parent 793f171 commit a7f09e4
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"debug": "^4.3.7"
},
"devDependencies": {
"@checkdigit/eslint-config": "^10.0.1",
"@checkdigit/eslint-config": "^10.1.0",
"@checkdigit/jest-config": "^6.0.2",
"@checkdigit/prettier-config": "^5.5.1",
"@checkdigit/typescript-config": "^8.0.0",
Expand Down
15 changes: 14 additions & 1 deletion src/operator/filter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { strict as assert } from 'node:assert';

import { describe, it } from '@jest/globals';

import { all, filter, from, pipeline, toArray } from '../index';
import { all, filter, from, map, pipeline, toArray } from '../index';

describe('filter', () => {
it('works for an empty array', async () => {
Expand Down Expand Up @@ -52,6 +52,19 @@ describe('filter', () => {
);
});

it('supports inferred type predicates', async () => {
const iterable = from(['a', 'bb', undefined, 'ccc']);
assert.deepEqual(
await pipeline(
iterable,
filter((value) => value !== undefined),
map((value) => value.length),
toArray,
),
[1, 2, 3],
);
});

it('is chain-able', async () => {
const iterable = from(['a', 'bb', 'ccc']);
assert.deepEqual(
Expand Down
5 changes: 5 additions & 0 deletions src/operator/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import type { Asyncerator } from '../asyncerator';

import type { Operator } from './index';

export default function <Input, Output extends Input>(
predicate: (value: Input, index: number) => value is Output,
): Operator<Input, Output>;
export default function <Input>(filterFunction: (value: Input, index: number) => boolean): Operator<Input, Input>;

/**
* Similar to `Array.filter`, only emit values from input for which filterFunction returns true.
* @param filterFunction
Expand Down

0 comments on commit a7f09e4

Please sign in to comment.