mirror of
https://github.com/45Drives/cockpit-navigator.git
synced 2025-07-20 20:25:09 +02:00
add function to assign to nested objects with fallbacks
This commit is contained in:
parent
f94edd5b8e
commit
ada824fd8e
22
navigator-vue/src/functions/assignObjectRecursive.js
Normal file
22
navigator-vue/src/functions/assignObjectRecursive.js
Normal file
@ -0,0 +1,22 @@
|
||||
const isObject = (obj) => typeof obj === 'object' && !Array.isArray(obj) && obj !== null;
|
||||
|
||||
/**
|
||||
* Assign target to source with Object.assign(), filling in any missing properties from defaults
|
||||
*
|
||||
* @param {Object} target - target object
|
||||
* @param {Object} source - source object
|
||||
* @param {Object} defaults - fallback values if source is missing any properties
|
||||
* @returns {Object} - the target object
|
||||
*/
|
||||
function assignObjectRecursive(target, source, defaults = {}) {
|
||||
Object.assign(target, defaults, source);
|
||||
for (const key in defaults) {
|
||||
if (isObject(defaults[key]) && isObject(source[key])) {
|
||||
target[key] = {};
|
||||
assignObjectRecursive(target[key], source[key], defaults[key]);
|
||||
}
|
||||
}
|
||||
return target;
|
||||
}
|
||||
|
||||
export default assignObjectRecursive;
|
Loading…
x
Reference in New Issue
Block a user