mirror of https://github.com/ghostfolio/ghostfolio
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
958 B
44 lines
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)
|
|
}
|
|
|