Skip to content

Commit

Permalink
Initial implementation, tests, readme
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Feb 5, 2023
1 parent 313dcf5 commit 650713e
Show file tree
Hide file tree
Showing 113 changed files with 3,512 additions and 9 deletions.
62 changes: 62 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"root": true,

"extends": "@ljharb",

"globals": {
"Iterator": false,
},

"rules": {
"array-bracket-newline": 0,
"func-name-matching": 0,
"id-length": 0,
"max-lines-per-function": 0,
"multiline-comment-style": 0,
"new-cap": [2, {
"capIsNewExceptions": [
"Call",
"CreateIteratorFromClosure",
"CreateIterResultObject",
"GeneratorResume",
"GeneratorResumeAbrupt",
"GeneratorStart",
"GeneratorValidate",
"Get",
"GetIntrinsic",
"GetIterator",
"GetIteratorDirect",
"GetIteratorFlattenable",
"GetMethod",
"IsCallable",
"IteratorClose",
"IteratorComplete",
"IteratorNext",
"IteratorStep",
"IteratorValue",
"NormalCompletion",
"OrdinaryHasInstance",
"OrdinaryObjectCreate",
"PromiseResolve",
"ThrowCompletion",
"ToBoolean",
"ToIntegerOrInfinity",
"ToNumber",
"ToObject",
"Type",
],
}],
"no-negated-condition": 1,
"object-curly-newline": 0,
"sort-keys": 0,
},

"overrides": [
{
"files": "test/**",
"rules": {
"max-params": 0,
},
},
],
}
12 changes: 12 additions & 0 deletions .github/FUNDING.yml
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/iterator-helpers
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']
18 changes: 18 additions & 0 deletions .github/workflows/node-aught.yml
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'
7 changes: 7 additions & 0 deletions .github/workflows/node-pretest.yml
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
18 changes: 18 additions & 0 deletions .github/workflows/node-tens.yml
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'
9 changes: 9 additions & 0 deletions .github/workflows/rebase.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name: Automatic Rebase

on: [pull_request_target]

jobs:
_:
uses: ljharb/actions/.github/workflows/rebase.yml@main
secrets:
token: ${{ secrets.GITHUB_TOKEN }}
12 changes: 12 additions & 0 deletions .github/workflows/require-allow-edits.yml
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,5 @@ dist
npm-shrinkwrap.json
package-lock.json
yarn.lock

.npmignore
2 changes: 2 additions & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
package-lock=false
allow-same-version=true
message=v%s
13 changes: 13 additions & 0 deletions .nycrc
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"
]
}
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
3 changes: 3 additions & 0 deletions Iterator.from/auto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

require('./shim')();
34 changes: 34 additions & 0 deletions Iterator.from/implementation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict';

var OrdinaryHasInstance = require('es-abstract/2022/OrdinaryHasInstance');
var OrdinaryObjectCreate = require('es-abstract/2022/OrdinaryObjectCreate');
var ToObject = require('es-abstract/2022/ToObject');
var Type = require('es-abstract/2022/Type');

var GetIteratorFlattenable = require('../aos/GetIteratorFlattenable');

var $Iterator = require('../Iterator/polyfill')();
var $WrapForValidIteratorPrototype = require('../WrapForValidIteratorPrototype');

var SLOT = require('internal-slot');

module.exports = function from(O) {
if (Type(O) === 'String') {
// eslint-disable-next-line no-param-reassign
O = ToObject(O); // step 1
}

var iteratorRecord = GetIteratorFlattenable(O, 'sync'); // step 2

var hasInstance = OrdinaryHasInstance($Iterator, iteratorRecord['[[Iterator]]']); // step 3

if (hasInstance) { // step 4
return iteratorRecord['[[Iterator]]']; // step 4.a
}

var wrapper = OrdinaryObjectCreate($WrapForValidIteratorPrototype); // , ['[[Iterated]]']); // step 5

SLOT.set(wrapper, '[[Iterated]]', iteratorRecord); // step 6

return wrapper; // step 7
};
18 changes: 18 additions & 0 deletions Iterator.from/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

var callBind = require('call-bind');
var define = require('define-properties');

var implementation = require('./implementation');
var getPolyfill = require('./polyfill');
var shim = require('./shim');

var bound = callBind(getPolyfill(), null);

define(bound, {
getPolyfill: getPolyfill,
implementation: implementation,
shim: shim
});

module.exports = bound;
9 changes: 9 additions & 0 deletions Iterator.from/polyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

var implementation = require('./implementation');

var $Iterator = require('../Iterator');

