foundryvtt-reve-de-dragon/module/item.js

28 lines
759 B
JavaScript
Raw Normal View History

/* -------------------------------------------- */
export class RdDItem {
/* -------------------------------------------- */
static buildItemsClassification(items) {
return RdDItem.classify(items, it => it.type)
}
static classify(items, classifier = it => it.type, transform = it => it) {
let itemsBy = {};
RdDItem.classifyInto(itemsBy, items, classifier, transform);
return itemsBy;
}
static classifyInto(itemsBy, items, classifier = it => it.type, transform = it => it) {
for (const item of items) {
const classification = classifier(item);
let list = itemsBy[classification];
if (!list) {
list = [];
itemsBy[classification] = list;
}
list.push(transform(item));
}
}
}