241 lines
6.4 KiB
JavaScript
241 lines
6.4 KiB
JavaScript
"use strict";
|
|
|
|
import {Check} from "./Check.ecma.js";
|
|
import {Patterns} from "./Patterns.ecma.js";
|
|
|
|
/**
|
|
* @callback anp_utils_default_callback
|
|
* @returns {void}
|
|
*/
|
|
|
|
/**
|
|
* @callback anp_utils_execute_callback
|
|
* @param {...any} [parameters]
|
|
* @returns {any|null}
|
|
*/
|
|
|
|
/**
|
|
* @callback anp_utils_execute_items_callback
|
|
* @param {?any} item
|
|
* @param {!anp_utils_default_callback} callback
|
|
* @returns {any|null}
|
|
*/
|
|
|
|
/**
|
|
* @class
|
|
* @constructor
|
|
* @returns {void}
|
|
* @access public
|
|
*/
|
|
export const Utils = (function(){
|
|
|
|
/**
|
|
* @constructs Utils
|
|
* @returns {void}
|
|
* @access private
|
|
*/
|
|
const Utils = function(){};
|
|
|
|
/**
|
|
* @param {?any} item
|
|
* @returns {Array.<any|null>}
|
|
* @access public
|
|
* @static
|
|
*/
|
|
Utils.get_array = item => Check.is_array(item) ? item : [item];
|
|
|
|
/**
|
|
* @param {!(string|Array.<string>)} keys
|
|
* @returns {Array.<string>}
|
|
* @access public
|
|
* @static
|
|
*/
|
|
Utils.get_keys = keys => Utils.get_array(keys).filter((key, i, array) => (
|
|
Check.is_key(key) && array.indexOf(key) == i
|
|
));
|
|
|
|
/**
|
|
* @param {!(string|Array.<string>)} keys
|
|
* @returns {Array.<string>}
|
|
* @access public
|
|
* @static
|
|
*/
|
|
Utils.get_pascal_keys = keys => Utils.get_array(keys).filter((key, i, array) => (
|
|
Check.is_pascal_key(key) && array.indexOf(key) == i
|
|
));
|
|
|
|
/**
|
|
* @param {!(string|Array.<string>)} strings
|
|
* @returns {Array.<string>}
|
|
* @access public
|
|
* @static
|
|
*/
|
|
Utils.get_strings = strings => Utils.get_array(strings).filter(string => Check.is_string(string));
|
|
|
|
/**
|
|
* @param {!(string|Array.<string>)} keys
|
|
* @param {!(Object.<string, any|null>|Array.<any|null>)} inputs
|
|
* @param {?any} [_default = null]
|
|
* @returns {any|null}
|
|
* @access public
|
|
* @static
|
|
*/
|
|
Utils.get_value = (keys, inputs, _default = null) => {
|
|
if((keys = Utils.get_keys(keys)).length){
|
|
for(let i = 0, l = (inputs = Utils.get_array(inputs)).length; i < l; i ++){
|
|
if(Check.is_dictionary(inputs[i])){
|
|
for(let j = 0, m = keys.length; j < m; j ++)
|
|
if(inputs[i][keys[j]] !== undefined)
|
|
return inputs[i][keys[j]];
|
|
}else if(Check.is_array(inputs[i])){
|
|
|
|
/** @type {any|null|undefined} */
|
|
const response = Utils.get_value(keys, inputs[i], undefined);
|
|
|
|
if(response !== undefined)
|
|
return response;
|
|
};
|
|
};
|
|
};
|
|
return _default;
|
|
};
|
|
|
|
/**
|
|
* @param {!(Object.<string, any|null>|Array.<any|null>)} item
|
|
* @param {!boolean} [overwrite = false]
|
|
* @returns {Object.<string, any|null>}
|
|
* @access public
|
|
* @static
|
|
*/
|
|
Utils.get_dictionary = (item, overwrite = false) => Utils.get_array(item).reduce((dictionary, subitem) => {
|
|
|
|
/** @type {Object.<string, any|null>} */
|
|
const results = (
|
|
Check.is_dictionary(subitem) ? subitem :
|
|
Check.is_array(subitem) ? Utils.get_dictionary(subitem, overwrite) :
|
|
{});
|
|
|
|
for(const key in results)
|
|
if(overwrite || dictionary[key] === undefined)
|
|
dictionary[key] = results[key];
|
|
|
|
return dictionary;
|
|
}, {});
|
|
|
|
/**
|
|
* @param {!anp_utils_execute_callback} callback
|
|
* @param {...any} [parameters]
|
|
* @returns {any|null}
|
|
* @access public
|
|
* @static
|
|
*/
|
|
Utils.execute = (callback, ...parameters) => {
|
|
return Check.is_function(callback) ? callback(...parameters) : null;
|
|
};
|
|
|
|
/**
|
|
* @param {!Array.<any|null>} items
|
|
* @param {!anp_utils_execute_items_callback} each_callback
|
|
* @param {?anp_utils_default_callback} [end_callback = null]
|
|
* @param {!number} [i = 0]
|
|
* @returns {void}
|
|
* @access public
|
|
* @static
|
|
*/
|
|
Utils.execute_items = (items, each_callback, end_callback = null, i = 0) => {
|
|
// console.log([i, items.length, items]);
|
|
Utils.execute(...(
|
|
i == items.length ? [end_callback] :
|
|
[each_callback, items[i], () => {
|
|
Utils.execute_items(items, each_callback, end_callback, i + 1);
|
|
}]));
|
|
};
|
|
|
|
/**
|
|
* @param {!string} string
|
|
* @param {!(Object.<string, any|null>|Array.<any|null>)} variables
|
|
* @param {?any} [_default = null]
|
|
* @returns {string}
|
|
* @access public
|
|
* @static
|
|
*/
|
|
Utils.string_variables = (string, variables, _default = null) => {
|
|
|
|
variables = Utils.get_dictionary(variables);
|
|
|
|
return ("" + string).replace(Patterns.RE_STRING_VARIABLES, (all, key) => (
|
|
variables[key] !== undefined ? variables[key] :
|
|
Check.is_null_or_undefined(_default) ? all :
|
|
_default));
|
|
};
|
|
|
|
/**
|
|
* @param {!(Object.<string, any|null>|Array.<any|null>)} data
|
|
* @returns {string|null}
|
|
* @access public
|
|
* @static
|
|
*/
|
|
Utils.json_encode = data => {
|
|
try{
|
|
return JSON.stringify(data);
|
|
}catch(exception){};
|
|
return null;
|
|
};
|
|
|
|
/**
|
|
* @param {!string} data
|
|
* @returns {Object.<string, any|null>|Array.<any|null>|null}
|
|
* @access public
|
|
* @static
|
|
*/
|
|
Utils.json_decode = data => {
|
|
try{
|
|
return JSON.parse(data);
|
|
}catch(exception){};
|
|
return null;
|
|
};
|
|
|
|
/**
|
|
* @param {!string} string
|
|
* @returns {string|null}
|
|
* @access public
|
|
* @static
|
|
*/
|
|
Utils.base64_encode = string => {
|
|
try{
|
|
return btoa(string);
|
|
}catch(exception){};
|
|
return null;
|
|
};
|
|
|
|
/**
|
|
* @param {!string} string
|
|
* @returns {string|null}
|
|
* @access public
|
|
* @static
|
|
*/
|
|
Utils.base64_decode = string => {
|
|
try{
|
|
return atob(string);
|
|
}catch(exception){};
|
|
return null;
|
|
};
|
|
|
|
/**
|
|
* @param {!(Object.<string, any|null>|Array.<any|null>)} variables
|
|
* @returns {string|null}
|
|
* @access public
|
|
* @static
|
|
*/
|
|
Utils.variables_encode = variables => Utils.base64_encode(Utils.json_encode(variables));
|
|
|
|
/**
|
|
* @param {!string} variables
|
|
* @returns {Object.<string, any|null>|Array.<any|null>|null}
|
|
* @access public
|
|
* @static
|
|
*/
|
|
Utils.variables_decode = variables => Utils.json_decode(Utils.base64_decode(variables));
|
|
|
|
return Utils
|
|
})(); |