1 | 'use strict';
|
---|
2 | var InternalStateModule = require('../internals/internal-state');
|
---|
3 | var createIteratorConstructor = require('../internals/iterator-create-constructor');
|
---|
4 | var createIterResultObject = require('../internals/create-iter-result-object');
|
---|
5 | var isNullOrUndefined = require('../internals/is-null-or-undefined');
|
---|
6 | var isObject = require('../internals/is-object');
|
---|
7 | var defineBuiltInAccessor = require('../internals/define-built-in-accessor');
|
---|
8 | var DESCRIPTORS = require('../internals/descriptors');
|
---|
9 |
|
---|
10 | var INCORRECT_RANGE = 'Incorrect Iterator.range arguments';
|
---|
11 | var NUMERIC_RANGE_ITERATOR = 'NumericRangeIterator';
|
---|
12 |
|
---|
13 | var setInternalState = InternalStateModule.set;
|
---|
14 | var getInternalState = InternalStateModule.getterFor(NUMERIC_RANGE_ITERATOR);
|
---|
15 |
|
---|
16 | var $RangeError = RangeError;
|
---|
17 | var $TypeError = TypeError;
|
---|
18 |
|
---|
19 | var $RangeIterator = createIteratorConstructor(function NumericRangeIterator(start, end, option, type, zero, one) {
|
---|
20 | // TODO: Drop the first `typeof` check after removing legacy methods in `core-js@4`
|
---|
21 | if (typeof start != type || (end !== Infinity && end !== -Infinity && typeof end != type)) {
|
---|
22 | throw new $TypeError(INCORRECT_RANGE);
|
---|
23 | }
|
---|
24 | if (start === Infinity || start === -Infinity) {
|
---|
25 | throw new $RangeError(INCORRECT_RANGE);
|
---|
26 | }
|
---|
27 | var ifIncrease = end > start;
|
---|
28 | var inclusiveEnd = false;
|
---|
29 | var step;
|
---|
30 | if (option === undefined) {
|
---|
31 | step = undefined;
|
---|
32 | } else if (isObject(option)) {
|
---|
33 | step = option.step;
|
---|
34 | inclusiveEnd = !!option.inclusive;
|
---|
35 | } else if (typeof option == type) {
|
---|
36 | step = option;
|
---|
37 | } else {
|
---|
38 | throw new $TypeError(INCORRECT_RANGE);
|
---|
39 | }
|
---|
40 | if (isNullOrUndefined(step)) {
|
---|
41 | step = ifIncrease ? one : -one;
|
---|
42 | }
|
---|
43 | if (typeof step != type) {
|
---|
44 | throw new $TypeError(INCORRECT_RANGE);
|
---|
45 | }
|
---|
46 | if (step === Infinity || step === -Infinity || (step === zero && start !== end)) {
|
---|
47 | throw new $RangeError(INCORRECT_RANGE);
|
---|
48 | }
|
---|
49 | // eslint-disable-next-line no-self-compare -- NaN check
|
---|
50 | var hitsEnd = start !== start || end !== end || step !== step || (end > start) !== (step > zero);
|
---|
51 | setInternalState(this, {
|
---|
52 | type: NUMERIC_RANGE_ITERATOR,
|
---|
53 | start: start,
|
---|
54 | end: end,
|
---|
55 | step: step,
|
---|
56 | inclusive: inclusiveEnd,
|
---|
57 | hitsEnd: hitsEnd,
|
---|
58 | currentCount: zero,
|
---|
59 | zero: zero
|
---|
60 | });
|
---|
61 | if (!DESCRIPTORS) {
|
---|
62 | this.start = start;
|
---|
63 | this.end = end;
|
---|
64 | this.step = step;
|
---|
65 | this.inclusive = inclusiveEnd;
|
---|
66 | }
|
---|
67 | }, NUMERIC_RANGE_ITERATOR, function next() {
|
---|
68 | var state = getInternalState(this);
|
---|
69 | if (state.hitsEnd) return createIterResultObject(undefined, true);
|
---|
70 | var start = state.start;
|
---|
71 | var end = state.end;
|
---|
72 | var step = state.step;
|
---|
73 | var currentYieldingValue = start + (step * state.currentCount++);
|
---|
74 | if (currentYieldingValue === end) state.hitsEnd = true;
|
---|
75 | var inclusiveEnd = state.inclusive;
|
---|
76 | var endCondition;
|
---|
77 | if (end > start) {
|
---|
78 | endCondition = inclusiveEnd ? currentYieldingValue > end : currentYieldingValue >= end;
|
---|
79 | } else {
|
---|
80 | endCondition = inclusiveEnd ? end > currentYieldingValue : end >= currentYieldingValue;
|
---|
81 | }
|
---|
82 | if (endCondition) {
|
---|
83 | state.hitsEnd = true;
|
---|
84 | return createIterResultObject(undefined, true);
|
---|
85 | } return createIterResultObject(currentYieldingValue, false);
|
---|
86 | });
|
---|
87 |
|
---|
88 | var addGetter = function (key) {
|
---|
89 | defineBuiltInAccessor($RangeIterator.prototype, key, {
|
---|
90 | get: function () {
|
---|
91 | return getInternalState(this)[key];
|
---|
92 | },
|
---|
93 | set: function () { /* empty */ },
|
---|
94 | configurable: true,
|
---|
95 | enumerable: false
|
---|
96 | });
|
---|
97 | };
|
---|
98 |
|
---|
99 | if (DESCRIPTORS) {
|
---|
100 | addGetter('start');
|
---|
101 | addGetter('end');
|
---|
102 | addGetter('inclusive');
|
---|
103 | addGetter('step');
|
---|
104 | }
|
---|
105 |
|
---|
106 | module.exports = $RangeIterator;
|
---|