2020-11-14 03:16:03 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This class is intended as a placeholder for utility methods unrelated
|
|
|
|
* to actual classes of the game system or of FoundryVTT
|
|
|
|
*/
|
|
|
|
export class Misc {
|
2021-01-05 18:43:13 +01:00
|
|
|
static isFunction(v) {
|
|
|
|
return v && {}.toString.call(v) === '[object Function]';
|
2021-03-22 20:10:37 +01:00
|
|
|
}
|
2021-01-05 18:43:13 +01:00
|
|
|
|
2020-11-15 02:07:41 +01:00
|
|
|
static upperFirst(text) {
|
2020-11-14 03:16:03 +01:00
|
|
|
return text.charAt(0).toUpperCase() + text.slice(1);
|
|
|
|
}
|
2021-01-05 18:43:13 +01:00
|
|
|
|
2021-03-22 20:10:37 +01:00
|
|
|
static toSignedString(number) {
|
2020-11-15 02:07:41 +01:00
|
|
|
const value = parseInt(number)
|
|
|
|
const isPositiveNumber = value != NaN && value > 0;
|
2021-03-22 20:10:37 +01:00
|
|
|
return isPositiveNumber ? "+" + number : number
|
2020-11-15 02:07:41 +01:00
|
|
|
}
|
2021-01-05 18:43:13 +01:00
|
|
|
|
2020-11-15 02:07:41 +01:00
|
|
|
/**
|
|
|
|
* Converts the value to an integer, or to 0 if undefined/null/not representing integer
|
|
|
|
* @param {*} value value to convert to an integer using parseInt
|
|
|
|
*/
|
2021-03-22 20:10:37 +01:00
|
|
|
static toInt(value) {
|
|
|
|
if (value == undefined) {
|
2020-11-15 02:07:41 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
const parsed = parseInt(value);
|
|
|
|
return isNaN(parsed) ? 0 : parsed;
|
|
|
|
}
|
2021-01-05 18:43:13 +01:00
|
|
|
|
|
|
|
static getFractionHtml(diviseur) {
|
|
|
|
if (!diviseur || diviseur <= 1) return undefined;
|
|
|
|
switch (diviseur || 1) {
|
|
|
|
case 2: return '½';
|
|
|
|
case 4: return '¼';
|
|
|
|
default: return '1/' + diviseur;
|
|
|
|
}
|
|
|
|
}
|
2021-02-05 01:38:40 +01:00
|
|
|
|
|
|
|
static classify(items, classifier = it => it.type, transform = it => it) {
|
|
|
|
let itemsBy = {};
|
|
|
|
Misc.classifyInto(itemsBy, items, classifier, transform);
|
|
|
|
return itemsBy;
|
|
|
|
}
|
|
|
|
|
2021-03-19 22:20:01 +01:00
|
|
|
static classifyFirst(items, classifier) {
|
|
|
|
let itemsBy = {};
|
|
|
|
for (const item of items) {
|
|
|
|
const classification = classifier(item);
|
|
|
|
if (!itemsBy[classification]) {
|
|
|
|
itemsBy[classification] = item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return itemsBy;
|
|
|
|
}
|
2021-03-25 03:18:27 +01:00
|
|
|
|
2021-02-05 01:38:40 +01:00
|
|
|
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));
|
|
|
|
}
|
2021-03-25 03:18:27 +01:00
|
|
|
for (const [key, list] of Object.entries(itemsBy)) {
|
|
|
|
list.sort();
|
|
|
|
};
|
2021-02-05 01:38:40 +01:00
|
|
|
}
|
|
|
|
|
2021-02-12 01:11:03 +01:00
|
|
|
static rollOneOf(array) {
|
2021-03-28 22:37:04 +02:00
|
|
|
return array[new Roll("1d" + array.length).evaluate( { async: false} ).total - 1];
|
2021-02-12 01:11:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static distinct(array) {
|
|
|
|
return [...new Set(array)];
|
|
|
|
}
|
|
|
|
|
2021-03-22 20:10:37 +01:00
|
|
|
static actorData(actor) {
|
|
|
|
return Misc.data(actor);
|
|
|
|
}
|
|
|
|
|
|
|
|
static itemData(item) {
|
|
|
|
return Misc.data(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
static data(it) {
|
|
|
|
if (it instanceof Item) {
|
|
|
|
return it.data;
|
|
|
|
}
|
|
|
|
if (it instanceof Actor) {
|
|
|
|
return it.data;
|
|
|
|
}
|
|
|
|
return it;
|
|
|
|
}
|
|
|
|
|
|
|
|
static templateData(it) {
|
|
|
|
return Misc.data(it)?.data ?? {}
|
|
|
|
}
|
2020-11-14 03:16:03 +01:00
|
|
|
}
|