module.exports = function getPolyfill() {
return typeof $Iterator.from === 'function' ? $Iterator.from : implementation;
};
18 changes: 18 additions & 0 deletions Iterator.from/shim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

var getPolyfill = require('./polyfill');
var define = require('define-properties');

var getIteratorPolyfill = require('../Iterator/polyfill');

module.exports = function shimIteratorFrom() {
var $Iterator = getIteratorPolyfill();
var polyfill = getPolyfill();
define(
$Iterator,
{ from: polyfill },
{ from: function () { return $Iterator.from !== polyfill; } }
);

return polyfill;
};
3 changes: 3 additions & 0 deletions Iterator.prototype.constructor/auto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

require('./shim')();
5 changes: 5 additions & 0 deletions Iterator.prototype.constructor/implementation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

var $Iterator = require('../Iterator/polyfill')();

module.exports = $Iterator;
18 changes: 18 additions & 0 deletions Iterator.prototype.constructor/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

var define = require('define-properties');
var bind = require('function-bind');

var implementation = require('./implementation');
var getPolyfill = require('./polyfill');
var shim = require('./shim');

var polyfill = bind.call(getPolyfill());

define(polyfill, {
getPolyfill: getPolyfill,
implementation: implementation,
shim: shim
});

module.exports = polyfill;
7 changes: 7 additions & 0 deletions Iterator.prototype.constructor/polyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

var implementation = require('./implementation');

module.exports = function getPolyfill() {
return implementation;
};
20 changes: 20 additions & 0 deletions Iterator.prototype.constructor/shim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

var define = require('define-properties');
var getPolyfill = require('./polyfill');

var $IteratorPrototype = require('iterator.prototype');

var $Iterator = require('./implementation');

module.exports = function shimIteratorPrototypeCtor() {
var polyfill = getPolyfill();

define(
$IteratorPrototype,
{ constructor: $Iterator },
{ constructor: function () { return $Iterator.constructor !== polyfill; } }
);

return polyfill;
};
3 changes: 3 additions & 0 deletions Iterator.prototype.drop/auto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

require('./shim')();
72 changes: 72 additions & 0 deletions Iterator.prototype.drop/implementation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
'use strict';

var GetIntrinsic = require('get-intrinsic');

var $RangeError = GetIntrinsic('%RangeError%');

var IteratorClose = require('es-abstract/2022/IteratorClose');
var IteratorStep = require('es-abstract/2022/IteratorStep');
var IteratorValue = require('es-abstract/2022/IteratorValue');
var ToIntegerOrInfinity = require('es-abstract/2022/ToIntegerOrInfinity');
var ToNumber = require('es-abstract/2022/ToNumber');

var GetIteratorDirect = require('../aos/GetIteratorDirect');
var CreateIteratorFromClosure = require('../aos/CreateIteratorFromClosure');

var iterHelperProto = require('../IteratorHelperPrototype');

var isNaN = require('es-abstract/helpers/isNaN');

var SLOT = require('internal-slot');

module.exports = function drop(limit) {
var iterated = GetIteratorDirect(this); // step 1

var numLimit = ToNumber(limit); // step 2
if (isNaN(numLimit)) {
throw new $RangeError('`limit` must be a non-NaN number'); // step 3
}

var integerLimit = ToIntegerOrInfinity(numLimit); // step 4
if (integerLimit < 0) {
throw new $RangeError('`limit` must be a >= 0'); // step 5
}

var sentinel = {};
var remaining = integerLimit; // step 6.a
var closure = function () { // step 6
var next;
while (remaining > 0) { // step 6.b
if (remaining !== Infinity) { // step 6.b.i
remaining -= 1; // step 6.b.i.1
}

next = IteratorStep(iterated['[[Iterator]]']); // step 6.b.ii
if (!next) {
// return void undefined; // step 6.b.iii
return sentinel;
}
}
// while (true) { // step 6.c
next = IteratorStep(iterated['[[Iterator]]']); // step 6.c.i
if (!next) {
// return void undefined; // step 6.c.ii
return sentinel;
}
try {
var value = IteratorValue(next); // step 6.c.iii
return value; // step 6.c.iii
} catch (e) {
// close iterator // step 6.c.icv
IteratorClose(
iterated['[[Iterator]]'],
function () { throw e; }
);
}
// }
return void undefined;
};
SLOT.set(closure, '[[Sentinel]]', sentinel); // for the userland implementation

return CreateIteratorFromClosure(closure, 'Iterator Helper', iterHelperProto); // step 4
};
Loading

0 comments on commit 650713e

Please sign in to comment.