UNPKG

rambdax

Version:

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

46 lines (32 loc) 937 B
import { _isArray } from './_internals/_isArray' export function groupWith(compareFn, list){ if (!_isArray(list)) throw new TypeError('list.reduce is not a function') const clone = list.slice() if (list.length === 1) return [ clone ] 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 }