debounce-throttle-utility 1.0.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/index.d.ts +7 -0
- package/dist/index.js +37 -0
- package/jest.config.ts +8 -0
- package/package.json +19 -4
- package/src/index.ts +51 -0
- package/{test.spec.js → test.test.ts} +2 -4
- package/tsconfig.json +12 -0
- package/index.js +0 -52
package/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Debounce & Throttle Utility [(
|
1
|
+
# Debounce & Throttle Utility [(NPM)](https://www.npmjs.com/package/debounce-throttle-utility)
|
2
2
|
|
3
3
|
A lightweight utility for debouncing and throttling functions, optimized for performance. Ideal for handling events like scrolling, resizing, or input changes in web apps.
|
4
4
|
|
package/dist/index.d.ts
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
interface DebounceOptions {
|
2
|
+
leading?: boolean;
|
3
|
+
trailing?: boolean;
|
4
|
+
}
|
5
|
+
export declare function debounce<T extends (...args: any[]) => any>(func: T, wait: number, options?: DebounceOptions): (...args: Parameters<T>) => void;
|
6
|
+
export declare function throttle<T extends (...args: any[]) => any>(func: T, limit: number): (...args: Parameters<T>) => void;
|
7
|
+
export {};
|
package/dist/index.js
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
"use strict";
|
2
|
+
// index.ts
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
4
|
+
exports.debounce = debounce;
|
5
|
+
exports.throttle = throttle;
|
6
|
+
function debounce(func, wait, options = {}) {
|
7
|
+
let timeout;
|
8
|
+
const { leading = false, trailing = true } = options;
|
9
|
+
return function (...args) {
|
10
|
+
const context = this;
|
11
|
+
if (timeout !== undefined) {
|
12
|
+
clearTimeout(timeout);
|
13
|
+
}
|
14
|
+
if (leading && timeout === undefined) {
|
15
|
+
func.apply(context, args);
|
16
|
+
}
|
17
|
+
timeout = setTimeout(() => {
|
18
|
+
if (trailing) {
|
19
|
+
func.apply(context, args);
|
20
|
+
}
|
21
|
+
timeout = undefined;
|
22
|
+
}, wait);
|
23
|
+
};
|
24
|
+
}
|
25
|
+
function throttle(func, limit) {
|
26
|
+
let inThrottle = false;
|
27
|
+
return function (...args) {
|
28
|
+
const context = this;
|
29
|
+
if (!inThrottle) {
|
30
|
+
func.apply(context, args);
|
31
|
+
inThrottle = true;
|
32
|
+
setTimeout(() => {
|
33
|
+
inThrottle = false;
|
34
|
+
}, limit);
|
35
|
+
}
|
36
|
+
};
|
37
|
+
}
|
package/jest.config.ts
ADDED
package/package.json
CHANGED
@@ -1,16 +1,26 @@
|
|
1
1
|
{
|
2
2
|
"name": "debounce-throttle-utility",
|
3
|
-
"version": "
|
3
|
+
"version": "2.0.0",
|
4
4
|
"description": "A lightweight utility for debouncing and throttling functions, optimized for performance.",
|
5
|
-
"main": "index.js",
|
5
|
+
"main": "dist/index.js",
|
6
6
|
"scripts": {
|
7
|
+
"build": "tsc",
|
7
8
|
"test": "jest"
|
8
9
|
},
|
9
10
|
"repository": {
|
10
11
|
"type": "git",
|
11
12
|
"url": "git+https://github.com/paradoxial-composition/debounce-throttle-utility.git"
|
12
13
|
},
|
13
|
-
"keywords": [
|
14
|
+
"keywords": [
|
15
|
+
"debounce",
|
16
|
+
"throttle",
|
17
|
+
"utility",
|
18
|
+
"performance",
|
19
|
+
"scroll",
|
20
|
+
"input",
|
21
|
+
"resize",
|
22
|
+
"optimize"
|
23
|
+
],
|
14
24
|
"author": "elbani.othman@gmail.com",
|
15
25
|
"license": "MIT",
|
16
26
|
"bugs": {
|
@@ -18,6 +28,11 @@
|
|
18
28
|
},
|
19
29
|
"homepage": "https://github.com/paradoxial-composition/debounce-throttle-utility#readme",
|
20
30
|
"devDependencies": {
|
21
|
-
"jest": "^29.
|
31
|
+
"@types/jest": "^29.5.14",
|
32
|
+
"@types/node": "^22.13.5",
|
33
|
+
"jest": "^29.7.0",
|
34
|
+
"ts-jest": "^29.2.6",
|
35
|
+
"typescript": "^5.7.3",
|
36
|
+
"ts-node": "^10.9.2"
|
22
37
|
}
|
23
38
|
}
|
package/src/index.ts
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
// index.ts
|
2
|
+
|
3
|
+
type Timer = ReturnType<typeof setTimeout> | undefined;
|
4
|
+
|
5
|
+
interface DebounceOptions {
|
6
|
+
leading?: boolean;
|
7
|
+
trailing?: boolean;
|
8
|
+
}
|
9
|
+
|
10
|
+
export function debounce<T extends (...args: any[]) => any>(
|
11
|
+
func: T,
|
12
|
+
wait: number,
|
13
|
+
options: DebounceOptions = {}
|
14
|
+
): (...args: Parameters<T>) => void {
|
15
|
+
let timeout: Timer;
|
16
|
+
const { leading = false, trailing = true } = options;
|
17
|
+
|
18
|
+
return function (this: any, ...args: Parameters<T>): void {
|
19
|
+
const context = this;
|
20
|
+
if (timeout !== undefined) {
|
21
|
+
clearTimeout(timeout);
|
22
|
+
}
|
23
|
+
if (leading && timeout === undefined) {
|
24
|
+
func.apply(context, args);
|
25
|
+
}
|
26
|
+
timeout = setTimeout(() => {
|
27
|
+
if (trailing) {
|
28
|
+
func.apply(context, args);
|
29
|
+
}
|
30
|
+
timeout = undefined;
|
31
|
+
}, wait);
|
32
|
+
};
|
33
|
+
}
|
34
|
+
|
35
|
+
export function throttle<T extends (...args: any[]) => any>(
|
36
|
+
func: T,
|
37
|
+
limit: number
|
38
|
+
): (...args: Parameters<T>) => void {
|
39
|
+
let inThrottle = false;
|
40
|
+
|
41
|
+
return function (this: any, ...args: Parameters<T>): void {
|
42
|
+
const context = this;
|
43
|
+
if (!inThrottle) {
|
44
|
+
func.apply(context, args);
|
45
|
+
inThrottle = true;
|
46
|
+
setTimeout(() => {
|
47
|
+
inThrottle = false;
|
48
|
+
}, limit);
|
49
|
+
}
|
50
|
+
};
|
51
|
+
}
|
@@ -1,6 +1,4 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
const { debounce, throttle } = require('./index');
|
1
|
+
import { debounce, throttle } from './src/index';
|
4
2
|
|
5
3
|
describe('Debounce', () => {
|
6
4
|
it('should delay execution', async () => {
|
@@ -31,7 +29,7 @@ describe('Throttle', () => {
|
|
31
29
|
expect(count).toBe(1); // Should execute only once within 100ms
|
32
30
|
});
|
33
31
|
|
34
|
-
it('should be executed only twice in a 500ms interval
|
32
|
+
it('should be executed only twice in a 500ms interval', async () => {
|
35
33
|
let count = 0;
|
36
34
|
const throttled = throttle(() => {
|
37
35
|
count++;
|
package/tsconfig.json
ADDED
package/index.js
DELETED
@@ -1,52 +0,0 @@
|
|
1
|
-
// index.js
|
2
|
-
|
3
|
-
/**
|
4
|
-
* Debounce function
|
5
|
-
* @param {Function} func - The function to debounce
|
6
|
-
* @param {number} wait - Delay in milliseconds
|
7
|
-
* @param {Object} options - Optional configuration
|
8
|
-
* @returns {Function} - Debounced function
|
9
|
-
*/
|
10
|
-
function debounce(func, wait, options = {}) {
|
11
|
-
let timeout;
|
12
|
-
const { leading = false, trailing = true } = options;
|
13
|
-
|
14
|
-
return function(...args) {
|
15
|
-
const context = this;
|
16
|
-
clearTimeout(timeout);
|
17
|
-
|
18
|
-
if (leading && !timeout) {
|
19
|
-
func.apply(context, args);
|
20
|
-
}
|
21
|
-
|
22
|
-
timeout = setTimeout(() => {
|
23
|
-
if (trailing) {
|
24
|
-
func.apply(context, args);
|
25
|
-
}
|
26
|
-
}, wait);
|
27
|
-
};
|
28
|
-
}
|
29
|
-
|
30
|
-
/**
|
31
|
-
* Throttle function
|
32
|
-
* @param {Function} func - The function to throttle
|
33
|
-
* @param {number} limit - Interval in milliseconds
|
34
|
-
* @returns {Function} - Throttled function
|
35
|
-
*/
|
36
|
-
function throttle(func, limit) {
|
37
|
-
let inThrottle;
|
38
|
-
|
39
|
-
return function(...args) {
|
40
|
-
const context = this;
|
41
|
-
if (!inThrottle) {
|
42
|
-
func.apply(context, args);
|
43
|
-
inThrottle = true;
|
44
|
-
setTimeout(() => (inThrottle = false), limit);
|
45
|
-
}
|
46
|
-
};
|
47
|
-
}
|
48
|
-
|
49
|
-
module.exports = {
|
50
|
-
debounce,
|
51
|
-
throttle,
|
52
|
-
};
|