frdh-mhead-js/dist/_helpers.js
2022-11-18 21:42:43 +01:00

41 lines
1.1 KiB
JavaScript

/**
* Deep extend an object with the given defaults.
* Note that the extended object is not a clone, meaning the original object will also be updated.
*
* @param {object} orignl The object to extend to.
* @param {object} dfault The object to extend from.
* @return {object} The extended "orignl" object.
*/
export function extend(orignl, dfault) {
if (type(orignl) != 'object') {
orignl = {};
}
if (type(dfault) != 'object') {
dfault = {};
}
for (let k in dfault) {
if (!dfault.hasOwnProperty(k)) {
continue;
}
if (typeof orignl[k] == 'undefined') {
orignl[k] = dfault[k];
}
else if (type(orignl[k]) == 'object') {
extend(orignl[k], dfault[k]);
}
}
return orignl;
}
/**
* Get the type of any given variable. Improvement of "typeof".
*
* @param {any} variable The variable.
* @return {string} The type of the variable in lowercase.
*/
export function type(variable) {
return {}.toString
.call(variable)
.match(/\s([a-zA-Z]+)/)[1]
.toLowerCase();
}