2026-01-18 15:55:07 +03:00
|
|
|
/**
|
|
|
|
|
* Splits an array into two arrays based on a callback function.
|
|
|
|
|
* @param array The array to split.
|
|
|
|
|
* @param callback The callback function to determine which array to push each item to.
|
|
|
|
|
* @returns - An array containing two arrays, the first array contains items that passed the callback, the second array contains items that failed the callback.
|
|
|
|
|
*/
|
2026-01-18 14:37:23 +03:00
|
|
|
export function splitArray<T>(array: T[], callback: (item: T) => boolean) {
|
|
|
|
|
return array.reduce<[T[], T[]]>(
|
|
|
|
|
([pass, fail], item) => (
|
|
|
|
|
callback(item) ? pass.push(item) : fail.push(item), [pass, fail]
|
|
|
|
|
),
|
|
|
|
|
[[], []],
|
|
|
|
|
);
|
|
|
|
|
}
|