Skip to content

Commit

Permalink
Use core-js-pure for iterator helpers (#24)
Browse files Browse the repository at this point in the history
* Use core-js-pure

* Update PR number for core-js-pure ports
  • Loading branch information
compulim authored Jun 15, 2024
1 parent e1f1fd3 commit dd552c2
Show file tree
Hide file tree
Showing 93 changed files with 1,271 additions and 742 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added `readableStreamFrom` in PR [#15](https://github.com/compulim/iter-fest/pull/15) and [#22](https://github.com/compulim/iter-fest/pull/22)
- Added `generatorWithLastValue`/`asyncGeneratorWithLastValue` in PR [#17](https://github.com/compulim/iter-fest/pull/17)
- Added `asyncIteratorToAsyncIterable` in PR [#18](https://github.com/compulim/iter-fest/pull/18)
- Added `iteratorDrop`, `iteratorFlatMap`, `iteratorFrom`, `iteratorTake`, `iteratorToArray` in PR [#24](https://github.com/compulim/iter-fest/pull/24)

### Changed

- Following functions will be using ponyfill from `core-js-pure` with types, in PR [#24](https://github.com/compulim/iter-fest/pull/24)
- `iteratorEvery`, `iteratorFilter`, `iteratorFind`, `iteratorForEach`, `iteratorMap`, `iteratorReduce`, `iteratorSome`
- Bumped dependencies, in PR [#7](https://github.com/compulim/iter-fest/pull/7)
- Development dependencies
- [`@babel/preset-env@7.24.6`](https://npmjs.com/package/@babel/preset-env/v/7.24.6)
Expand Down
33 changes: 26 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,22 +224,41 @@ const generator = generatorWithLastValue(
);
```

## Adding types to `core-js-pure`

We added types to implementation from `core-js-pure`:

- [`Iterator.drop`](https://tc39.es/ecma262/#sec-array.prototype.drop)
- [`Iterator.every`](https://tc39.es/ecma262/#sec-array.prototype.every)
- [`Iterator.filter`](https://tc39.es/ecma262/#sec-array.prototype.filter)
- [`Iterator.find`](https://tc39.es/ecma262/#sec-array.prototype.find)
- [`Iterator.flatMap`](https://tc39.es/ecma262/#sec-array.prototype.flatmap)
- [`Iterator.forEach`](https://tc39.es/ecma262/#sec-array.prototype.foreach)
- [`Iterator.from`](https://tc39.es/ecma262/#sec-array.prototype.from)
- [`Iterator.map`](https://tc39.es/ecma262/#sec-array.prototype.map)
- [`Iterator.reduce`](https://tc39.es/ecma262/#sec-array.prototype.reduce)
- [`Iterator.some`](https://tc39.es/ecma262/#sec-array.prototype.some)
- [`Iterator.take`](https://tc39.es/ecma262/#sec-array.prototype.take)
- [`Iterator.toArray`](https://tc39.es/ecma262/#sec-array.prototype.toarray)

## `Array.prototype` ports

> Note: we are working to move them from `iterable*` to `iterator*` to make them similar to TC39 proposals.
We ported majority of functions from `Array.prototype.*` to `iterable*`.

```ts
import { iterableIncludes, iterableReduce } from 'iter-fest'; // Via default exports.
import { iterableSome } from 'iter-fest/iterableSome'; // Via named exports.
import { iteratorIncludes, iteratorReduce } from 'iter-fest'; // Via default exports.
import { iteratorSome } from 'iter-fest/iteratorSome'; // Via named exports.

const iterable: Iterable<number> = [1, 2, 3, 4, 5].values();
const iterator: iterator<number> = [1, 2, 3, 4, 5].values();

console.log(iterableIncludes(iterable, 3)); // Prints "true".
console.log(iterableReduce(iterable, (sum, value) => sum + value, 0)); // Prints "15".
console.log(iterableSome(iterable, value => value % 2)); // Prints "true".
console.log(iteratorIncludes(iterator, 3)); // Prints "true".
console.log(iteratorReduce(iterator, (sum, value) => sum + value, 0)); // Prints "15".
console.log(iteratorSome(iterator, value => value % 2)); // Prints "true".
```

List of ported functions: [`at`](https://tc39.es/ecma262/#sec-array.prototype.at), [`concat`](https://tc39.es/ecma262/#sec-array.prototype.concat), [`entries`](https://tc39.es/ecma262/#sec-array.prototype.entries), [`every`](https://tc39.es/ecma262/#sec-array.prototype.every), [`filter`](https://tc39.es/ecma262/#sec-array.prototype.filter), [`find`](https://tc39.es/ecma262/#sec-array.prototype.find), [`findIndex`](https://tc39.es/ecma262/#sec-array.prototype.findindex), [`findLast`](https://tc39.es/ecma262/#sec-array.prototype.findlast), [`findLastIndex`](https://tc39.es/ecma262/#sec-array.prototype.findlastindex), [`forEach`](https://tc39.es/ecma262/#sec-array.prototype.foreach), [`includes`](https://tc39.es/ecma262/#sec-array.prototype.includes), [`indexOf`](https://tc39.es/ecma262/#sec-array.prototype.indexof), [`join`](https://tc39.es/ecma262/#sec-array.prototype.join), [`keys`](https://tc39.es/ecma262/#sec-array.prototype.keys), [`map`](https://tc39.es/ecma262/#sec-array.prototype.map), [`reduce`](https://tc39.es/ecma262/#sec-array.prototype.reduce), [`slice`](https://tc39.es/ecma262/#sec-array.prototype.slice), [`some`](https://tc39.es/ecma262/#sec-array.prototype.some), [`toSpliced`](https://tc39.es/ecma262/#sec-array.prototype.tospliced), and [`toString`](https://tc39.es/ecma262/#sec-array.prototype.tostring).
List of ported functions: [`at`](https://tc39.es/ecma262/#sec-array.prototype.at), [`concat`](https://tc39.es/ecma262/#sec-array.prototype.concat), [`entries`](https://tc39.es/ecma262/#sec-array.prototype.entries), [`findIndex`](https://tc39.es/ecma262/#sec-array.prototype.findindex), [`findLast`](https://tc39.es/ecma262/#sec-array.prototype.findlast), [`findLastIndex`](https://tc39.es/ecma262/#sec-array.prototype.findlastindex), [`includes`](https://tc39.es/ecma262/#sec-array.prototype.includes), [`indexOf`](https://tc39.es/ecma262/#sec-array.prototype.indexof), [`join`](https://tc39.es/ecma262/#sec-array.prototype.join), [`keys`](https://tc39.es/ecma262/#sec-array.prototype.keys), [`slice`](https://tc39.es/ecma262/#sec-array.prototype.slice), [`toSpliced`](https://tc39.es/ecma262/#sec-array.prototype.tospliced), and [`toString`](https://tc39.es/ecma262/#sec-array.prototype.tostring).

## Behaviors

Expand Down
30 changes: 0 additions & 30 deletions packages/integration-test/importDefault.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,14 @@ import {
iterableAt,
iterableConcat,
iterableEntries,
iterableEvery,
iterableFilter,
iterableFind,
iterableFindIndex,
iterableFindLast,
iterableFindLastIndex,
iterableForEach,
iterableIncludes,
iterableIndexOf,
iterableJoin,
iterableKeys,
iterableMap,
iterableReduce,
iterableSlice,
iterableSome,
iterableToSpliced,
iterableToString,
iteratorToIterable,
Expand Down Expand Up @@ -97,27 +90,12 @@ test('iterableEntries should work', () =>
[2, 'C']
]));

test('iterableEvery should work', () => expect(iterableEvery([1, 2, 3].values(), value => value)).toBe(true));

test('iterableFilter should work', () =>
expect(Array.from(iterableFilter([1, 2, 3], value => value % 2))).toEqual([1, 3]));

test('iterableFind should work', () => expect(iterableFind([1, 2, 3], value => value % 2)).toBe(1));

test('iterableFindIndex should work', () => expect(iterableFindIndex([1, 2, 3], value => value % 2)).toBe(0));

test('iterableFindLast should work', () => expect(iterableFindLast([1, 2, 3], value => value % 2)).toBe(3));

test('iterableFindLastIndex should work', () => expect(iterableFindLastIndex([1, 2, 3], value => value % 2)).toBe(2));

test('iterableForEach should work', () => {
const callbackfn = jest.fn();

iterableForEach([1, 2, 3], callbackfn);

expect(callbackfn).toHaveBeenCalledTimes(3);
});

test('iterableIncludes should work', () => expect(iterableIncludes([1, 2, 3], 2)).toBe(true));

test('iterableIndexOf should work', () => expect(iterableIndexOf([1, 2, 3], 2)).toBe(1));
Expand All @@ -126,16 +104,8 @@ test('iterableJoin should work', () => expect(iterableJoin([1, 2, 3], ', ')).toB

test('iterableKeys should work', () => expect(Array.from(iterableKeys(['A', 'B', 'C']))).toEqual([0, 1, 2]));

test('iterableMap should work', () =>
expect(Array.from(iterableMap([1, 2, 3], value => String.fromCharCode(value + 64)))).toEqual(['A', 'B', 'C']));

test('iterableReduce should work', () =>
expect(iterableReduce([1, 2, 3].values(), (previousValue, currentValue) => previousValue + currentValue, 0)).toBe(6));

test('iterableSlice should work', () => expect(Array.from(iterableSlice([1, 2, 3, 4, 5], 1, 4))).toEqual([2, 3, 4]));

test('iterableSome should work', () => expect(iterableSome([1, 2, 3].values(), value => value % 2)).toBe(true));

test('iterableToSpliced should work', () =>
expect(Array.from(iterableToSpliced([1, 2, 3].values(), 1, 1, 9))).toEqual([1, 9, 3]));

Expand Down
20 changes: 20 additions & 0 deletions packages/integration-test/importDefault/iteratorDrop.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { iteratorDrop } from 'iter-fest';

test('iteratorDrop should work', () => {
// Copied from https://github.com/tc39/proposal-iterator-helpers.
function* naturals() {
let i = 0;

while (true) {
yield i;

i += 1;
}
}

const result = iteratorDrop(naturals(), 3);

expect(result.next()).toEqual({ done: false, value: 3 });
expect(result.next()).toEqual({ done: false, value: 4 });
expect(result.next()).toEqual({ done: false, value: 5 });
});
3 changes: 3 additions & 0 deletions packages/integration-test/importDefault/iteratorEvery.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { iteratorEvery } from 'iter-fest';

test('iteratorEvery should work', () => expect(iteratorEvery([1, 2, 3].values(), value => value)).toBe(true));
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { iteratorFilter } from 'iter-fest';

test('iteratorFilter should work', () =>
expect(Array.from(iteratorFilter([1, 2, 3].values(), value => value % 2))).toEqual([1, 3]));
3 changes: 3 additions & 0 deletions packages/integration-test/importDefault/iteratorFind.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { iteratorFind } from 'iter-fest';

test('iteratorFind should work', () => expect(iteratorFind([1, 2, 3].values(), value => value % 2)).toBe(1));
15 changes: 15 additions & 0 deletions packages/integration-test/importDefault/iteratorFlatMap.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { iteratorFlatMap } from 'iter-fest';

test('iteratorFlatMap should work', () => {
// Copied from https://github.com/tc39/proposal-iterator-helpers.
const sunny = ["It's Sunny in", '', 'California'].values();

const result = iteratorFlatMap(sunny, value => value.split(' ').values());

expect(result.next()).toEqual({ done: false, value: "It's" });
expect(result.next()).toEqual({ done: false, value: 'Sunny' });
expect(result.next()).toEqual({ done: false, value: 'in' });
expect(result.next()).toEqual({ done: false, value: '' });
expect(result.next()).toEqual({ done: false, value: 'California' });
expect(result.next()).toEqual({ done: true, value: undefined });
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { iteratorForEach } from 'iter-fest';

test('iteratorForEach should work', () => {
const callbackfn = jest.fn();

iteratorForEach([1, 2, 3].values(), callbackfn);

expect(callbackfn).toHaveBeenCalledTimes(3);
});
15 changes: 15 additions & 0 deletions packages/integration-test/importDefault/iteratorFrom.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { iteratorFrom } from 'iter-fest';

test('iteratorFrom', () => {
// Copied from https://github.com/tc39/proposal-iterator-helpers.
class Iter {
next() {
return { done: false, value: 1 };
}
}

const iter = new Iter();
const wrapper = iteratorFrom(iter);

expect(wrapper.next()).toEqual({ done: false, value: 1 });
});
8 changes: 8 additions & 0 deletions packages/integration-test/importDefault/iteratorMap.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { iteratorMap } from 'iter-fest';

test('iteratorMap should work', () =>
expect(Array.from(iteratorMap([1, 2, 3].values(), value => String.fromCharCode(value + 64)))).toEqual([
'A',
'B',
'C'
]));
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { iteratorReduce } from 'iter-fest';

test('iteratorReduce should work', () =>
expect(iteratorReduce([1, 2, 3].values(), (previousValue, currentValue) => previousValue + currentValue, 0)).toBe(6));
3 changes: 3 additions & 0 deletions packages/integration-test/importDefault/iteratorSome.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { iteratorSome } from 'iter-fest';

test('iteratorSome should work', () => expect(iteratorSome([1, 2, 3].values(), value => value % 2)).toBe(true));
21 changes: 21 additions & 0 deletions packages/integration-test/importDefault/iteratorTake.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { iteratorTake } from 'iter-fest';

test('iteratorTake should work', () => {
// Copied from https://github.com/tc39/proposal-iterator-helpers.
function* naturals() {
let i = 0;

while (true) {
yield i;

i += 1;
}
}

const result = iteratorTake(naturals(), 3);

expect(result.next()).toEqual({ done: false, value: 0 });
expect(result.next()).toEqual({ done: false, value: 1 });
expect(result.next()).toEqual({ done: false, value: 2 });
expect(result.next()).toEqual({ done: true, value: undefined });
});
16 changes: 16 additions & 0 deletions packages/integration-test/importDefault/iteratorToArray.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { iteratorTake, iteratorToArray } from 'iter-fest';

test('should follow TC39 proposal sample', () => {
// Copied from https://github.com/tc39/proposal-iterator-helpers.
function* naturals() {
let i = 0;
while (true) {
yield i;
i += 1;
}
}

const result = iteratorToArray(iteratorTake(naturals(), 5));

expect(result).toEqual([0, 1, 2, 3, 4]);
});
30 changes: 0 additions & 30 deletions packages/integration-test/importNamed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,14 @@ import { generatorWithLastValue } from 'iter-fest/generatorWithLastValue';
import { iterableAt } from 'iter-fest/iterableAt';
import { iterableConcat } from 'iter-fest/iterableConcat';
import { iterableEntries } from 'iter-fest/iterableEntries';
import { iterableEvery } from 'iter-fest/iterableEvery';
import { iterableFilter } from 'iter-fest/iterableFilter';
import { iterableFind } from 'iter-fest/iterableFind';
import { iterableFindIndex } from 'iter-fest/iterableFindIndex';
import { iterableFindLast } from 'iter-fest/iterableFindLast';
import { iterableFindLastIndex } from 'iter-fest/iterableFindLastIndex';
import { iterableForEach } from 'iter-fest/iterableForEach';
import { iterableIncludes } from 'iter-fest/iterableIncludes';
import { iterableIndexOf } from 'iter-fest/iterableIndexOf';
import { iterableJoin } from 'iter-fest/iterableJoin';
import { iterableKeys } from 'iter-fest/iterableKeys';
import { iterableMap } from 'iter-fest/iterableMap';
import { iterableReduce } from 'iter-fest/iterableReduce';
import { iterableSlice } from 'iter-fest/iterableSlice';
import { iterableSome } from 'iter-fest/iterableSome';
import { iterableToSpliced } from 'iter-fest/iterableToSpliced';
import { iterableToString } from 'iter-fest/iterableToString';
import { IterableWritableStream } from 'iter-fest/iterableWritableStream';
Expand Down Expand Up @@ -95,27 +88,12 @@ test('iterableEntries should work', () =>
[2, 'C']
]));

test('iterableEvery should work', () => expect(iterableEvery([1, 2, 3].values(), value => value)).toBe(true));

test('iterableFilter should work', () =>
expect(Array.from(iterableFilter([1, 2, 3], value => value % 2))).toEqual([1, 3]));

test('iterableFind should work', () => expect(iterableFind([1, 2, 3], value => value % 2)).toBe(1));

test('iterableFindIndex should work', () => expect(iterableFindIndex([1, 2, 3], value => value % 2)).toBe(0));

test('iterableFindLast should work', () => expect(iterableFindLast([1, 2, 3], value => value % 2)).toBe(3));

test('iterableFindLastIndex should work', () => expect(iterableFindLastIndex([1, 2, 3], value => value % 2)).toBe(2));

test('iterableForEach should work', () => {
const callbackfn = jest.fn();

iterableForEach([1, 2, 3], callbackfn);

expect(callbackfn).toHaveBeenCalledTimes(3);
});

test('iterableIncludes should work', () => expect(iterableIncludes([1, 2, 3], 2)).toBe(true));

test('iterableIndexOf should work', () => expect(iterableIndexOf([1, 2, 3], 2)).toBe(1));
Expand All @@ -124,16 +102,8 @@ test('iterableJoin should work', () => expect(iterableJoin([1, 2, 3], ', ')).toB

test('iterableKeys should work', () => expect(Array.from(iterableKeys(['A', 'B', 'C']))).toEqual([0, 1, 2]));

test('iterableMap should work', () =>
expect(Array.from(iterableMap([1, 2, 3], value => String.fromCharCode(value + 64)))).toEqual(['A', 'B', 'C']));

test('iterableReduce should work', () =>
expect(iterableReduce([1, 2, 3].values(), (previousValue, currentValue) => previousValue + currentValue, 0)).toBe(6));

test('iterableSlice should work', () => expect(Array.from(iterableSlice([1, 2, 3, 4, 5], 1, 4))).toEqual([2, 3, 4]));

test('iterableSome should work', () => expect(iterableSome([1, 2, 3].values(), value => value % 2)).toBe(true));

test('iterableToSpliced should work', () =>
expect(Array.from(iterableToSpliced([1, 2, 3].values(), 1, 1, 9))).toEqual([1, 9, 3]));

Expand Down
20 changes: 20 additions & 0 deletions packages/integration-test/importNamed/iteratorDrop.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { iteratorDrop } from 'iter-fest/iteratorDrop';

test('iteratorDrop should work', () => {
// Copied from https://github.com/tc39/proposal-iterator-helpers.
function* naturals() {
let i = 0;

while (true) {
yield i;

i += 1;
}
}

const result = iteratorDrop(naturals(), 3);

expect(result.next()).toEqual({ done: false, value: 3 });
expect(result.next()).toEqual({ done: false, value: 4 });
expect(result.next()).toEqual({ done: false, value: 5 });
});
3 changes: 3 additions & 0 deletions packages/integration-test/importNamed/iteratorEvery.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { iteratorEvery } from 'iter-fest/iteratorEvery';

test('iteratorEvery should work', () => expect(iteratorEvery([1, 2, 3].values(), value => value)).toBe(true));
4 changes: 4 additions & 0 deletions packages/integration-test/importNamed/iteratorFilter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { iteratorFilter } from 'iter-fest/iteratorFilter';

test('iteratorFilter should work', () =>
expect(Array.from(iteratorFilter([1, 2, 3].values(), value => value % 2))).toEqual([1, 3]));
3 changes: 3 additions & 0 deletions packages/integration-test/importNamed/iteratorFind.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { iteratorFind } from 'iter-fest/iteratorFind';

test('iteratorFind should work', () => expect(iteratorFind([1, 2, 3].values(), value => value % 2)).toBe(1));
15 changes: 15 additions & 0 deletions packages/integration-test/importNamed/iteratorFlatMap.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { iteratorFlatMap } from 'iter-fest/iteratorFlatMap';

test('iteratorFlatMap should work', () => {
// Copied from https://github.com/tc39/proposal-iterator-helpers.
const sunny = ["It's Sunny in", '', 'California'].values();

const result = iteratorFlatMap(sunny, value => value.split(' ').values());

expect(result.next()).toEqual({ done: false, value: "It's" });
expect(result.next()).toEqual({ done: false, value: 'Sunny' });
expect(result.next()).toEqual({ done: false, value: 'in' });
expect(result.next()).toEqual({ done: false, value: '' });
expect(result.next()).toEqual({ done: false, value: 'California' });
expect(result.next()).toEqual({ done: true, value: undefined });
});
9 changes: 9 additions & 0 deletions packages/integration-test/importNamed/iteratorForEach.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { iteratorForEach } from 'iter-fest/iteratorForEach';

test('iteratorForEach should work', () => {
const callbackfn = jest.fn();

iteratorForEach([1, 2, 3].values(), callbackfn);

expect(callbackfn).toHaveBeenCalledTimes(3);
});
Loading

0 comments on commit dd552c2

Please sign in to comment.