Skip to content

Commit

Permalink
🚧 progress: Import existing sources and tests from js-itertools.
Browse files Browse the repository at this point in the history
  • Loading branch information
make-github-pseudonymous-again committed May 1, 2021
1 parent a7ca875 commit 2f13e63
Show file tree
Hide file tree
Showing 9 changed files with 10,106 additions and 10 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
Iterable consumption for JavaScript.
See [docs](https://iterable-iterator.github.io/consume/index.html).

> :building_construction: Caveat emptor! This is work in progress. Code may be
> working. Documentation may be present. Coherence may be. Maybe.
> :warning: Depending on your environment, the code may require
> `regeneratorRuntime` to be defined, for instance by importing
> [regenerator-runtime/runtime](https://www.npmjs.com/package/regenerator-runtime).
```js
import {exhaust} from '@iterable-iterator/consume';
import {map} from '@iterable-iterator/map';
import {range} from '@iterable-iterator/range';
exhaust(map((i) => {console.log(i)}, range(10))); // 0 1 2 ...
```

[![License](https://img.shields.io/github/license/iterable-iterator/consume.svg)](https://raw.githubusercontent.com/iterable-iterator/consume/main/LICENSE)
[![Version](https://img.shields.io/npm/v/@iterable-iterator/consume.svg)](https://www.npmjs.org/package/@iterable-iterator/consume)
[![Tests](https://img.shields.io/github/workflow/status/iterable-iterator/consume/ci:test?event=push&label=tests)](https://github.com/iterable-iterator/consume/actions/workflows/ci:test.yml?query=branch:main)
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@
"@babel/preset-env": "7.14.0",
"@babel/register": "7.13.16",
"@commitlint/cli": "12.1.1",
"@iterable-iterator/iter": "^0.0.2",
"@iterable-iterator/list": "^0.0.2",
"@iterable-iterator/next": "^1.0.0",
"@iterable-iterator/range": "^0.0.1",
"@js-library/commitlint-config": "0.0.4",
"ava": "3.15.0",
"babel-plugin-transform-remove-console": "6.9.4",
Expand Down
11 changes: 11 additions & 0 deletions src/consume.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Consumes a given number of iterations of the input iterator.
*
* @param {Iterator} iterator - The input iterator.
* @param {Number} n - The number of iterations to consume.
*
*/
export default function consume(iterator, n) {
// eslint-disable-next-line no-empty
while (n-- > 0 && !iterator.next().done) {}
}
15 changes: 15 additions & 0 deletions src/exhaust.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Exhausts the input iterator.
*
* @example
* let it = range( 1000 ) ;
* exhaust( it ) ;
* list( it ) ; // returns []
*
* @param {Iterator} iterator - The input iterator.
*
*/
export default function exhaust(iterator) {
// eslint-disable-next-line no-empty,no-unused-vars,prettier/prettier
for (const item of iterator) {}
}
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
const answer = 42;
export default answer;
export {default as consume} from './consume.js';
export {default as exhaust} from './exhaust.js';
5 changes: 0 additions & 5 deletions test/src/api.js

This file was deleted.

15 changes: 15 additions & 0 deletions test/src/consume.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import test from 'ava';

import {range} from '@iterable-iterator/range';
import {list} from '@iterable-iterator/list';
import {consume} from '../../src/index.js';

test('consume', (t) => {
const iterator = range(100);

consume(iterator, 37);

const output = list(iterator);

t.deepEqual(output, list(range(37, 100)));
});
22 changes: 22 additions & 0 deletions test/src/exhaust.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import test from 'ava';

import {iter} from '@iterable-iterator/iter';
import {next} from '@iterable-iterator/next';
import {range} from '@iterable-iterator/range';
import {list} from '@iterable-iterator/list';
import {exhaust} from '../../src/index.js';

test('exhaust', (t) => {
const iterator = iter(range(0, 100, 1));

const head = function* (iterator, n) {
// eslint-disable-next-line no-unused-vars
for (const _ of range(n)) yield next(iterator);
};

exhaust(head(iterator, 37));

const output = list(iterator);

t.deepEqual(output, list(range(37, 100, 1)));
});
10,030 changes: 10,030 additions & 0 deletions yarn.lock

Large diffs are not rendered by default.

0 comments on commit 2f13e63

Please sign in to comment.