+21
| MIT License | ||
| Copyright (c) [2023] [Muhammed_Nabeel] | ||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
| The above copyright notice and this permission notice shall be included in | ||
| all copies or substantial portions of the Software. | ||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| THE SOFTWARE. |
@@ -1,2 +0,2 @@ | ||
| import { AnyFunction } from "../types/types"; | ||
| export declare const debounce: (func: AnyFunction, delay: number) => AnyFunction; | ||
| 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; | ||
| const debounce = (func, delay) => { | ||
| function debounce(func, delay, options) { | ||
| var _a, _b; | ||
| let timeoutId; | ||
| return function (...args) { | ||
| clearTimeout(timeoutId); | ||
| timeoutId = setTimeout(() => { | ||
| func.apply(this, args); | ||
| }, delay); | ||
| let lastArgs; | ||
| let lastCallTime; | ||
| const leading = (_a = options === null || options === void 0 ? void 0 : options.leading) !== null && _a !== void 0 ? _a : false; | ||
| const trailing = (_b = options === null || options === void 0 ? void 0 : options.trailing) !== null && _b !== void 0 ? _b : true; | ||
| const maxWait = options === null || options === void 0 ? void 0 : options.maxWait; | ||
| function invokeFunc() { | ||
| if (lastArgs) { | ||
| lastCallTime = Date.now(); | ||
| func(...lastArgs); | ||
| lastArgs = null; | ||
| } | ||
| } | ||
| const debounced = function (...args) { | ||
| const currentTime = Date.now(); | ||
| if (!lastCallTime && !leading) { | ||
| lastCallTime = currentTime; | ||
| } | ||
| const remainingDelay = delay - (currentTime - lastCallTime); | ||
| if (remainingDelay <= 0 || remainingDelay > delay) { | ||
| if (timeoutId) { | ||
| clearTimeout(timeoutId); | ||
| timeoutId = null; | ||
| } | ||
| lastCallTime = currentTime; | ||
| return func(...args); // Ensure to return the result of func | ||
| } | ||
| else if (!timeoutId && trailing) { | ||
| lastArgs = args; | ||
| timeoutId = setTimeout(invokeFunc, remainingDelay); | ||
| } | ||
| }; | ||
| }; | ||
| debounced.cancel = () => { | ||
| if (timeoutId) { | ||
| clearTimeout(timeoutId); | ||
| timeoutId = null; | ||
| lastArgs = null; | ||
| lastCallTime = 0; | ||
| } | ||
| }; | ||
| debounced.flush = () => { | ||
| if (timeoutId) { | ||
| clearTimeout(timeoutId); | ||
| invokeFunc(); | ||
| } | ||
| }; | ||
| if (maxWait !== undefined) { | ||
| const maxWaitHandler = () => { | ||
| if (timeoutId) { | ||
| clearTimeout(timeoutId); | ||
| invokeFunc(); | ||
| } | ||
| }; | ||
| setInterval(maxWaitHandler, maxWait); | ||
| } | ||
| return debounced; | ||
| } | ||
| exports.debounce = debounce; | ||
| //# sourceMappingURL=debounce.js.map |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"debounce.js","sourceRoot":"","sources":["../../src/utils/debounce.ts"],"names":[],"mappings":";;;AAEO,MAAM,QAAQ,GAAG,CAAC,IAAiB,EAAE,KAAa,EAAe,EAAE;IACtE,IAAI,SAAyB,CAAC;IAC9B,OAAO,UAAU,GAAG,IAAW;QAC3B,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;YAChD,IAAI,SAAS,EAAE,CAAC;gBACZ,YAAY,CAAC,SAAS,CAAC,CAAC;gBACxB,SAAS,GAAG,IAAI,CAAC;YACrB,CAAC;YAED,YAAY,GAAG,WAAW,CAAC;YAC3B,OAAO,IAAI,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,sCAAsC;QAChE,CAAC;aAAM,IAAI,CAAC,SAAS,IAAI,QAAQ,EAAE,CAAC;YAChC,QAAQ,GAAG,IAAI,CAAC;YAChB,SAAS,GAAG,UAAU,CAAC,UAAU,EAAE,cAAc,CAAC,CAAC;QACvD,CAAC;IACL,CAAC,CAAC;IAEF,SAAS,CAAC,MAAM,GAAG,GAAG,EAAE;QACpB,IAAI,SAAS,EAAE,CAAC;YACZ,YAAY,CAAC,SAAS,CAAC,CAAC;YACxB,SAAS,GAAG,IAAI,CAAC;YACjB,QAAQ,GAAG,IAAI,CAAC;YAChB,YAAY,GAAG,CAAC,CAAC;QACrB,CAAC;IACL,CAAC,CAAC;IAEF,SAAS,CAAC,KAAK,GAAG,GAAG,EAAE;QACnB,IAAI,SAAS,EAAE,CAAC;YACZ,YAAY,CAAC,SAAS,CAAC,CAAC;YACxB,UAAU,EAAE,CAAC;QACjB,CAAC;IACL,CAAC,CAAC;IAEF,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;QACxB,MAAM,cAAc,GAAG,GAAG,EAAE;YACxB,IAAI,SAAS,EAAE,CAAC;gBACZ,YAAY,CAAC,SAAS,CAAC,CAAC;gBACxB,UAAU,EAAE,CAAC;YACjB,CAAC;QACL,CAAC,CAAC;QAEF,WAAW,CAAC,cAAc,EAAE,OAAO,CAAC,CAAC;IACzC,CAAC;IAED,OAAO,SAAS,CAAC;AACrB,CAAC;AAxED,4BAwEC"} |
@@ -1,2 +0,2 @@ | ||
| import { AnyFunction } from "../types/types"; | ||
| export declare const throttle: (func: AnyFunction, interval: number) => AnyFunction; | ||
| 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; | ||
| const throttle = (func, interval) => { | ||
| let lastExecuted = 0; | ||
| return function (...args) { | ||
| function throttle(func, wait, options = {}) { | ||
| let timeout = null; | ||
| let previous = 0; | ||
| const throttled = function (...args) { | ||
| const now = Date.now(); | ||
| if (now - lastExecuted >= interval) { | ||
| if (!previous && options.leading === false) | ||
| previous = now; | ||
| const remaining = wait - (now - previous); | ||
| if (remaining <= 0 || remaining > wait) { | ||
| if (timeout) { | ||
| clearTimeout(timeout); | ||
| timeout = null; | ||
| } | ||
| previous = now; | ||
| func.apply(this, args); | ||
| lastExecuted = now; | ||
| } | ||
| else if (!timeout && options.trailing !== false) { | ||
| timeout = setTimeout(() => { | ||
| previous = options.leading === false ? 0 : Date.now(); | ||
| timeout = null; | ||
| func.apply(this, args); | ||
| }, remaining); | ||
| } | ||
| }; | ||
| }; | ||
| throttled.cancel = () => { | ||
| if (timeout) { | ||
| clearTimeout(timeout); | ||
| timeout = null; | ||
| } | ||
| previous = 0; | ||
| }; | ||
| return throttled; | ||
| } | ||
| exports.throttle = throttle; | ||
| //# sourceMappingURL=throttle.js.map |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"throttle.js","sourceRoot":"","sources":["../../src/utils/throttle.ts"],"names":[],"mappings":";;;AAEO,MAAM,QAAQ,GAAG,CAAC,IAAiB,EAAE,QAAgB,EAAe,EAAE;IACzE,IAAI,YAAY,GAAG,CAAC,CAAC;IACrB,OAAO,UAAU,GAAG,IAAW;QAC3B,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QACvB,IAAI,GAAG,GAAG,YAAY,IAAI,QAAQ,EAAE,CAAC;YACjC,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;YACvB,YAAY,GAAG,GAAG,CAAC;QACvB,CAA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AC,MAAM,GAAG,GAAG,EAAE;QACpB,IAAI,OAAO,EAAE,CAAC;YACV,YAAY,CAAC,OAAO,CAAC,CAAC;YACtB,OAAO,GAAG,IAAI,CAAC;QACnB,CAAC;QACD,QAAQ,GAAG,CAAC,CAAC;IACjB,CAAC,CAAC;IAEF,OAAO,SAAgC,CAAC;AAC5C,CAAC;AAtCD,4BAsCC"} |
+14
-3
| { | ||
| "name": "time-loom", | ||
| "version": "1.1.1", | ||
| "version": "1.2.0", | ||
| "description": "", | ||
@@ -27,3 +27,14 @@ "main": "dist/index.js", | ||
| "npm": ">=7.0.0" | ||
| } | ||
| } | ||
| }, | ||
| "keywords": [ | ||
| "debounce", | ||
| "throttle", | ||
| "utility", | ||
| "function", | ||
| "delay", | ||
| "timer", | ||
| "performance", | ||
| "javascript", | ||
| "typescript" | ||
| ] | ||
| } |
+26
-0
@@ -39,3 +39,15 @@ # Time Loom | ||
| - `delay`: The delay in milliseconds. | ||
| - `options`: (Optional) Additional options for customization (e.g., { leading: true, trailing: false, maxWait: 1000 }). | ||
| # Debounce Function Options: | ||
| - `leading (boolean)`: Whether to execute the function on the leading edge of the timeout (default: false). | ||
| - `trailing (boolean)`: Whether to execute the function on the trailing edge of the timeout (default: true). | ||
| - `maxWait (number)`: The maximum time to wait before triggering the function (default: undefined). | ||
| # Debounce Function Methods: | ||
| - `cancel`: Cancels the scheduled execution of the debounced function. | ||
| - `flush`: Executes the debounced function immediately. | ||
| ## throttle(fn, delay) | ||
@@ -47,3 +59,13 @@ | ||
| - `delay`: The minimum time between invocations in milliseconds. | ||
| - `options`: (Optional) Additional options for customization (e.g., { leading: false, trailing: true }). | ||
| # Throttle Function Options: | ||
| - `leading (boolean)`: Whether to execute the function on the leading edge of the timeout (default: true). | ||
| - `trailing (boolean)`: Whether to execute the function on the trailing edge of the timeout (default: true). | ||
| # Throttle Function Methods: | ||
| - `cancel`: Cancels the scheduled execution of the throttled function. | ||
| ## Examples | ||
@@ -58,1 +80,5 @@ | ||
| - [LinkedIn Profile](https://www.linkedin.com/in/muhammed-nabeel-b71279254/) | ||
| ## License | ||
| This project is licensed under the [MIT License](./LICENSE.md) - see the [LICENSE](./LICENSE.md) file for details. |
-21
| MIT License | ||
| Copyright (c) [2023] [Muhammed_Nabeel] | ||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
| The above copyright notice and this permission notice shall be included in | ||
| all copies or substantial portions of the Software. | ||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| THE SOFTWARE. |
12437
100.11%127
135.19%82
46.43%