added some utilities to utils

This commit is contained in:
Gavin McDonald
2018-09-02 22:13:49 -04:00
parent 9dddf78ccc
commit 727a8ea105

View File

@@ -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;
}