Skip to content

Instantly share code, notes, and snippets.

@fauxsaurus
Last active November 20, 2025 00:18
Show Gist options
  • Select an option

  • Save fauxsaurus/83367f4e0bb7094a7c6fa25570a4aed4 to your computer and use it in GitHub Desktop.

Select an option

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
const splitArrayAt = <T>(i: number, arr: T[]) => [arr.slice(0, i), arr.slice(i)]
const toggleArrayValue = <T>(array: T[], value2toggle: T) => {
if (!array.includes(value2toggle)) return array.concat([value2toggle]);
return array.filter((value) => value !== value2toggle);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment