deno.land / x / rambda@v9.1.1 / source / groupWith.js

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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}
rambda

Version Info

Tagged at
2 months ago