ATLYSS TechPendium

Viewing old revision of Module:Utils

You are viewing an old revision of this page from 1/27/2026, 10:23:29 AM.

View latest version
This Module contains Internal Wiki logic and must be loaded using require keyword inside other logic modules.
Note that require supports only static imports.
This module can't be {{#invoke}}-ed.
exports.resolveArgs = function (args) {
    if (typeof args == "string") return { "0": args };
    return args;
}

exports.isModuleEmpty = function (mod) {
    return !mod || Object.keys(mod).length === 0;
}

exports.resolveData = async function (moduleName, test = false) {
    const Versions = await require(test ? `TEST/Versions` : `Versions`);
    const LatestGameVersion = Versions.latest;
    const VersionEntries = Versions.versions || [];

    let DATA = await require(test ? `TEST/${moduleName}/data/${LatestGameVersion}` : `${moduleName}/data/${LatestGameVersion}`);
    let usedVersion = LatestGameVersion;

    // Fallback: iterate versions list in order
    if (exports.isModuleEmpty(DATA)) {
        for (const entry of VersionEntries) {
            const v = entry.id;
            const candidate = await require(test ? `TEST/${moduleName}/data/${v}` : `${moduleName}/data/${v}`);

            if (!exports.isModuleEmpty(candidate)) {
                DATA = candidate;
                usedVersion = v;
                break;
            }
        }
    }

    return { data: DATA, version: usedVersion };
}

exports.sortObjectByKey = function (obj) {
    return Object.keys(obj).sort().reduce((result, key) => {
        result[key] = obj[key];
        return result;
    }, {});
}

exports.filterObjectByProperty = function (obj, key, value) {
    return Object.fromEntries(
        Object.entries(obj).filter(([_, v]) => v[key] === value)
    );
}

exports.sortObject = function (obj, compareFunction) {
    // Turn the object into an array of objects
    const entries = Object.entries(obj);

    // Sort the array using the provided compare function
    entries.sort((a, b) => compareFunction(a[1], b[1]));

    // Convert the sorted array back into an object
    return Object.fromEntries(entries);
}
Last Edited by LiveGobe on 1/27/2026, 10:23:29 AM

This page categories: