Skip to content

Instantly share code, notes, and snippets.

@rauschma
Last active November 6, 2019 11:53
Show Gist options
  • Star 28 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rauschma/aff80ad0080e8b7af1a78d78b94f12f2 to your computer and use it in GitHub Desktop.
Save rauschma/aff80ad0080e8b7af1a78d78b94f12f2 to your computer and use it in GitHub Desktop.
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 ]
*/
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 ]
*/
@BenAychh
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment