MCPcopy Create free account
hub / github.com/freeCodeCamp/freeCodeCamp / insertInto

Function insertInto

tools/challenge-helper-scripts/helpers/utils.ts:1–8  ·  view source on GitHub ↗
(arr: T[], index: number, elem: T)

Source from the content-addressed store, hash-verified

1export function insertInto<T>(arr: T[], index: number, elem: T): T[] {
2 if (index >= arr.length) return [...arr, elem];
3 if (index <= 0) return [elem, ...arr];
4
5 return arr.flatMap((x, id) => {
6 return id === index ? [elem, x] : x;
7 });
8}
9
10// fs Promise functions return errors, but no stack trace. This adds back in
11// the stack trace.

Calls

no outgoing calls

Tested by

no test coverage detected