| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 1 | 'use strict'; |
| 2 | |
| 3 | var isExtendable = require('is-extendable'); |
| 4 | var forIn = require('for-in'); |
| 5 | |
| 6 | function mixinDeep(target, objects) { |
| 7 | var len = arguments.length, i = 0; |
| 8 | while (++i < len) { |
| 9 | var obj = arguments[i]; |
| 10 | if (isObject(obj)) { |
| 11 | forIn(obj, copy, target); |
| 12 | } |
| 13 | } |
| 14 | return target; |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * Copy properties from the source object to the |
| 19 | * target object. |
| 20 | * |
| 21 | * @param {*} `val` |
| 22 | * @param {String} `key` |
| 23 | */ |
| 24 | |
| 25 | function copy(val, key) { |
| 26 | if (!isValidKey(key)) { |
| 27 | return; |
| 28 | } |
| 29 | |
| 30 | var obj = this[key]; |
| 31 | if (isObject(val) && isObject(obj)) { |
| 32 | mixinDeep(obj, val); |
| 33 | } else { |
| 34 | this[key] = val; |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Returns true if `val` is an object or function. |
| 40 | * |
| 41 | * @param {any} val |
| 42 | * @return {Boolean} |
| 43 | */ |
| 44 | |
| 45 | function isObject(val) { |
| 46 | return isExtendable(val) && !Array.isArray(val); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Returns true if `key` is a valid key to use when extending objects. |
| 51 | * |
| 52 | * @param {String} `key` |
| 53 | * @return {Boolean} |
| 54 | */ |
| 55 | |
| 56 | function isValidKey(key) { |
| 57 | return key !== '__proto__' && key !== 'constructor' && key !== 'prototype'; |
| 58 | }; |
| 59 | |
| 60 | /** |
| 61 | * Expose `mixinDeep` |
| 62 | */ |
| 63 | |
| 64 | module.exports = mixinDeep; |