Skip to main content
Module

x/rambda/src/groupWith.js

Faster and smaller alternative to Ramda
Go to Latest
File
import {_isArray} from './_internals/_isArray'import {cloneList} from './_internals/cloneList'
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}