ATLYSS TechPendium

Viewing old revision of Module:Utils

You are viewing an old revision of this page from 2/3/2026, 9:51:45 PM.

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 versionsModuleName = test ? `TEST/Versions` : `Versions`;
    const Versions = await requireData(versionsModuleName);
    const LatestGameVersion = Versions.latest;
    const VersionEntries = Versions.versions || [];

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

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

            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);
}

exports.findByProperty = function (obj, key, value) {
    return Object.values(obj).find(item => item[key] === value);
}
Last Edited by LiveGobe on 2/3/2026, 9:51:45 PM

This page categories: