Skip to content
Snippets Groups Projects
Commit d517c873 authored by David Sehnal's avatar David Sehnal
Browse files

added data/util/group-by

parent 75829b47
No related branches found
No related tags found
No related merge requests found
/**
* Copyright (c) 2017 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author David Sehnal <david.sehnal@gmail.com>
*/
export interface Grouping<V, K> {
keys: ReadonlyArray<K>,
groups: ReadonlyArray<ReadonlyArray<V>>
}
class GroupingImpl<K, V> {
private byKey = new Map<K, V[]>();
readonly keys: K[] = [];
readonly groups: V[][] = [];
add(a: V) {
const key = this.getKey(a) as any;
if (!!this.byKey.has(key)) {
const group = this.byKey.get(key)!;
group[group.length] = a;
} else {
const group = [a];
this.byKey.set(key, group);
this.keys[this.keys.length] = key;
this.groups[this.groups.length] = group;
}
}
getGrouping(): Grouping<V, K> {
return { keys: this.keys, groups: this.groups };
}
constructor(private getKey: (v: V) => K) { }
}
export function Grouper<V, K>(getKey: (x: V) => K) {
return new GroupingImpl<K, V>(getKey);
}
function groupBy<V, K>(values: ArrayLike<V>, getKey: (x: V) => K) {
const gs = Grouper(getKey);
for (let i = 0, _i = values.length; i < _i; i++) gs.add(values[i]);
return gs.getGrouping();
}
export default groupBy;
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment