From c0ffee8858dac56a99a120b3ed5a88d8bd52f03c Mon Sep 17 00:00:00 2001 From: Hendrik Sollich Date: Tue, 24 Dec 2024 21:26:34 +0100 Subject: [PATCH] fix: update examples in README --- CHANGELOG.md | 20 +++---- README.md | 142 ++++++++++++++++++++++++++++++++++++++++--------- bumpversion.ts | 3 +- 3 files changed, 130 insertions(+), 35 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dd64275..980c8ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,31 +4,31 @@ #### Fixes -* add docs for .chain() -([7c6d89f](https://github.com/hoodie/iterators.ts/commit/7c6d89f75ae386b37ee8514583507f8864073f40)) +- add docs for .chain() + ([7c6d89f](https://github.com/hoodie/iterators.ts/commit/7c6d89f75ae386b37ee8514583507f8864073f40)) ### [v0.1.2](https://github.com/hoodie/iterators.ts/compare/v0.1.1...v0.1.2) (2024-12-24) #### Features -* add .chain() -([f4b2cb7](https://github.com/hoodie/iterators.ts/commit/f4b2cb729ca607d0ac6c60d150d1dab889e5a07f)) +- add .chain() + ([f4b2cb7](https://github.com/hoodie/iterators.ts/commit/f4b2cb729ca607d0ac6c60d150d1dab889e5a07f)) ### [v0.1.1](https://github.com/hoodie/iterators.ts/compare/v0.1.0...v0.1.1) (2024-12-17) #### Fixes -* add more documentation -([903fef4](https://github.com/hoodie/iterators.ts/commit/903fef446935a417f23d4a441d6d3fbb0849a662)) +- add more documentation + ([903fef4](https://github.com/hoodie/iterators.ts/commit/903fef446935a417f23d4a441d6d3fbb0849a662)) ## v0.1.0 (2024-12-17) ### Features -* port to deno module -([cdf631e](https://github.com/hoodie/iterators.ts/commit/cdf631e802346065d542d6facf8132b35cce533a)) +- port to deno module + ([cdf631e](https://github.com/hoodie/iterators.ts/commit/cdf631e802346065d542d6facf8132b35cce533a)) ### Fixes -* map must obey {done} -([2bf7b3d](https://github.com/hoodie/iterators.ts/commit/2bf7b3d03ed7e085da2260cb3c69e1ac83db1806)) +- map must obey {done} + ([2bf7b3d](https://github.com/hoodie/iterators.ts/commit/2bf7b3d03ed7e085da2260cb3c69e1ac83db1806)) diff --git a/README.md b/README.md index e7870ab..6db4809 100644 --- a/README.md +++ b/README.md @@ -1,32 +1,126 @@ -# Lazy Iterators in Typescript - -Javascript iterators suck so... +# Lazy Iterators ```typescript -const it = Iter.fromArray([ - "a", - "b", - "c", - "d", - "e", - "f", - "g", - "h", - "i", - "j", - "k", - "l", +const result = Iter.fromArray([ + "apple", + "banana", + "cherry", + "date", + "elderberry", ]) .enumerate() - .map(([k, v]) => [k, v.toUpperCase()]) - .filter(([k, v]) => k % 2 == 0) - .map(([, v]) => v); + .map(([index, value]) => [index, value.toUpperCase()]) + .filter(([index, value]) => index % 2 === 0) + .take(2) + .intoArray(); +``` + +This is now published under . + +## Examples -const collected = it.take(3) - .intoArray(); // -> [ 'A', 'C', 'E'] +### Basic Usage -const collected2 = it - .intoArray(); // -> ['G', 'I', 'K'] +Create an iterator from an array and collect all elements into an array. + +```ts +const array = [1, 2, 3, 4, 5]; +const iterator = Iter.fromArray(array); +const result = iterator.intoArray(); +console.log(result); // [1, 2, 3, 4, 5] ``` -This is now published under . +### Enumerate + +Create an iterator which yields the current index and the value of the original +iterator. + +```ts +const array = ["a", "b", "c"]; +const iterator = Iter.fromArray(array).enumerate(); +const result = iterator.intoArray(); +console.log(result); // [[0, 'a'], [1, 'b'], [2, 'c']] +``` + +### Filter + +Create an iterator skipping all elements which don't meet the given predicate. + +```ts +const array = [1, 2, 3, 4, 5]; +const iterator = Iter.fromArray(array).filter((x) => x % 2 === 0); +const result = iterator.intoArray(); +console.log(result); // [2, 4] +``` + +### Find + +Find the first element that satisfies the predicate. + +```ts +const array = [1, 2, 3, 4, 5]; +const iterator = Iter.fromArray(array); +const result = iterator.find((x) => x > 3); +console.log(result); // 4 +``` + +### Map + +Create an iterator applying the given callback to each element. + +```ts +const array = [1, 2, 3]; +const iterator = Iter.fromArray(array).map((x) => x * 2); +const result = iterator.intoArray(); +console.log(result); // [2, 4, 6] +``` + +### Skip + +Create an iterator skipping the first `limit` elements. + +```ts +const array = [1, 2, 3, 4, 5]; +const iterator = Iter.fromArray(array).skip(2); +const result = iterator.intoArray(); +console.log(result); // [3, 4, 5] +``` + +### Take + +Create an iterator yielding the first `limit` elements. + +```ts +const array = [1, 2, 3, 4, 5]; +const iterator = Iter.fromArray(array).take(3); +const result = iterator.intoArray(); +console.log(result); // [1, 2, 3] +``` + +### Zip + +Create an iterator that ‘Zips up’ two iterators into a single iterator of pairs. + +```ts +const array1 = [1, 2, 3]; +const array2 = ["a", "b", "c"]; +const iterator1 = Iter.fromArray(array1); +const iterator2 = Iter.fromArray(array2); +const zipped = iterator1.zip(iterator2); +const result = zipped.intoArray(); +console.log(result); // [[1, 'a'], [2, 'b'], [3, 'c']] +``` + +### Cycle + +Create an iterator that cycles through the elements of the original iterator. + +```ts +const array = [1, 2, 3]; +const iterator = Iter.fromArray(array).cycle(); +const result = []; +for (let i = 0; i < 10; i++) { + result.push(iterator.next().value); +} +console.log(result); // [1, 2, 3, 1, 2, 3, 1, 2, 3, 1] +``` diff --git a/bumpversion.ts b/bumpversion.ts index fb0aeeb..83c2768 100755 --- a/bumpversion.ts +++ b/bumpversion.ts @@ -43,9 +43,10 @@ if (import.meta.main) { await git("add", "deno.json", "deno.lock"); await git("commit", "-m", "chore: bump version"); - await git("tag", `v${next_version}`); + await convco("changelog", "-o", "CHANGELOG.md"); + await run("deno", { args: ["fmt"] }); await git("add", "CHANGELOG.md"); await git("commit", "-m", "chore: changelog");