molstar
Version:
A comprehensive macromolecular library.
53 lines • 1.64 kB
JavaScript
/**
* Copyright (c) 2017 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author David Sehnal <david.sehnal@gmail.com>
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.groupBy = exports.Grouper = void 0;
var db_1 = require("../db");
var GroupingImpl = /** @class */ (function () {
function GroupingImpl(getKey) {
this.getKey = getKey;
this.map = new Map();
this.keys = [];
this.groups = [];
}
GroupingImpl.prototype.add = function (a) {
var key = this.getKey(a);
if (!!this.map.has(key)) {
var group = this.map.get(key);
group[group.length] = a;
}
else {
var group = [a];
this.map.set(key, group);
this.keys[this.keys.length] = key;
this.groups[this.groups.length] = group;
}
};
GroupingImpl.prototype.getGrouping = function () {
return { keys: this.keys, groups: this.groups, map: this.map };
};
return GroupingImpl;
}());
function Grouper(getKey) {
return new GroupingImpl(getKey);
}
exports.Grouper = Grouper;
function groupBy(values, getKey) {
var gs = Grouper(getKey);
if (db_1.Column.is(values)) {
var v = values.value;
for (var i = 0, _i = values.rowCount; i < _i; i++)
gs.add(v(i));
}
else {
for (var i = 0, _i = values.length; i < _i; i++)
gs.add(values[i]);
}
return gs.getGrouping();
}
exports.groupBy = groupBy;
//# sourceMappingURL=grouping.js.map
;