mirror of
https://github.com/idanoo/GoScrobble.git
synced 2024-11-24 09:25:15 +00:00
57 lines
1.4 KiB
JavaScript
57 lines
1.4 KiB
JavaScript
/**
|
|
* Takes an argument and if it's an array, returns the first item in the array,
|
|
* otherwise returns the argument. Used for Preact compatibility.
|
|
*/
|
|
export var unwrapArray = function unwrapArray(arg) {
|
|
return Array.isArray(arg) ? arg[0] : arg;
|
|
};
|
|
/**
|
|
* Takes a maybe-undefined function and arbitrary args and invokes the function
|
|
* only if it is defined.
|
|
*/
|
|
|
|
export var safeInvoke = function safeInvoke(fn) {
|
|
if (typeof fn === "function") {
|
|
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
|
args[_key - 1] = arguments[_key];
|
|
}
|
|
|
|
return fn.apply(void 0, args);
|
|
}
|
|
};
|
|
/**
|
|
* Does a shallow equality check of two objects by comparing the reference
|
|
* equality of each value.
|
|
*/
|
|
|
|
export var shallowEqual = function shallowEqual(objA, objB) {
|
|
var aKeys = Object.keys(objA);
|
|
var bKeys = Object.keys(objB);
|
|
|
|
if (bKeys.length !== aKeys.length) {
|
|
return false;
|
|
}
|
|
|
|
for (var i = 0; i < bKeys.length; i++) {
|
|
var key = aKeys[i];
|
|
|
|
if (objA[key] !== objB[key]) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
};
|
|
/**
|
|
* Sets a ref using either a ref callback or a ref object
|
|
*/
|
|
|
|
export var setRef = function setRef(ref, node) {
|
|
// if its a function call it
|
|
if (typeof ref === "function") {
|
|
return safeInvoke(ref, node);
|
|
} // otherwise we should treat it as a ref object
|
|
else if (ref != null) {
|
|
ref.current = node;
|
|
}
|
|
}; |