200 lines
6.2 KiB
JavaScript
200 lines
6.2 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(key) || 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} target
|
|
* @param {!string} query
|
|
* @returns {boolean}
|
|
*/
|
|
Common.match_query = function(target, query){
|
|
if(!query || !query.trim())
|
|
return true;
|
|
|
|
const raw_query = query.trim(),
|
|
regex_match = raw_query.match(/^\/(.+)\/([gimsuy]*)$/);
|
|
|
|
if(!target)
|
|
target = "";
|
|
|
|
if(regex_match)
|
|
try{
|
|
const pattern = regex_match[1],
|
|
flags = regex_match[2] || "i",
|
|
regular_expression = new RegExp(pattern, flags);
|
|
return regular_expression.test(target);
|
|
}catch(exception){
|
|
return false;
|
|
};
|
|
|
|
const target_lower = ("" + target).toLowerCase(),
|
|
or_clauses = raw_query.split("||").map(s => s.trim()).filter(Boolean);
|
|
|
|
if(!or_clauses.length)
|
|
return true;
|
|
|
|
return or_clauses.some(or_clause => {
|
|
|
|
const and_terms = or_clause.split("&&").map(s => s.trim()).filter(Boolean);
|
|
|
|
if(!and_terms.length)
|
|
return true;
|
|
|
|
return and_terms.every(term => {
|
|
|
|
const is_negated = term.startsWith("!"),
|
|
clean_term = (is_negated ? term.slice(1) : term).trim().toLowerCase();
|
|
|
|
if(!clean_term)
|
|
return true;
|
|
|
|
const matches = target_lower.includes(clean_term);
|
|
|
|
return is_negated ? !matches : matches;
|
|
});
|
|
});
|
|
};
|
|
|
|
Common.get_minutes_diff = (start_time, end_time) => {
|
|
if(!start_time || !end_time || start_time === "--:--" || end_time === "--:--")
|
|
return null;
|
|
|
|
const [sh, sm] = start_time.split(":").map(Number),
|
|
[eh, em] = end_time.split(":").map(Number);
|
|
|
|
if(isNaN(sh) || isNaN(sm) || isNaN(eh) || isNaN(em))
|
|
return null;
|
|
|
|
let start_mins = sh * 60 + sm,
|
|
end_mins = eh * 60 + em;
|
|
|
|
// Si cruza medianoche (ej. 23:50 a 00:15)
|
|
if(end_mins < start_mins)
|
|
end_mins += 24 * 60;
|
|
|
|
return end_mins - start_mins;
|
|
};
|
|
|
|
return Common;
|
|
})(); |