You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

27 lines
452 B

import { reduce } from './reduce.js'
export function collectBy(fn, list){
if (arguments.length === 1){
return _list => collectBy(fn, _list)
}
const group = reduce(
(o, x) => {
const tag = fn(x)
if (o[ tag ] === undefined){
o[ tag ] = []
}
o[ tag ].push(x)
return o
},
{},
list
)
const newList = []
for (const tag in group){
newList.push(group[ tag ])
}
return newList
}