| export {}; |
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| var index_1 = require("../src/index"); | ||
| jest.useFakeTimers(); | ||
| describe('Debouncing Utility', function () { | ||
| describe('debounce', function () { | ||
| it('should debounce a function', function () { | ||
| var callback = jest.fn(); | ||
| var debounced = (0, index_1.debounce)(callback, 100); | ||
| debounced(); | ||
| jest.advanceTimersByTime(50); | ||
| debounced(); | ||
| expect(callback).not.toHaveBeenCalled(); | ||
| jest.advanceTimersByTime(100); | ||
| expect(callback).toHaveBeenCalledTimes(1); | ||
| debounced.cancel(); | ||
| debounced.flush(); | ||
| }); | ||
| }); | ||
| }); |
| export {}; |
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| var index_1 = require("../src/index"); | ||
| jest.useFakeTimers(); | ||
| describe('Throttling Utility', function () { | ||
| describe('throttle', function () { | ||
| it('should throttle a function', function () { | ||
| var callback = jest.fn(); | ||
| var throttled = (0, index_1.throttle)(callback, 100); | ||
| throttled(); | ||
| jest.advanceTimersByTime(50); | ||
| throttled(); | ||
| expect(callback).toHaveBeenCalledTimes(1); | ||
| throttled.cancel(); | ||
| }); | ||
| }); | ||
| }); |
| export * from './utils/debounce'; | ||
| export * from './utils/throttle'; |
| "use strict"; | ||
| var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
| if (k2 === undefined) k2 = k; | ||
| var desc = Object.getOwnPropertyDescriptor(m, k); | ||
| if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
| desc = { enumerable: true, get: function() { return m[k]; } }; | ||
| } | ||
| Object.defineProperty(o, k2, desc); | ||
| }) : (function(o, m, k, k2) { | ||
| if (k2 === undefined) k2 = k; | ||
| o[k2] = m[k]; | ||
| })); | ||
| var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
| for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
| }; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| __exportStar(require("./utils/debounce"), exports); | ||
| __exportStar(require("./utils/throttle"), exports); |
| export type AnyFunction = (...args: any[]) => void; |
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); |
| import { DebounceOptions, DebouncedFunction } from "../types/debounce"; | ||
| export declare function debounce<T extends (...args: any[]) => void>(func: T, delay: number, options?: DebounceOptions): DebouncedFunction<T>; |
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.debounce = void 0; | ||
| function debounce(func, delay, options) { | ||
| var _a, _b; | ||
| var timeoutId; | ||
| var lastArgs; | ||
| var lastCallTime; | ||
| var leading = (_a = options === null || options === void 0 ? void 0 : options.leading) !== null && _a !== void 0 ? _a : false; | ||
| var trailing = (_b = options === null || options === void 0 ? void 0 : options.trailing) !== null && _b !== void 0 ? _b : true; | ||
| var maxWait = options === null || options === void 0 ? void 0 : options.maxWait; | ||
| function invokeFunc() { | ||
| if (lastArgs) { | ||
| lastCallTime = Date.now(); | ||
| func.apply(void 0, lastArgs); | ||
| lastArgs = null; | ||
| } | ||
| } | ||
| var debounced = function () { | ||
| var args = []; | ||
| for (var _i = 0; _i < arguments.length; _i++) { | ||
| args[_i] = arguments[_i]; | ||
| } | ||
| var currentTime = Date.now(); | ||
| if (!lastCallTime && !leading) { | ||
| lastCallTime = currentTime; | ||
| } | ||
| var remainingDelay = delay - (currentTime - lastCallTime); | ||
| if (remainingDelay <= 0 || remainingDelay > delay) { | ||
| if (timeoutId) { | ||
| clearTimeout(timeoutId); | ||
| timeoutId = null; | ||
| } | ||
| lastCallTime = currentTime; | ||
| return func.apply(void 0, args); // Ensure to return the result of func | ||
| } | ||
| else if (!timeoutId && trailing) { | ||
| lastArgs = args; | ||
| timeoutId = setTimeout(invokeFunc, remainingDelay); | ||
| } | ||
| }; | ||
| debounced.cancel = function () { | ||
| if (timeoutId) { | ||
| clearTimeout(timeoutId); | ||
| timeoutId = null; | ||
| lastArgs = null; | ||
| lastCallTime = 0; | ||
| } | ||
| }; | ||
| debounced.flush = function () { | ||
| if (timeoutId) { | ||
| clearTimeout(timeoutId); | ||
| invokeFunc(); | ||
| } | ||
| }; | ||
| if (maxWait !== undefined) { | ||
| var maxWaitHandler = function () { | ||
| if (timeoutId) { | ||
| clearTimeout(timeoutId); | ||
| invokeFunc(); | ||
| } | ||
| }; | ||
| setInterval(maxWaitHandler, maxWait); | ||
| } | ||
| return debounced; | ||
| } | ||
| exports.debounce = debounce; |
| import { ThrottleControl, ThrottleOptions } from "../types/throttle"; | ||
| export declare function throttle<T extends (...args: any[]) => void>(func: T, wait: number, options?: ThrottleOptions): T & ThrottleControl; |
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.throttle = void 0; | ||
| function throttle(func, wait, options) { | ||
| if (options === void 0) { options = {}; } | ||
| var timeout = null; | ||
| var previous = 0; | ||
| var throttled = function () { | ||
| var _this = this; | ||
| var args = []; | ||
| for (var _i = 0; _i < arguments.length; _i++) { | ||
| args[_i] = arguments[_i]; | ||
| } | ||
| var now = Date.now(); | ||
| if (!previous && options.leading === false) | ||
| previous = now; | ||
| var remaining = wait - (now - previous); | ||
| if (remaining <= 0 || remaining > wait) { | ||
| if (timeout) { | ||
| clearTimeout(timeout); | ||
| timeout = null; | ||
| } | ||
| previous = now; | ||
| func.apply(this, args); | ||
| } | ||
| else if (!timeout && options.trailing !== false) { | ||
| timeout = setTimeout(function () { | ||
| previous = options.leading === false ? 0 : Date.now(); | ||
| timeout = null; | ||
| func.apply(_this, args); | ||
| }, remaining); | ||
| } | ||
| }; | ||
| throttled.cancel = function () { | ||
| if (timeout) { | ||
| clearTimeout(timeout); | ||
| timeout = null; | ||
| } | ||
| previous = 0; | ||
| }; | ||
| return throttled; | ||
| } | ||
| exports.throttle = throttle; |
+4
-2
| { | ||
| "name": "time-loom", | ||
| "version": "1.2.1", | ||
| "version": "2.0.0", | ||
| "description": "", | ||
@@ -11,3 +11,4 @@ "main": "dist/index.js", | ||
| "scripts": { | ||
| "build": "tsc", | ||
| "clean": "del-cli ./build/*", | ||
| "build": "npm run clean && tsc", | ||
| "prepublishOnly": "npm test", | ||
@@ -20,2 +21,3 @@ "test": "jest" | ||
| "@types/jest": "^29.5.11", | ||
| "del-cli": "^5.1.0", | ||
| "jest": "^29.7.0", | ||
@@ -22,0 +24,0 @@ "ts-jest": "^29.1.1", |
18878
51.64%27
80%303
138.58%6
20%