Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# IterTools Typescript Change Log

## v2.5.0 - 2025-12-31

### New features
* summary
* `isEmpty()`
* `isEmptyAsync()`
* Stream
* `isEmpty()`
* AsyncStream
* `isEmpty()`

## v2.4.1 - 2025-12-30

### Fixes
Expand Down
53 changes: 53 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ Quick Reference
| [`anyMatch`](#any-match) | True if any item is true according to predicate | `summary.anyMatch(data, predicate)` | `summary.anyMatchAsync(data, predicate)` |
| [`exactlyN`](#exactly-n) | True if exactly n items are true according to predicate | `summary.exactlyN(data, n, predicate)` | `summary.exactlyNAsync(data, n, predicate)` |
| [`isAsyncIterable`](#is-async-iterable) | True if given data is async iterable | `summary.isAsyncIterable(data)` | — |
| [`isEmpty`](#is-empty) | True if iterable is empty | `summary.isEmpty(data)` | `summary.isEmptyAsync(data)` |
| [`isIterable`](#is-iterable) | True if given data is iterable | `summary.isIterable(data)` | — |
| [`isIterator`](#is-iterator) | True if given data is iterator | `summary.isIterator(data)` | — |
| [`isReversed`](#is-reversed) | True if iterable reverse sorted | `summary.isReversed(data)` | `summary.isReversedAsync(data)` |
Expand Down Expand Up @@ -341,6 +342,7 @@ Quick Reference
| [`allUnique`](#all-unique-1) | Returns true if all elements of stream are unique | `stream.allUnique(predicate)` |
| [`anyMatch`](#any-match-1) | Returns true if any item in stream matches predicate | `stream.anyMatch(predicate)` |
| [`exactlyN`](#exactly-n-1) | Returns true if exactly n items are true according to predicate | `stream.exactlyN(n, predicate)` |
| [`isEmpty`](#is-empty-1) | Returns true if stream is empty | `stream.isEmpty()` |
| [`isReversed`](#is-reversed-1) | Returns true if stream is sorted in reverse descending order | `stream.isReversed()` |
| [`isSorted`](#is-sorted-1) | Returns true if stream is sorted in ascending order | `stream.isSorted()` |
| [`noneMatch`](#none-match-1) | Returns true if none of the items in stream match predicate | `stream.noneMatch(predicate)` |
Expand Down Expand Up @@ -1945,6 +1947,30 @@ Summary.isReversed(numbers);
// false
```

### Is Empty
Returns true if there are no elements, otherwise false.

```
function isEmpty(data: Iterable<unknown> | Iterator<unknown>): boolean
```

- Returns true if empty.
- Returns false if one or more elements.

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

const emptyArray = [];

Summary.isEmpty(emptyArray);
// true

const numbers = [3];

Summary.isEmpty(numbers);
// false
```

### Is Sorted
Returns true if elements are sorted, otherwise false.

Expand Down Expand Up @@ -3618,6 +3644,33 @@ Stream.of(input)
// false
```

##### Is Empty
Returns true if iterable source is empty, otherwise false.

```
Stream<T>.isEmpty(): boolean
```

Returns true if iterable source is empty.

Returns false if iterable source has one or more elements.

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

const emptyArray = [];

Stream.of(emptyArray)
.isEmpty();
// true

const input = [1];

Stream.of(isEmpty)
.isEmpty();
// false
```

##### Is Sorted
Returns true if iterable source is sorted in ascending order; otherwise false.

Expand Down
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@smoren/itertools-ts",
"version": "2.4.1",
"version": "2.5.0",
"license": "MIT",
"exports": "./src/index.ts"
}
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": "2.4.1",
"version": "2.5.0",
"description": "Extended itertools port for TypeScript and JavaScript. Provides a huge set of functions for working with iterable collections (including async ones)",
"license": "MIT",
"repository": {
Expand Down
9 changes: 9 additions & 0 deletions src/async-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,15 @@ export class AsyncStream<T> implements AsyncIterable<T> {
return summary.exactlyNAsync(this, n, predicate);
}

/**
* Returns true if given stream is empty.
*
* @see summary.isEmptyAsync
*/
async isEmpty(): Promise<boolean> {
return summary.isEmptyAsync(this);
}

/**
* Returns true if stream is sorted in ascending order; otherwise false.
*
Expand Down
10 changes: 10 additions & 0 deletions src/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,16 @@ export class Stream<T> implements Iterable<T> {
exactlyN(n: number, predicate?: (item: T) => boolean): boolean {
return summary.exactlyN(this, n, predicate);
}

/**
* Returns true if given stream is empty.
*
* @see summary.isEmpty
*/
isEmpty(): boolean {
return summary.isEmpty(this);
}

/**
* Returns true if stream is sorted in ascending order; otherwise false.
*
Expand Down
50 changes: 50 additions & 0 deletions tests/async-stream/summary.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ function dataProviderForArraysTrue(): Array<[Array<any>, (iterable: Array<any>)
(iterable: Iterable<unknown | Iterator<unknown>>) => AsyncStream.of(iterable)
.exactlyN(3),
],
[
[],
(iterable: Iterable<number> | Iterator<number>) => AsyncStream.of(iterable)
.isEmpty(),
],
[
[1, -1, 2, -2, 3, -3],
(iterable: Iterable<number> | Iterator<number>) => AsyncStream.of(iterable)
Expand Down Expand Up @@ -232,6 +237,11 @@ function dataProviderForStringsTrue(): Array<[string, (iterable: string) => Prom
(iterable) => AsyncStream.of(iterable)
.exactlyN(0),
],
[
'',
(iterable) => AsyncStream.of(iterable)
.isEmpty(),
],
[
'123',
(iterable) => AsyncStream.of(iterable)
Expand Down Expand Up @@ -355,6 +365,11 @@ function dataProviderForSetsTrue(): Array<[Set<any>, (iterable: Set<any>) => Pro
(iterable: Set<unknown>) => AsyncStream.of(iterable)
.exactlyN(3),
],
[
new Set([]),
(iterable: Set<number>) => AsyncStream.of(iterable)
.isEmpty(),
],
[
new Set([1, -1, 2, -2, 3, -3]),
(iterable: Set<number>) => AsyncStream.of(iterable)
Expand Down Expand Up @@ -458,6 +473,11 @@ function dataProviderForMapsTrue(): Array<[Map<any, any>, (iterable: Map<any, an
(iterable: Map<unknown, number>) => AsyncStream.of(iterable)
.anyMatch((x) => x[1] > 0),
],
[
createMapFixture([]),
(iterable: Map<unknown, number>) => AsyncStream.of(iterable)
.isEmpty(),
],
[
createMapFixture([1, -1, 2, -2, 3, -3]),
(iterable: Map<unknown, number>) => AsyncStream.of(iterable)
Expand Down Expand Up @@ -582,6 +602,11 @@ function dataProviderForAsyncTrue(): Array<[Array<any>, (iterable: AsyncIterable
(iterable: AsyncIterable<unknown> | AsyncIterator<unknown>) => AsyncStream.of(iterable)
.exactlyN(3),
],
[
[],
(iterable: AsyncIterable<number> | AsyncIterator<number>) => AsyncStream.of(iterable)
.isEmpty(),
],
[
[1, -1, 2, -2, 3, -3],
(iterable: AsyncIterable<number> | AsyncIterator<number>) => AsyncStream.of(iterable)
Expand Down Expand Up @@ -706,6 +731,11 @@ function dataProviderForArraysFalse(): Array<[Array<any>, (iterable: Array<any>)
(iterable: Iterable<unknown>) => AsyncStream.of(iterable)
.exactlyN(4),
],
[
[1],
(iterable: Iterable<number>) => AsyncStream.of(iterable)
.isEmpty(),
],
[
[1, -1, 2, -2, 3, -3],
(iterable: Iterable<number>) => AsyncStream.of(iterable)
Expand Down Expand Up @@ -805,6 +835,11 @@ function dataProviderForStringsFalse(): Array<[string, (iterable: string) => Pro
(iterable) => AsyncStream.of(iterable)
.exactlyN(1),
],
[
'1',
(iterable) => AsyncStream.of(iterable)
.isEmpty(),
],
[
'131',
(iterable) => AsyncStream.of(iterable)
Expand Down Expand Up @@ -892,6 +927,11 @@ function dataProviderForSetsFalse(): Array<[Set<any>, (iterable: Set<any>) => Pr
(iterable: Set<number>) => AsyncStream.of(iterable)
.exactlyN(4),
],
[
new Set([1]),
(iterable: Set<number>) => AsyncStream.of(iterable)
.isEmpty(),
],
[
new Set([1, -1, 2, -2, 3, -3]),
(iterable: Set<number>) => AsyncStream.of(iterable)
Expand Down Expand Up @@ -966,6 +1006,11 @@ function dataProviderForMapsFalse(): Array<[Map<any, any>, (iterable: Map<any, a
(iterable: Map<unknown, number>) => AsyncStream.of(iterable)
.anyMatch((x) => x[1] > 10),
],
[
createMapFixture([1]),
(iterable: Map<unknown, number>) => AsyncStream.of(iterable)
.isEmpty(),
],
[
createMapFixture([1, -1, 2, -2, 3, -3]),
(iterable: Map<unknown, number>) => AsyncStream.of(iterable)
Expand Down Expand Up @@ -1066,6 +1111,11 @@ function dataProviderForAsyncFalse(): Array<[Array<any>, (iterable: AsyncIterabl
(iterable: AsyncIterable<unknown> | AsyncIterator<unknown>) => AsyncStream.of(iterable)
.exactlyN(4),
],
[
[1],
(iterable: AsyncIterable<number> | AsyncIterator<number>) => AsyncStream.of(iterable)
.isEmpty(),
],
[
[1, -1, 2, -2, 3, -3],
(iterable: AsyncIterable<number> | AsyncIterator<number>) => AsyncStream.of(iterable)
Expand Down
40 changes: 40 additions & 0 deletions tests/stream/summary.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ function dataProviderForArraysTrue(): Array<[Array<any>, (iterable: Array<any>)
(iterable: Iterable<unknown | Iterator<unknown>>) => Stream.of(iterable)
.exactlyN(3),
],
[
[],
(iterable: Iterable<number> | Iterator<number>) => Stream.of(iterable)
.isEmpty(),
],
[
[1, -1, 2, -2, 3, -3],
(iterable: Iterable<number> | Iterator<number>) => Stream.of(iterable)
Expand Down Expand Up @@ -218,6 +223,11 @@ function dataProviderForStringsTrue(): Array<[string, (iterable: string) => bool
(iterable) => Stream.of(iterable)
.exactlyN(0),
],
[
'',
(iterable) => Stream.of(iterable)
.isEmpty(),
],
[
'123',
(iterable) => Stream.of(iterable)
Expand Down Expand Up @@ -341,6 +351,11 @@ function dataProviderForSetsTrue(): Array<[Set<any>, (iterable: Set<any>) => boo
(iterable: Set<unknown>) => Stream.of(iterable)
.exactlyN(3),
],
[
new Set([]),
(iterable: Set<number>) => Stream.of(iterable)
.isEmpty(),
],
[
new Set([1, -1, 2, -2, 3, -3]),
(iterable: Set<number>) => Stream.of(iterable)
Expand Down Expand Up @@ -444,6 +459,11 @@ function dataProviderForMapsTrue(): Array<[Map<any, any>, (iterable: Map<any, an
(iterable: Map<unknown, number>) => Stream.of(iterable)
.anyMatch((x) => x[1] > 0),
],
[
createMapFixture([]),
(iterable: Map<unknown, number>) => Stream.of(iterable)
.isEmpty(),
],
[
createMapFixture([1, -1, 2, -2, 3, -3]),
(iterable: Map<unknown, number>) => Stream.of(iterable)
Expand Down Expand Up @@ -547,6 +567,11 @@ function dataProviderForArraysFalse(): Array<[Array<any>, (iterable: Array<any>)
(iterable: Iterable<unknown>) => Stream.of(iterable)
.exactlyN(4),
],
[
[1],
(iterable: Iterable<number>) => Stream.of(iterable)
.isEmpty(),
],
[
[1, -1, 2, -2, 3, -3],
(iterable: Iterable<number>) => Stream.of(iterable)
Expand Down Expand Up @@ -646,6 +671,11 @@ function dataProviderForStringsFalse(): Array<[string, (iterable: string) => boo
(iterable) => Stream.of(iterable)
.exactlyN(1),
],
[
'1',
(iterable) => Stream.of(iterable)
.isEmpty(),
],
[
'131',
(iterable) => Stream.of(iterable)
Expand Down Expand Up @@ -733,6 +763,11 @@ function dataProviderForSetsFalse(): Array<[Set<any>, (iterable: Set<any>) => bo
(iterable: Set<number>) => Stream.of(iterable)
.exactlyN(4),
],
[
new Set([1]),
(iterable: Set<number>) => Stream.of(iterable)
.isEmpty(),
],
[
new Set([1, -1, 2, -2, 3, -3]),
(iterable: Set<number>) => Stream.of(iterable)
Expand Down Expand Up @@ -807,6 +842,11 @@ function dataProviderForMapsFalse(): Array<[Map<any, any>, (iterable: Map<any, a
(iterable: Map<unknown, number>) => Stream.of(iterable)
.anyMatch((x) => x[1] > 10),
],
[
createMapFixture([1]),
(iterable: Map<unknown, number>) => Stream.of(iterable)
.isEmpty(),
],
[
createMapFixture([1, -1, 2, -2, 3, -3]),
(iterable: Map<unknown, number>) => Stream.of(iterable)
Expand Down