/
dimamir
/
medusa
Обзор
Документация
Войти
/
dimamir
/
medusa
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
develop
packages/core/utils/src/common/partition-array.ts
30 строк
699 B
Adrien de Peretti
chore(): start moving some packages to the core directory (#7215)
03 май 2024, 14:37
Не верифицирован
03 май 2024, 14:37
bbccd64
Код
Авторство
О чём код?
/** * Partitions an array into two arrays based on a predicate function * @example * const result = partitionArray([1, 2, 3, 4, 5], (x) => x % 2 === 0) * * console.log(result) * * // output: [[2, 4], [1, 3, 5]] * * @param {T} input input array of type T * @param {(T) => boolean} predicate function to use when split array elements */ export const partitionArray = <T>( input: T[], predicate: (T) => boolean ): [T[], T[]] => { return input.reduce( ([pos, neg], currentElement) => { if (predicate(currentElement)) { pos.push(currentElement) } else { neg.push(currentElement) } return [pos, neg] }, [[], []] as [T[], T[]] ) }