thaw
Version:
The narrow belt for AOP 🎀
54 lines (53 loc) • 1.18 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.throttle = exports.debounce = exports.compose = exports.before = exports.around = exports.after = void 0;
const around = (fn, cb) => {
return (...args) => {
return cb(fn(cb(...args)));
};
};
exports.around = around;
const after = (fn, cb) => {
return (...args) => {
return cb(fn(...args));
};
};
exports.after = after;
const before = (fn, cb) => {
return (...args) => {
return fn(cb(...args));
};
};
exports.before = before;
const compose = (...fns) => {
return (...args) => {
let result = null;
for (let i = 0; i < fns.length; i++) {
result = fns[i](...(i ? [result] : args));
}
return result;
};
};
exports.compose = compose;
const debounce = (fn, wait = 0) => {
let tick;
return (...args) => {
clearTimeout(tick);
tick = setTimeout(() => {
fn(...args);
}, wait);
};
};
exports.debounce = debounce;
const throttle = (fn, wait = 0) => {
let tick;
return (...args) => {
tick = !tick && setTimeout(() => {
tick = clearTimeout(tick);
fn(...args);
}, wait);
};
};
exports.throttle = throttle;