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.
36 lines
673 B
36 lines
673 B
import { isArray } from './_internals/isArray.js'
|
|
import { curry } from './curry.js'
|
|
|
|
class ReduceStopper{
|
|
constructor(value){
|
|
this.value = value
|
|
}
|
|
}
|
|
|
|
export function reduceFn(
|
|
reducer, acc, list
|
|
){
|
|
if (list == null){
|
|
return acc
|
|
}
|
|
if (!isArray(list)){
|
|
throw new TypeError('reduce: list must be array or iterable')
|
|
}
|
|
let index = 0
|
|
const len = list.length
|
|
|
|
while (index < len){
|
|
acc = reducer(
|
|
acc, list[ index ], index, list
|
|
)
|
|
if (acc instanceof ReduceStopper){
|
|
return acc.value
|
|
}
|
|
index++
|
|
}
|
|
|
|
return acc
|
|
}
|
|
|
|
export const reduce = curry(reduceFn)
|
|
export const reduceStopper = value => new ReduceStopper(value)
|
|
|