Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial implementation, tests, readme
- Loading branch information
Showing
14 changed files
with
291 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"root": true, | ||
|
||
"extends": "@ljharb", | ||
|
||
"globals": { | ||
"StopIteration": false, | ||
}, | ||
|
||
"rules": { | ||
"func-name-matching": [2, "always"], | ||
"id-length": 0, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# These are supported funding model platforms | ||
|
||
github: [ljharb] | ||
patreon: # Replace with a single Patreon username | ||
open_collective: # Replace with a single Open Collective username | ||
ko_fi: # Replace with a single Ko-fi username | ||
tidelift: npm/stop-iteration-iterator | ||
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry | ||
liberapay: # Replace with a single Liberapay username | ||
issuehunt: # Replace with a single IssueHunt username | ||
otechie: # Replace with a single Otechie username | ||
custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
name: 'Tests: node.js < 10' | ||
|
||
on: [pull_request, push] | ||
|
||
jobs: | ||
tests: | ||
uses: ljharb/actions/.github/workflows/node.yml@main | ||
with: | ||
range: '< 10' | ||
type: minors | ||
command: npm run tests-only | ||
|
||
node: | ||
name: 'node < 10' | ||
needs: [tests] | ||
runs-on: ubuntu-latest | ||
steps: | ||
- run: 'echo tests completed' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
name: 'Tests: pretest/posttest' | ||
|
||
on: [pull_request, push] | ||
|
||
jobs: | ||
tests: | ||
uses: ljharb/actions/.github/workflows/pretest.yml@main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
name: 'Tests: node.js >= 10' | ||
|
||
on: [pull_request, push] | ||
|
||
jobs: | ||
tests: | ||
uses: ljharb/actions/.github/workflows/node.yml@main | ||
with: | ||
range: '>= 10' | ||
type: minors | ||
command: npm run tests-only | ||
|
||
node: | ||
name: 'node >= 10' | ||
needs: [tests] | ||
runs-on: ubuntu-latest | ||
steps: | ||
- run: 'echo tests completed' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
name: Automatic Rebase | ||
|
||
on: [pull_request_target] | ||
|
||
permissions: | ||
contents: read | ||
|
||
jobs: | ||
_: | ||
permissions: | ||
contents: write # for ljharb/rebase to push code to rebase | ||
pull-requests: read # for ljharb/rebase to get info about PR | ||
|
||
name: "Automatic Rebase" | ||
|
||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: ljharb/rebase@master | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
name: Require “Allow Edits” | ||
|
||
on: [pull_request_target] | ||
|
||
jobs: | ||
_: | ||
name: "Require “Allow Edits”" | ||
|
||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: ljharb/require-allow-edits@main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -107,3 +107,5 @@ dist | |
npm-shrinkwrap.json | ||
package-lock.json | ||
yarn.lock | ||
|
||
.npmignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
package-lock=false | ||
allow-same-version=true | ||
message=v%s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"all": true, | ||
"check-coverage": false, | ||
"reporter": ["text-summary", "text", "html", "json"], | ||
"lines": 86, | ||
"statements": 85.93, | ||
"functions": 82.43, | ||
"branches": 76.06, | ||
"exclude": [ | ||
"coverage", | ||
"test" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,42 @@ | ||
# stop-iteration-iterator | ||
# stop-iteration-iterator <sup>[![Version Badge][npm-version-svg]][package-url]</sup> | ||
|
||
[![github actions][actions-image]][actions-url] | ||
[![coverage][codecov-image]][codecov-url] | ||
[![License][license-image]][license-url] | ||
[![Downloads][downloads-image]][downloads-url] | ||
|
||
[![npm badge][npm-badge-png]][package-url] | ||
|
||
Firefox 17-26 iterators throw a StopIteration object to indicate "done". This normalizes it. | ||
|
||
# Usage | ||
|
||
```js | ||
var stopIterationIterator = require('stop-iteration-iterator'); | ||
|
||
var s = new Set([1, 2]); | ||
|
||
var iterator = stopIterationIterator(s.keys()); | ||
|
||
iterator.next(); // { done: false, value: 1 } | ||
``` | ||
|
||
## Tests | ||
Simply clone the repo, `npm install`, and run `npm test` | ||
|
||
|
||
[package-url]: https://npmjs.org/package/stop-iteration-iterator | ||
[npm-version-svg]: https://versionbadg.es/ljharb/stop-iteration-iterator.svg | ||
[deps-svg]: https://david-dm.org/ljharb/stop-iteration-iterator.svg | ||
[deps-url]: https://david-dm.org/ljharb/stop-iteration-iterator | ||
[dev-deps-svg]: https://david-dm.org/ljharb/stop-iteration-iterator/dev-status.svg | ||
[dev-deps-url]: https://david-dm.org/ljharb/stop-iteration-iterator#info=devDependencies | ||
[npm-badge-png]: https://nodei.co/npm/stop-iteration-iterator.png?downloads=true&stars=true | ||
[license-image]: https://img.shields.io/npm/l/stop-iteration-iterator.svg | ||
[license-url]: LICENSE | ||
[downloads-image]: https://img.shields.io/npm/dm/stop-iteration-iterator.svg | ||
[downloads-url]: https://npm-stat.com/charts.html?package=stop-iteration-iterator | ||
[codecov-image]: https://codecov.io/gh/ljharb/stop-iteration-iterator/branch/main/graphs/badge.svg | ||
[codecov-url]: https://app.codecov.io/gh/ljharb/stop-iteration-iterator/ | ||
[actions-image]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/ljharb/stop-iteration-iterator | ||
[actions-url]: https://github.com/ljharb/stop-iteration-iterator/actions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
'use strict'; | ||
|
||
var SLOT = require('internal-slot'); | ||
|
||
var $SyntaxError = SyntaxError; | ||
var $StopIteration = typeof StopIteration === 'object' ? StopIteration : null; | ||
|
||
module.exports = function getStopIterationIterator(origIterator) { | ||
if (!$StopIteration) { | ||
throw new $SyntaxError('this environment lacks StopIteration'); | ||
} | ||
|
||
SLOT.set(origIterator, '[[Done]]', false); | ||
|
||
var siIterator = { | ||
next: function next() { | ||
var iterator = SLOT.get(this, '[[Iterator]]'); | ||
var done = SLOT.get(iterator, '[[Done]]'); | ||
try { | ||
return { | ||
done: done, | ||
value: done ? void undefined : iterator.next() | ||
}; | ||
} catch (e) { | ||
SLOT.set(iterator, '[[Done]]', true); | ||
if (e !== $StopIteration) { | ||
throw e; | ||
} | ||
return { | ||
done: true, | ||
value: void undefined | ||
}; | ||
} | ||
} | ||
}; | ||
|
||
SLOT.set(siIterator, '[[Iterator]]', origIterator); | ||
|
||
return siIterator; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
'use strict'; | ||
|
||
var test = require('tape'); | ||
|
||
var stopIterationIterator = require('../'); | ||
|
||
test('stopIterationIterator', function (t) { | ||
t.equal(typeof stopIterationIterator, 'function', 'stopIterationIterator is a function'); | ||
|
||
t.test('no StopIteration support', { skip: typeof StopIteration === 'object' }, function (st) { | ||
st['throws']( | ||
function () { stopIterationIterator(); }, | ||
SyntaxError, | ||
'throws a SyntaxError when StopIteration is not supported' | ||
); | ||
|
||
st.end(); | ||
}); | ||
|
||
t.test('StopIteration support', { skip: typeof StopIteration !== 'object' }, function (st) { | ||
var s = new Set([1, 2]); | ||
|
||
var i = s.iterator(); | ||
st.equal(i.next(), 1, 'first item is 1'); | ||
st.equal(i.next(), 2, 'second item is 2'); | ||
try { | ||
i.next(); | ||
st.fail(); | ||
} catch (e) { | ||
st.equal(e, StopIteration, 'StopIteration thrown'); | ||
} | ||
|
||
var m = new Map([[1, 'a'], [2, 'b']]); | ||
var mi = m.iterator(); | ||
st.deepEqual(mi.next(), [1, 'a'], 'first item is 1 and a'); | ||
st.deepEqual(mi.next(), [2, 'b'], 'second item is 2 and b'); | ||
try { | ||
mi.next(); | ||
st.fail(); | ||
} catch (e) { | ||
st.equal(e, StopIteration, 'StopIteration thrown'); | ||
} | ||
|
||
st.end(); | ||
}); | ||
|
||
t.end(); | ||
}); |