Skip to content

Instantly share code, notes, and snippets.

@jonrandy
Last active July 16, 2024 14:21
Show Gist options
  • Save jonrandy/a6a3c27b2ad9e6ec077ecce6ee746d06 to your computer and use it in GitHub Desktop.
Save jonrandy/a6a3c27b2ad9e6ec077ecce6ee746d06 to your computer and use it in GitHub Desktop.
Range
const to = Symbol('number to')
// Number.prototype[to] = function to(end) {
// let [start, tend, rev] = (this>end) ? [end, this, true] : [this, end, false], arr = []
// for (let i=start; i<=tend; i++) arr.push(i)
// return rev ? arr.sort(_=>1) : arr
// }
Number.prototype[to] = function to(end, {step}={step:this<=end?1:-1}) {
let arr = [], i, d = end>this
for (i=+this;d?(i<=end):(i>=end);i+=step) arr.push(i)
return arr
}
console.log(1[to](11))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment