diff --git a/src/utils.js b/src/utils.js index 0ece316..8b71e7b 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,4 +1,4 @@ -import {mapObj} from './funky.js'; +import {forEach, mapObj} from './funky.js'; export function noop() {}; @@ -43,6 +43,10 @@ export function hypotenuse(a, b) { return Math.sqrt(a*a + b*b); } +export function isObject (obj) { + return obj && (typeof obj === 'object') && !Array.isArray(obj) ? true : false; +} + export function random(min, max) { if (Array.isArray(min)) { return min[random(min.length - 1)]; @@ -126,3 +130,25 @@ export function getColor ({red, green, blue, alpha}) { `rgb(${ red }, ${ green }, ${ blue })`; } +export function extend (obj, ...sources) { + return Object.assign({}, obj, ...sources); +} + +export function extendDeep (obj, ...sources) { + let extended = extend(obj); + + sources.forEach(src => { + forEach(src, (value, key) => { + if (has(src, key)) { + if (isObject(value) && isObject(extended[key])) { + extended[key] = extendDeep(extended[key], value); + } + else { + extended[key] = value; + } + } + }); + }); + + return extended; +}