* Merging MagicMirror² (or other) default/config script by `@bugsounet` * Merge 2 objects or/with array * * Usage: * ------- * this.config = configMerge({}, this.defaults, this.config) * ------- * arg1: initial object * arg2: config model * arg3: config to merge * ------- * why using it ?
(result)
| 442 | * @returns {object} the merged config |
| 443 | */ |
| 444 | function configMerge (result) { |
| 445 | const stack = Array.prototype.slice.call(arguments, 1); |
| 446 | let item, key; |
| 447 | |
| 448 | while (stack.length) { |
| 449 | item = stack.shift(); |
| 450 | for (key in item) { |
| 451 | if (item.hasOwnProperty(key)) { |
| 452 | if (typeof result[key] === "object" && result[key] && Object.prototype.toString.call(result[key]) !== "[object Array]") { |
| 453 | if (typeof item[key] === "object" && item[key] !== null) { |
| 454 | result[key] = configMerge({}, result[key], item[key]); |
| 455 | } else { |
| 456 | result[key] = item[key]; |
| 457 | } |
| 458 | } else { |
| 459 | result[key] = item[key]; |
| 460 | } |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | return result; |
| 465 | } |
| 466 | |
| 467 | Module.definitions = {}; |
| 468 |