Created
October 8, 2025 22:46
-
-
Save acquitelol/1bcdba63f458668d5d5e99ae9446eb99 to your computer and use it in GitHub Desktop.
Proxies arrays allowing for inserting elements by indexing with fractional indices (ie x[0.5] = 1 inserts between 0 and 1)
This file contains hidden or 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
| function arrWithFractionalIndices<T>(arr: T[]) { | |
| return new Proxy(arr, { | |
| get(target, prop: string, receiver) { | |
| if (!isNaN(+prop)) { | |
| return (Reflect.get(target, String(Math.ceil(+prop)), receiver) * (1 - +prop)) | |
| + (Reflect.get(target, String(Math.floor(+prop)), receiver) * +prop); | |
| } | |
| return Reflect.get(target, prop, receiver); | |
| }, | |
| set(target, prop: string, value: T, _) { | |
| if (!isNaN(+prop)) { | |
| target.splice(Math.ceil(+prop), 0, value); | |
| } | |
| return true; | |
| } | |
| }) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment