fix: remove xo, migration to eslint

This commit is contained in:
JellyBrick
2023-08-29 17:22:38 +09:00
parent 31a7588cee
commit c722896a73
142 changed files with 17210 additions and 18409 deletions

View File

@ -1,10 +1,10 @@
module.exports = {
singleton,
debounce,
cache,
throttle,
memoize,
retry,
singleton,
debounce,
cache,
throttle,
memoize,
retry,
};
/**
@ -13,12 +13,15 @@ module.exports = {
* @returns {T}
*/
function singleton(fn) {
let called = false;
return (...args) => {
if (called) return;
called = true;
return fn(...args);
};
let called = false;
return (...args) => {
if (called) {
return;
}
called = true;
return fn(...args);
};
}
/**
@ -28,11 +31,11 @@ function singleton(fn) {
* @returns {T}
*/
function debounce(fn, delay) {
let timeout;
return (...args) => {
clearTimeout(timeout);
timeout = setTimeout(() => fn(...args), delay);
};
let timeout;
return (...args) => {
clearTimeout(timeout);
timeout = setTimeout(() => fn(...args), delay);
};
}
/**
@ -41,22 +44,23 @@ function debounce(fn, delay) {
* @returns {T}
*/
function cache(fn) {
let lastArgs;
let lastResult;
return (...args) => {
if (
args.length !== lastArgs?.length ||
args.some((arg, i) => arg !== lastArgs[i])
) {
lastArgs = args;
lastResult = fn(...args);
}
return lastResult;
};
let lastArgs;
let lastResult;
return (...args) => {
if (
args.length !== lastArgs?.length
|| args.some((arg, i) => arg !== lastArgs[i])
) {
lastArgs = args;
lastResult = fn(...args);
}
return lastResult;
};
}
/*
the following are currently unused, but potentially useful in the future
The following are currently unused, but potentially useful in the future
*/
/**
@ -66,14 +70,17 @@ function cache(fn) {
* @returns {T}
*/
function throttle(fn, delay) {
let timeout;
return (...args) => {
if (timeout) return;
timeout = setTimeout(() => {
timeout = undefined;
fn(...args);
}, delay);
};
let timeout;
return (...args) => {
if (timeout) {
return;
}
timeout = setTimeout(() => {
timeout = undefined;
fn(...args);
}, delay);
};
}
/**
@ -82,14 +89,15 @@ function throttle(fn, delay) {
* @returns {T}
*/
function memoize(fn) {
const cache = new Map();
return (...args) => {
const key = JSON.stringify(args);
if (!cache.has(key)) {
cache.set(key, fn(...args));
}
return cache.get(key);
};
const cache = new Map();
return (...args) => {
const key = JSON.stringify(args);
if (!cache.has(key)) {
cache.set(key, fn(...args));
}
return cache.get(key);
};
}
/**
@ -98,16 +106,16 @@ function memoize(fn) {
* @returns {T}
*/
function retry(fn, { retries = 3, delay = 1000 } = {}) {
return (...args) => {
try {
return fn(...args);
} catch (e) {
if (retries > 0) {
retries--;
setTimeout(() => retry(fn, { retries, delay })(...args), delay);
} else {
throw e;
}
}
};
return (...args) => {
try {
return fn(...args);
} catch (error) {
if (retries > 0) {
retries--;
setTimeout(() => retry(fn, { retries, delay })(...args), delay);
} else {
throw error;
}
}
};
}