main
Last change
on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago |
Update repo after prototype presentation
|
-
Property mode
set to
100644
|
File size:
1.1 KB
|
Rev | Line | |
---|
[d565449] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | var hasSymbols = require('has-symbols')();
|
---|
| 4 | var GetIntrinsic = require('get-intrinsic');
|
---|
| 5 | var callBound = require('call-bind/callBound');
|
---|
| 6 | var isString = require('is-string');
|
---|
| 7 |
|
---|
| 8 | var $iterator = GetIntrinsic('%Symbol.iterator%', true);
|
---|
| 9 | var $stringSlice = callBound('String.prototype.slice');
|
---|
| 10 | var $String = GetIntrinsic('%String%');
|
---|
| 11 |
|
---|
| 12 | module.exports = function getIteratorMethod(ES, iterable) {
|
---|
| 13 | var usingIterator;
|
---|
| 14 | if (hasSymbols) {
|
---|
| 15 | usingIterator = ES.GetMethod(iterable, $iterator);
|
---|
| 16 | } else if (ES.IsArray(iterable)) {
|
---|
| 17 | usingIterator = function () {
|
---|
| 18 | var i = -1;
|
---|
| 19 | var arr = this; // eslint-disable-line no-invalid-this
|
---|
| 20 | return {
|
---|
| 21 | next: function () {
|
---|
| 22 | i += 1;
|
---|
| 23 | return {
|
---|
| 24 | done: i >= arr.length,
|
---|
| 25 | value: arr[i]
|
---|
| 26 | };
|
---|
| 27 | }
|
---|
| 28 | };
|
---|
| 29 | };
|
---|
| 30 | } else if (isString(iterable)) {
|
---|
| 31 | usingIterator = function () {
|
---|
| 32 | var i = 0;
|
---|
| 33 | return {
|
---|
| 34 | next: function () {
|
---|
| 35 | var nextIndex = ES.AdvanceStringIndex($String(iterable), i, true);
|
---|
| 36 | var value = $stringSlice(iterable, i, nextIndex);
|
---|
| 37 | i = nextIndex;
|
---|
| 38 | return {
|
---|
| 39 | done: nextIndex > iterable.length,
|
---|
| 40 | value: value
|
---|
| 41 | };
|
---|
| 42 | }
|
---|
| 43 | };
|
---|
| 44 | };
|
---|
| 45 | }
|
---|
| 46 | return usingIterator;
|
---|
| 47 | };
|
---|
Note:
See
TracBrowser
for help on using the repository browser.