Skip to content

Commit

Permalink
Stream.filter() method added and tested. Release v0.3.0 published.
Browse files Browse the repository at this point in the history
  • Loading branch information
Smoren committed Mar 5, 2023
1 parent 80207d9 commit 0c3fc6e
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 2 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ npm i itertools-ts
|---------------------------------------|-------------------------------------------------------------------------------------------|---------------------------------------|
| [`chainWith`](#Chain-With) | Chain iterable source withs given iterables together into a single iteration | `stream.chainWith(...iterables)` |
| [`distinct`](#Distinct-1) | Filter out elements: iterate only unique items | `stream.distinct()` |
| [`filter`](#Filter-1) | Filter for only elements where the predicate function is true | `stream.filter(predicate)` |
| [`flatMap`](#Flat-Map-1) | Map function onto elements and flatten result | `stream.flatMap(mapper)` |
| [`map`](#Map-1) | Map function onto elements | `stream.map(mapper)` |
| [`zipWith`](#Zip-With) | Iterate iterable source with another iterable collections simultaneously | `stream.zipWith(...iterables)` |
Expand Down Expand Up @@ -347,6 +348,24 @@ const stream = Stream.of(input)
// 1, 2, 3, '1', '2', '3'
```

#### Filter
Filter out elements from the stream only keeping elements where there predicate function is true.

```
filter(predicate: (item: unknown) => boolean): Stream
```

```typescript
import { Stream } from "itertools-ts";

const input = [1, -1, 2, -2, 3, -3];

const result = Stream.of(input)
.filter((value) => value > 0)
.toArray();
// 1, 2, 3
```

#### Flat Map
Map a function onto the elements of the stream and flatten the results.

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "itertools-ts",
"version": "0.2.0",
"version": "0.3.0",
"description": "Provides a huge set of functions for working with iterable collections",
"main": "src/index.ts",
"scripts": {
Expand Down
7 changes: 6 additions & 1 deletion src/stream.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { toIterable } from './tools';
import { flatMap, map } from './single';
import { filter, flatMap, map } from './single';
import { chain, zip, zipEqual, zipLongest } from "./multi";
import { distinct } from "./set";

Expand Down Expand Up @@ -34,6 +34,11 @@ export class Stream {
return this;
}

filter(predicate: (item: unknown) => boolean): Stream {
this.data = filter(this.data, predicate);
return this;
}

map(mapper: (datum: unknown) => unknown): Stream {
this.data = map(this.data, mapper);
return this;
Expand Down
100 changes: 100 additions & 0 deletions tests/stream/single.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ describe.each([

function dataProviderForArrays(): Array<unknown> {
return [
[
[],
(iterable: Iterable<unknown>) => Stream.of(iterable)
.filter((value) => (value as number) > 0)
.toArray(),
[],
],
[
[1, -1, 2, -2, 3, -3],
(iterable: Iterable<unknown>) => Stream.of(iterable)
.filter((value) => (value as number) > 0)
.toArray(),
[1, 2, 3],
],
[
[],
() => Stream.ofEmpty()
Expand Down Expand Up @@ -84,6 +98,20 @@ function dataProviderForArrays(): Array<unknown> {

function dataProviderForGenerators(): Array<unknown> {
return [
[
createGeneratorFixture([]),
(iterable: Iterable<unknown>) => Stream.of(iterable)
.filter((value) => (value as number) > 0)
.toArray(),
[],
],
[
createGeneratorFixture([1, -1, 2, -2, 3, -3]),
(iterable: Iterable<unknown>) => Stream.of(iterable)
.filter((value) => (value as number) > 0)
.toArray(),
[1, 2, 3],
],
[
createGeneratorFixture([]),
() => Stream.ofEmpty()
Expand Down Expand Up @@ -145,6 +173,20 @@ function dataProviderForGenerators(): Array<unknown> {

function dataProviderForIterables(): Array<unknown> {
return [
[
createIterableFixture([]),
(iterable: Iterable<unknown>) => Stream.of(iterable)
.filter((value) => (value as number) > 0)
.toArray(),
[],
],
[
createIterableFixture([1, -1, 2, -2, 3, -3]),
(iterable: Iterable<unknown>) => Stream.of(iterable)
.filter((value) => (value as number) > 0)
.toArray(),
[1, 2, 3],
],
[
createIterableFixture([]),
() => Stream.ofEmpty()
Expand Down Expand Up @@ -206,6 +248,20 @@ function dataProviderForIterables(): Array<unknown> {

function dataProviderForIterators(): Array<unknown> {
return [
[
createIteratorFixture([]),
(iterable: Iterable<unknown>) => Stream.of(iterable)
.filter((value) => (value as number) > 0)
.toArray(),
[],
],
[
createIteratorFixture([1, -1, 2, -2, 3, -3]),
(iterable: Iterable<unknown>) => Stream.of(iterable)
.filter((value) => (value as number) > 0)
.toArray(),
[1, 2, 3],
],
[
createIteratorFixture([]),
() => Stream.ofEmpty()
Expand Down Expand Up @@ -267,6 +323,20 @@ function dataProviderForIterators(): Array<unknown> {

function dataProviderForStrings(): Array<unknown> {
return [
[
'',
(iterable: Iterable<unknown>) => Stream.of(iterable)
.filter((value) => parseInt(value as string) > 0)
.toArray(),
[],
],
[
'123456',
(iterable: Iterable<unknown>) => Stream.of(iterable)
.filter((value) => parseInt(value as string) % 2 === 0)
.toArray(),
['2', '4', '6'],
],
[
'',
() => Stream.ofEmpty()
Expand Down Expand Up @@ -314,6 +384,20 @@ function dataProviderForStrings(): Array<unknown> {

function dataProviderForSets(): Array<unknown> {
return [
[
new Set([]),
(iterable: Iterable<unknown>) => Stream.of(iterable)
.filter((value) => (value as number) > 0)
.toArray(),
[],
],
[
new Set([1, -1, 2, -2, 3, -3]),
(iterable: Iterable<unknown>) => Stream.of(iterable)
.filter((value) => (value as number) > 0)
.toArray(),
[1, 2, 3],
],
[
new Set([]),
() => Stream.ofEmpty()
Expand Down Expand Up @@ -375,6 +459,22 @@ function dataProviderForSets(): Array<unknown> {

function dataProviderForMaps(): Array<unknown> {
return [
[
createMapFixture([]),
(iterable: Iterable<unknown>) => Stream.of(iterable)
.map((item) => (item as Array<number>)[1])
.filter((value) => (value as number) > 0)
.toArray(),
[],
],
[
createMapFixture([1, -1, 2, -2, 3, -3]),
(iterable: Iterable<unknown>) => Stream.of(iterable)
.map((item) => (item as Array<number>)[1])
.filter((value) => (value as number) > 0)
.toArray(),
[1, 2, 3],
],
[
createMapFixture([]),
() => Stream.ofEmpty()
Expand Down

0 comments on commit 0c3fc6e

Please sign in to comment.