Last active
November 6, 2019 11:53
-
-
Save rauschma/aff80ad0080e8b7af1a78d78b94f12f2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const handler = { | |
get(target, propKey, receiver) { | |
if (/^_[0-9]+$/.test(propKey)) { | |
const result = []; | |
const first = Number(receiver); | |
const last = Number(propKey.slice(1)); | |
for (let i=first; i<=last; i++) { | |
result.push(i); | |
} | |
return result; | |
} | |
return Reflect.get(target, propKey, receiver); | |
} | |
}; | |
const proxy = new Proxy(Object.prototype, handler); | |
Object.setPrototypeOf(Number.prototype, proxy); | |
/* | |
> 1 . _3 | |
[ 1, 2, 3 ] | |
> 11 . _14 | |
[ 11, 12, 13, 14 ] | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const handler = { | |
get(target, propKey, receiver) { | |
if (/^[0-9]+$/.test(propKey)) { | |
const result = []; | |
const first = Number(receiver); | |
const last = Number(propKey); | |
for (let i=first; i<=last; i++) { | |
result.push(i); | |
} | |
return result; | |
} | |
return Reflect.get(target, propKey, receiver); | |
} | |
}; | |
const proxy = new Proxy(Object.prototype, handler); | |
Object.setPrototypeOf(Number.prototype, proxy); | |
/* | |
> 1[3] | |
[ 1, 2, 3 ] | |
> 11[14] | |
[ 11, 12, 13, 14 ] | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You may want something like this in your if statements if you plan on testing this via console.log
if (typeof propKey !== 'symbol' && /^[0-9]+$/.test(propKey)) {
This is amazing btw.