From 727a8ea10555f6a4c2be41ed6e00e28b6ddc87eb Mon Sep 17 00:00:00 2001 From: Gavin McDonald Date: Sun, 2 Sep 2018 22:13:49 -0400 Subject: [PATCH] added some utilities to utils --- src/utils.js | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) 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; +}