rambdax
Version:
Extended version of Rambda - a lightweight, faster alternative to Ramda
41 lines (33 loc) • 877 B
JavaScript
import { _isArray } from './_internals/_isArray'
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){
const yes = []
const no = []
let counter = -1
while (counter++ < list.length - 1){
if (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)
}