Skip to content

Instantly share code, notes, and snippets.

@danakt
Last active October 25, 2017 14:28
Show Gist options
  • Select an option

  • Save danakt/edd8b1036095eea928b19ece4132cf2a to your computer and use it in GitHub Desktop.

Select an option

Save danakt/edd8b1036095eea928b19ece4132cf2a to your computer and use it in GitHub Desktop.

Revisions

  1. danakt revised this gist Oct 25, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion iterableNumber.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    Number.prototype[Symbol.iterator] = function* () {
    if (isNaN(this) || !isFinite(this)) {
    throw new Error('Non-iterable number')
    throw new ReferenceError('Non-iterable number')
    }

    const abs = Math.abs(this)
  2. danakt created this gist Oct 25, 2017.
    17 changes: 17 additions & 0 deletions iterableNumber.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    Number.prototype[Symbol.iterator] = function* () {
    if (isNaN(this) || !isFinite(this)) {
    throw new Error('Non-iterable number')
    }

    const abs = Math.abs(this)
    const isNegative = this < 0

    for (let i = 0; i < abs; i++) {
    yield isNegative ? -i : i
    }
    }

    [...10] // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    for (let i of 5) {
    // ...
    }