Skip to content

Instantly share code, notes, and snippets.

@fauxsaurus
Created December 18, 2020 01:22
Show Gist options
  • Save fauxsaurus/83367f4e0bb7094a7c6fa25570a4aed4 to your computer and use it in GitHub Desktop.
Save fauxsaurus/83367f4e0bb7094a7c6fa25570a4aed4 to your computer and use it in GitHub Desktop.
Array Utils
// note: the existing item needs to appear after so that this can put stuff at the start of the array
// note: if an i > length is used, it will just be added to the last place
const insertAt = <T>(arr: T[], i:number, item: T) =>
arr.slice(0, i).concat([item]).concat(arr.slice(i)); // prettier-ignore
const removeAt = <T>(arr: T[], i: number) =>
arr.slice(0, i).concat(arr.slice(i + 1)) // prettier-ignore
const setAt = <T>(arr: T[], i: number, item: T) =>
arr.slice(0, i).concat([item]).concat(arr.slice(i + 1)); // prettier-ignore
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment