Tomotoes / weibo

GNU Lesser General Public License v3.0
9 stars 0 forks source link

thank u, [Symbol.iterator].next #97

Open Tomotoes opened 4 years ago

Tomotoes commented 4 years ago

🙃🙃🙃

const range = (start = 0, stop, step = 1) => {
  if (stop === undefined) {
    [start, stop] = [0, start]
  }

  start -= step
  return {
    [Symbol.iterator]: () => ({
      next: () => ({
        value: start += step,
        done: start >= stop
      })
    })
  }
}

Number.prototype.to = stop => range(this, stop);

for (let num of (1).to(10)) {
  // ...
}

console.log([...(1).to(10)]) // [1, 2, 3, 4, 5, 6, 7, 8, 9]

thank u, [Symbol.iterator].next