added some utilities to utils
This commit is contained in:
28
src/utils.js
28
src/utils.js
@@ -1,4 +1,4 @@
|
|||||||
import {mapObj} from './funky.js';
|
import {forEach, mapObj} from './funky.js';
|
||||||
|
|
||||||
export function noop() {};
|
export function noop() {};
|
||||||
|
|
||||||
@@ -43,6 +43,10 @@ export function hypotenuse(a, b) {
|
|||||||
return Math.sqrt(a*a + b*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) {
|
export function random(min, max) {
|
||||||
if (Array.isArray(min)) {
|
if (Array.isArray(min)) {
|
||||||
return min[random(min.length - 1)];
|
return min[random(min.length - 1)];
|
||||||
@@ -126,3 +130,25 @@ export function getColor ({red, green, blue, alpha}) {
|
|||||||
`rgb(${ red }, ${ green }, ${ blue })`;
|
`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;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user