Skip to content

Instantly share code, notes, and snippets.

@acquitelol
Created October 8, 2025 22:46
Show Gist options
  • Select an option

  • Save acquitelol/1bcdba63f458668d5d5e99ae9446eb99 to your computer and use it in GitHub Desktop.

Select an option

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)
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