Stage 1 Draft / March 28, 2022

Proposal Number.range & BigInt.range

1 Number

1.1 Number.range ( start, end, option )

  1. Return ? CreateNumericRangeIterator(start, end, option, number-range).

2 BigInt

2.1 BigInt.range ( start, end, option )

  1. Return ? CreateNumericRangeIterator(start, end, option, bigint-range).

3 Algorithms

3.1 CreateNumericRangeIterator ( start, end, option, type )

  1. If type is number-range, let primitiveType be "number", else let primitiveType be "bigint".
  2. If Type(start) is not primitiveType, throw a TypeError.
  3. If type is bigint-range, let zero be 0n, else let zero be 0.
  4. If type is bigint-range, let one be 1n, else let one be 1.
  5. NOTE: Editor's Note
    Allowing all kinds of number (number, bigint, decimals, ...) to range from a finite number to infinity.
    .
  6. If end is not +∞ or -∞ and if Type(end) is not primitiveType, throw a TypeError exception.
  7. If start is +∞ or -∞, throw a RangeError exception.
  8. Let ifIncrease be end > start.
  9. Let inclusiveEnd be false.
  10. If option is undefined or null, let step be undefined.
  11. Else if Type(option) is object, then
    1. Let step be ? Get(option, "step").
    2. Let inclusiveEnd be ToBoolean(? Get(option, "inclusive")).
  12. Else if Type(option) is primitiveType, let step be option.
  13. Else, throw a TypeError exception.
  14. If step is undefined or null, then
    1. If ifIncrease is true, let step be one.
    2. Else let step be -one.
  15. If Type(step) is not primitiveType, throw a TypeError exception.
  16. If step is +∞ or -∞, throw a RangeError exception.
  17. If step is zero and start is not equal to end, throw a RangeError exception.
  18. Let closure be a new Abstract Closure with no parameters that captures start, end, step, inclusiveEnd, zero, one and performs the following steps when called:
    1. If start is NaN, return undefined.
    2. If end is NaN, return undefined.
    3. If step is NaN, return undefined.
    4. Let ifIncrease be end > start.
    5. Let ifStepIncrease to be step > zero.
    6. If ifIncrease is not equal to ifStepIncrease, return undefined.
    7. Let hitsEnd be false.
    8. Let currentCount be zero.
    9. NOTE: Debug these steps at https://tc39.es/proposal-Number.range/playground.html .
    10. Repeat, while hitsEnd is false,
      1. Let currentYieldingValue be start + (step * currentCount).
      2. If currentYieldingValue equal to end, Set hitsEnd to true.
      3. Set currentCount to currentCount + one. NOTE: Prevent value overflow.
      4. Let endCondition be false.
      5. If ifIncrease is true, then
        1. If inclusiveEnd is true, set endCondition be currentYieldingValue > end.
        2. Else set endCondition be currentYieldingValue >= end.
      6. Else,
        1. If inclusiveEnd is true, set endCondition be end > currentYieldingValue.
        2. Else set endCondition be end >= currentYieldingValue.
      7. If endCondition is true, return undefined.
      8. Perform ? Yield(currentYieldingValue).
    11. Return undefined.
  19. Let iterator be CreateIteratorFromClosure(closure, Numberic Range Iterator, %NumericRangeIteratorPrototype%, « start, end, step, inclusiveEnd »).
  20. Set iterator.[[start]] to start.
  21. Set iterator.[[end]] to end.
  22. Set iterator.[[step]] to step.
  23. Set iterator.[[inclusiveEnd]] to inclusiveEnd.
  24. Return iterator.

4 Properties of the NumericRangeIterator Prototype Object

The NumericRangeIterator prototype object is the intrinsic object %NumericRangeIteratorPrototype%.

The value has a [[Prototype]] internal slot whose value is the intrinsic object %IteratorPrototype%.

4.1 %NumericRangeIterator%.next ( )

  1. Return ? GeneratorResume(this value, undefined, Numeric Range Iterator).

4.2 %NumericRangeIteratorPrototype%.[@@toStringTag]

The initial value of the @@toStringTag property is the String value "NumericRangeIterator".

This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: true }.

4.3 %NumericRangeIteratorPrototype%.start

%NumericRangeIteratorPrototype%.start is an accessor property with attributes { [[Enumerable]]: false, [[Configurable]]: true}. The [[Get]] attribute is defined as follows:

4.3.1 get %NumericRangeIteratorPrototype%.start

  1. Perform ? RequireInternalSlot(this, [[start]]).
  2. Return this.[[start]].

4.4 %NumericRangeIteratorPrototype%.end

%NumericRangeIteratorPrototype%.end is an accessor property with attributes { [[Enumerable]]: false, [[Configurable]]: true}. The [[Get]] attribute is defined as follows:

4.4.1 get %NumericRangeIteratorPrototype%.end

  1. Perform ? RequireInternalSlot(this, [[end]]).
  2. Return this.[[end]].

4.5 %NumericRangeIteratorPrototype%.inclusive

%NumericRangeIteratorPrototype%.inclusive is an accessor property with attributes { [[Enumerable]]: false, [[Configurable]]: true}. The [[Get]] attribute is defined as follows:

4.5.1 get %NumericRangeIteratorPrototype%.isEnclusiveEnd

  1. Perform ? RequireInternalSlot(this, [[inclusiveEnd]]).
  2. Return this.[[inclusiveEnd]].

4.6 %NumericRangeIteratorPrototype%.step

%NumericRangeIteratorPrototype%.step is an accessor property with attributes { [[Enumerable]]: false, [[Configurable]]: true}. The [[Get]] attribute is defined as follows:

4.6.1 get %NumericRangeIteratorPrototype%.step

  1. Perform ? RequireInternalSlot(this, [[step]]).
  2. Return this.[[step]].

4.7 Internal slots

Table 1: Internal slots
Internal slot Description
[[start]]The range starts start.
[[end]]The range ends to.
[[step]]The range step size.
[[inclusiveEnd]]Boolean. If the end is inclusive.

A Copyright & Software License

Copyright Notice

© 2022 Jack Works

Software License

All Software contained in this document ("Software") is protected by copyright and is being made available under the "BSD License", included below. This Software may be subject to third party rights (rights from parties other than Ecma International), including patent rights, and no licenses under such third party rights are granted under this license even if the third party concerned is a member of Ecma International. SEE THE ECMA CODE OF CONDUCT IN PATENT MATTERS AVAILABLE AT https://ecma-international.org/memento/codeofconduct.htm FOR INFORMATION REGARDING THE LICENSING OF PATENT CLAIMS THAT ARE REQUIRED TO IMPLEMENT ECMA INTERNATIONAL STANDARDS.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  3. Neither the name of the authors nor Ecma International may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE ECMA INTERNATIONAL "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ECMA INTERNATIONAL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.