JudgePole/Public/ecma/Utils/Common.ecma.js
2026-09-20 17:12:09 +02:00

127 lines
4.1 KiB
JavaScript

"use strict";
const Common = (function(){
const Common = function(){};
Common.get_keys = (...items) => items.reduce((keys, item) => {
if(item){
if(typeof item == "string" && /^[a-z_][a-z0-9_]*$/i.test(item) && !keys.includes(item))
keys.push(item);
else if(item instanceof Array)
for(const key of Common.get_keys(...item))
keys.includes(item) || keys.push(key);
};
return keys;
}, []);
Common.get_dictionaries = (...items) => items.reduce((dictionaries, item) => {
if(item){
if(item.constructor == Object)
dictionaries.push(item);
else if(item instanceof Array)
dictionaries.push(...Common.get_dictionaries(...item));
};
return dictionaries;
}, []);
Common.get_value = (keys, inputs, _default = null) => {
if((keys = Common.get_keys(keys)).length)
for(const subinputs of Common.get_dictionaries(inputs))
for(const key of keys)
if(subinputs[key] !== undefined)
return subinputs[key];
return _default;
};
Common.get_dictionary = (items, overwrite = false) => {
if(items){
if(items.constructor == Object)
return items;
else if(items instanceof Array)
return items.reduce((dictionary, item) => {
Object.entries(Common.get_dictionary(item)).forEach(([key, value]) => {
if(overwrite || dictionary[key] === undefined)
dictionary[key] = value;
});
return dictionary;
}, {});
};
return {};
};
Common.string_variables = (string, variables, _default = null) => {
variables = Common.get_dictionary(variables);
return ("" + string).replace(/\{([^\{\}]+)\}/g, (all, key) => (
variables[key] !== undefined ? variables[key] :
_default !== null ? _default :
all));
};
Common.execute_array = (array, each_callback, end_callback, i = 0) => {
if(i == array.length)
end_callback();
else
each_callback(array[i], () => {
Common.execute_array(array, each_callback, end_callback, i + 1);
});
};
Common.SCSS = (data, selector = "") => {
/** @type {Arrya.<string>} */
const childs = [];
/** @type {string} */
let css = ``;
Object.entries(data).forEach(([key, value]) => {
if(value && value.constructor == Object)
key.split(",").forEach(key => {
/** @type {string} */
const subselector = `${(selector ? selector + (
key[0] == "&" ? "" : " "
) : "") + (
key[0] == "&" ? key.substring(1) : key
)}`,
/** @type {Array<string>} */
subcss = Common.SCSS(value, subselector);
childs.push(`\n\n${subselector}{${subcss[0]}\n}`, ...subcss.slice(1));
});
else
css += `\n ${key.replace(/[^a-z0-9]+/gi, "-")} : ${value === null ? "none" : value};`;
});
if(!selector){
/** @type {HTMLStyleElement} */
let style = document.querySelector("head>style");
if(!style){
style = document.querySelector("head").appendChild(document.createElement("style"));
[
["data-type", "text/css;charset=utf-8"],
["data-language", "CSS3"],
["charset", "utf-8"]
].forEach(([key, value]) => style.setAttribute(key, value === null ? "none" : value));
};
style.appendChild(document.createTextNode(css + childs.join("")));
};
return [css, ...childs];
};
return Common;
})();