Add prepend util

This commit is contained in:
Jon Staab
2025-11-12 11:06:13 -08:00
parent cc17711dc0
commit 24860c9a02
2 changed files with 11 additions and 0 deletions
+3
View File
@@ -218,6 +218,9 @@ export declare const take: <T>(n: number, xs: Iterable<T>) => T[];
// Concatenates multiple arrays, filtering out null/undefined // Concatenates multiple arrays, filtering out null/undefined
export declare const concat: <T>(...xs: T[][]) => T[]; export declare const concat: <T>(...xs: T[][]) => T[];
// Prepends element to array
export declare const prepend: <T>(x: T, xs: T[]) => T[];
// Appends element to array // Appends element to array
export declare const append: <T>(x: T, xs: T[]) => T[]; export declare const append: <T>(x: T, xs: T[]) => T[];
+8
View File
@@ -430,6 +430,14 @@ export const concat = <T>(...xs: T[][]) => xs.flatMap(x => (x === undefined ? []
*/ */
export const append = <T>(x: T, xs: Iterable<T>) => concat(Array.from(xs), [x]) export const append = <T>(x: T, xs: Iterable<T>) => concat(Array.from(xs), [x])
/**
* Prepends element to array
* @param x - Element to prepend
* @param xs - Array to prepend to
* @returns New array with element prepended
*/
export const prepend = <T>(x: T, xs: Iterable<T>) => concat([x], Array.from(xs))
/** /**
* Creates union of two arrays * Creates union of two arrays
* @param a - First array * @param a - First array