UNPKG

rambdax

Version:

Extended version of Rambda - a lightweight, faster alternative to Ramda

45 lines (37 loc) 958 B
import { isArray } from './_internals/isArray.js' export function partitionObject(predicate, iterable){ const yes = {} const no = {} Object.entries(iterable).forEach(([ prop, value ]) => { if (predicate(value, prop)){ yes[ prop ] = value } else { no[ prop ] = value } }) return [ yes, no ] } export function partitionArray( predicate, list, indexed = false ){ const yes = [] const no = [] let counter = -1 while (counter++ < list.length - 1){ if ( indexed ? predicate(list[ counter ], counter) : predicate(list[ counter ]) ){ yes.push(list[ counter ]) } else { no.push(list[ counter ]) } } return [ yes, no ] } export function partition(predicate, iterable){ if (arguments.length === 1){ return listHolder => partition(predicate, listHolder) } if (!isArray(iterable)) return partitionObject(predicate, iterable) return partitionArray(predicate, iterable) }