mirror of
https://github.com/idanoo/GoScrobble.git
synced 2024-11-25 09:55:15 +00:00
52 lines
1.3 KiB
JavaScript
52 lines
1.3 KiB
JavaScript
|
'use strict';
|
||
|
|
||
|
var safeIsNaN = Number.isNaN ||
|
||
|
function ponyfill(value) {
|
||
|
return typeof value === 'number' && value !== value;
|
||
|
};
|
||
|
function isEqual(first, second) {
|
||
|
if (first === second) {
|
||
|
return true;
|
||
|
}
|
||
|
if (safeIsNaN(first) && safeIsNaN(second)) {
|
||
|
return true;
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
function areInputsEqual(newInputs, lastInputs) {
|
||
|
if (newInputs.length !== lastInputs.length) {
|
||
|
return false;
|
||
|
}
|
||
|
for (var i = 0; i < newInputs.length; i++) {
|
||
|
if (!isEqual(newInputs[i], lastInputs[i])) {
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
function memoizeOne(resultFn, isEqual) {
|
||
|
if (isEqual === void 0) { isEqual = areInputsEqual; }
|
||
|
var lastThis;
|
||
|
var lastArgs = [];
|
||
|
var lastResult;
|
||
|
var calledOnce = false;
|
||
|
function memoized() {
|
||
|
var newArgs = [];
|
||
|
for (var _i = 0; _i < arguments.length; _i++) {
|
||
|
newArgs[_i] = arguments[_i];
|
||
|
}
|
||
|
if (calledOnce && lastThis === this && isEqual(newArgs, lastArgs)) {
|
||
|
return lastResult;
|
||
|
}
|
||
|
lastResult = resultFn.apply(this, newArgs);
|
||
|
calledOnce = true;
|
||
|
lastThis = this;
|
||
|
lastArgs = newArgs;
|
||
|
return lastResult;
|
||
|
}
|
||
|
return memoized;
|
||
|
}
|
||
|
|
||
|
module.exports = memoizeOne;
|