HollowBond/Public/ecma/Utils/Common.ecma.js

166 lines
5.4 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];
};
/**
* @param {!string} string
* @returns {number}
*/
Common.string_to_integer = string => string.split("").reduce((total, character, j) => (total + character.charCodeAt(0) * (j + 1)) | 0, 0);
/**
* @param {?(number|string)} [seed = null]
* @param {!number} [i = 0]
* @returns {number}
*/
Common.random = (seed = null, i = 0) => {
if(seed === null)
return Math.random() * 0xFFFFFFFF | 0;
let state, temp;
if(typeof seed == "string")
seed = Common.string_to_integer(seed);
return (((
temp = (
temp = Math.imul((
state = (seed + Math.imul(i, 0x9E3779B9) + 0x6D2B79F5) & 0xFFFFFFFF
) ^ state >>> 15, 1 | state)
) + Math.imul(temp ^ temp >>> 7, 61 | temp) ^ temp
) ^ temp >>> 14) >>> 0) / 4294967296;
};
Common.random_decay = (value, from, to, decay) => {
const minimum = Math.min(from, to) | 0,
maximum = Math.max(from, to) | 0,
// Factor normalizado en escala logarítmica
k = Math.max(0.01, Math.min(0.99, decay));
return Math.min(maximum, minimum + Math.max(0, Math.floor(Math.log(1 - Math.max(0, Math.min(0.999999, value)) * (1 - Math.pow(k, maximum - minimum + 1))) / Math.log(k))));
};
return Common;
})();