Refactor the custom helpers into their own files

This commit is contained in:
Oliver-Akins 2023-12-26 19:39:56 -07:00
parent af2df4f7c5
commit 0392d339db
7 changed files with 69 additions and 62 deletions

View file

@ -0,0 +1,3 @@
export function createArray(...args) {
return args.slice(0, -1);
};

View file

@ -0,0 +1,11 @@
/**
* Checks if the specified collapseId is currently open, so that during re-renders
* it remains open or closed.
*
* @param {Set<string>} expanded A set indicating what collapseIds are expanded
* @param {string} collapseId The collapseId to check for
* @returns {"open"|null} The HTML insertion indicating the details is expanded
*/
export function detailsExpanded(expanded, collapseId) {
return expanded.has(collapseId) ? "open" : null;
};

14
module/helpers/index.mjs Normal file
View file

@ -0,0 +1,14 @@
import { schemaOptions } from "./schemaOptions.mjs";
import { createArray } from "./createArray.mjs";
import { detailsExpanded } from "./detailsExpanded.mjs";
import { objectValue } from "./objectValue.mjs";
import { toFriendlyDuration } from "./toFriendlyDuration.mjs";
export default {
"dotdungeon-schemaOptions": schemaOptions,
"dotdungeon-array": createArray,
"dotdungeon-toFriendlyDuration": toFriendlyDuration,
"dotdungeon-objectValue": objectValue,
"dotdungeon-stringify": v => JSON.stringify(v, null, ` `),
"dotdungeon-expanded": detailsExpanded,
};

View file

@ -0,0 +1,11 @@
export function objectValue(obj, keypath) {
function helper(o, k) {
let v = o[k[0]];
if (typeof v === "object") {
return helper(v, k.slice(1));
};
return v;
};
let resp = helper(obj, keypath.string.split(`.`));
return resp;
};

View file

@ -0,0 +1 @@
export function schemaOptions() {};

View file

@ -0,0 +1,26 @@
const secondsInAMinute = 60;
const secondsInAnHour = 60 * secondsInAMinute;
/**
* Converts a duration into a more human-friendly format
* @param {number} duration The length of time in seconds
* @returns The human-friendly time string
*/
export function toFriendlyDuration(duration) {
let friendly = ``;
if (duration >= secondsInAnHour) {
let hours = Math.floor(duration / secondsInAnHour);
friendly += `${hours}h`;
duration -= hours * secondsInAnHour;
};
if (duration >= secondsInAMinute) {
let minutes = Math.floor(duration / secondsInAMinute);
friendly += `${minutes}m`;
duration -= minutes * secondsInAMinute;
};
if (duration > 0) {
friendly += `${duration}s`;
};
return friendly;
};