UNPKG

rambda

Version:

Lightweight faster alternative to Ramda

42 lines (31 loc) 852 B
export function groupWith(compareFn, list){ if (!Array.isArray(list)) throw new TypeError('list.reduce is not a function') const clone = list.slice() const toReturn = [] let holder = [] clone.reduce(( prev, current, i ) => { if (i === 0) return current const okCompare = compareFn(prev, current) const holderIsEmpty = holder.length === 0 const lastCall = i === list.length - 1 if (okCompare){ if (holderIsEmpty) holder.push(prev) holder.push(current) if (lastCall) toReturn.push(holder) return current } if (holderIsEmpty){ toReturn.push([ prev ]) if (lastCall) toReturn.push([ current ]) return current } toReturn.push(holder) if (lastCall) toReturn.push([ current ]) holder = [] return current }, undefined) return toReturn }