| import { cloneList } from './_internals/cloneList.js' |
| import { isArray } from './_internals/isArray.js' |
| |
| export function groupWith(compareFn, list){ |
| if (!isArray(list)) throw new TypeError('list.reduce is not a function') |
| |
| const clone = cloneList(list) |
| |
| 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 |
| } |