Nodejs Number Iterate __iterator__(iterKeys)

Here you can find the source of __iterator__(iterKeys)

Method Source Code

/*/*from www .  j a va2 s  .c om*/
 * JavaScript Number iterator
 * Requires JavaScript 1.7+
 *
 * 2010-03-11
 * 
 * By Eli Grey, http://eligrey.com
 *
 * Public Domain.
 * NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
 */

"use strict";

Number.prototype.__iterator__ = function (iterKeys) {
   if (this % 1) {
      throw new TypeError("Integer expected for number iterator");
   }
   
   var i = 1 - iterKeys,
       j = this + i;
   
   if (j < 0) {
      i = j - i + iterKeys;
      j = +iterKeys;
   }
   
   for (; i < j; i++) {
      yield i;
   }
};

/* examples:
 * [i for (i in 5)] is [0,1,2,3,4]
 * [i for each (i in 5)] is [1,2,3,4,5]
 * [i for (i in -5)] is [-4,-3,-2,-1,0]
 * [i for each (i in -5)] is [-5,-4,-3,-2,-1]
 */

Related

  1. downto(down, iterator)
    Number.prototype.downto = function (down, iterator) {
      for (var i = this.valueOf(); i >= down; i--) {
        iterator(i);
      return this.valueOf();
    };
    
  2. iters()
    Number.prototype.iters = function () {
        return [(this for (x of [0])),
                (this for (y of [0]))];
    };
    var [a, b] = (3).iters();
    var three = a.next();
    assertEq(Object.prototype.toString.call(three), '[object Number]');
    assertEq(+three, 3);
    assertEq(b.next(), three);
    ...
    
  3. upto(up, iterator)
    Number.prototype.upto = function (up, iterator) {
      for (var i = this.valueOf(); i <= up; i++) {
        iterator(i);
      return this.valueOf();
    };
    
  4. upto(from)
    Number.prototype.upto = function (from) {
      var a = [];
      var to = this.valueOf();
      var i = to - 1;
      while (i++ < from) {
        a.push(i);
      return a;
    };
    ...