Skip to content

Commit 485ff6e

Browse files
Initial commit
1 parent dc6f46e commit 485ff6e

File tree

5 files changed

+67
-70
lines changed

5 files changed

+67
-70
lines changed

README.md

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,27 @@
1-
# Node Package Template
2-
A basic npm package template.
3-
This is only meant for building packages and no standalone application, therefore it does not contain any startup scripts.
4-
5-
## Contents
6-
- TypeScript (for type safety)
7-
- Jest (for testing)
8-
- Github Packages (for publishing)
9-
- Github Test
1+
# Debounce Function
2+
3+
A TypeScript debounce function that can be used to limit the frequency of function calls.
4+
5+
## Usage
6+
7+
```typescript
8+
import { debounce } from '@garmingo/debounce';
9+
10+
const myFunction = () => {
11+
console.log('Debounced function called');
12+
};
13+
14+
const debouncedFunction = debounce(myFunction, 500); // Set the delay time in milliseconds
15+
16+
// Call debouncedFunction multiple times quickly
17+
debouncedFunction();
18+
debouncedFunction();
19+
debouncedFunction();
20+
21+
// Only one actual call to myFunction will be made after 500ms
22+
```
23+
24+
## Parameters
25+
26+
- `func`: The original function you want to debounce.
27+
- `delay`: The delay time in milliseconds.

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
{
2-
"name": "@garmingo/node-package-template",
2+
"name": "@garmingo/debounce",
33
"version": "1.0.0",
4-
"description": "A basic template for a node package with typescript and jest",
4+
"description": "A basic debounce function for TypeScript",
55
"main": "src/index.ts",
66
"type": "module",
77
"scripts": {
88
"test": "jest"
99
},
1010
"repository": {
1111
"type": "git",
12-
"url": "git+https://github.com/Garmingo/node-package-template.git"
12+
"url": "git+https://github.com/Garmingo/debounce.git"
1313
},
14-
"author": "Codineer Digital",
14+
"author": "Garmingo",
1515
"license": "UNLICENSED",
1616
"bugs": {
17-
"url": "https://github.com/Garmingo/node-package-template/issues"
17+
"url": "https://github.com/Garmingo/debounce/issues"
1818
},
19-
"homepage": "https://github.com/Garmingo/node-package-template#readme",
19+
"homepage": "https://github.com/Garmingo/debounce#readme",
2020
"devDependencies": {
2121
"@types/jest": "^29.5.8",
2222
"jest": "^29.7.0",

src/index.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,22 @@
66

77

88
/**
9-
* This is an example function
10-
* It returns the n-th fibonacci number
11-
* @param n The n-th fibonacci number to return
12-
* @returns The n-th fibonacci number
9+
* Executes only the last call of a function within a given delay.
10+
* @param func The function to debounce.
11+
* @param delay The delay in milliseconds.
12+
* @returns A function that can be called to execute the debounced function.
1313
*/
14-
export function fib(n: number): number {
15-
if (n < 2)
16-
return n;
17-
return fib(n - 1) + fib(n - 2);
18-
}
14+
export function debounce<T extends (...args: any[]) => any>(
15+
func: T,
16+
delay: number
17+
): (...args: Parameters<T>) => void {
18+
let timeoutId: NodeJS.Timeout;
19+
20+
return function (...args: Parameters<T>): void {
21+
clearTimeout(timeoutId);
22+
23+
timeoutId = setTimeout(() => {
24+
func(...args);
25+
}, delay);
26+
};
27+
}

tests/tokenizer.test.ts

Lines changed: 14 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,19 @@
33
* All rights reserved.
44
* Unauthorized use, reproduction, and distribution of this source code is strictly prohibited.
55
*/
6-
import { fib } from '../src/index';
6+
import { debounce } from '../src/index';
77

8-
test('Test the fibonacci function', () => {
9-
expect(fib(0)).toBe(0);
10-
expect(fib(1)).toBe(1);
11-
expect(fib(2)).toBe(1);
12-
expect(fib(3)).toBe(2);
13-
expect(fib(4)).toBe(3);
14-
expect(fib(5)).toBe(5);
15-
expect(fib(6)).toBe(8);
16-
expect(fib(7)).toBe(13);
17-
expect(fib(8)).toBe(21);
18-
expect(fib(9)).toBe(34);
19-
expect(fib(10)).toBe(55);
20-
expect(fib(11)).toBe(89);
21-
expect(fib(12)).toBe(144);
22-
expect(fib(13)).toBe(233);
23-
expect(fib(14)).toBe(377);
24-
expect(fib(15)).toBe(610);
25-
expect(fib(16)).toBe(987);
26-
expect(fib(17)).toBe(1597);
27-
expect(fib(18)).toBe(2584);
28-
expect(fib(19)).toBe(4181);
29-
expect(fib(20)).toBe(6765);
30-
expect(fib(21)).toBe(10946);
31-
expect(fib(22)).toBe(17711);
32-
expect(fib(23)).toBe(28657);
33-
expect(fib(24)).toBe(46368);
34-
expect(fib(25)).toBe(75025);
35-
expect(fib(26)).toBe(121393);
36-
expect(fib(27)).toBe(196418);
37-
expect(fib(28)).toBe(317811);
38-
expect(fib(29)).toBe(514229);
39-
expect(fib(30)).toBe(832040);
40-
expect(fib(31)).toBe(1346269);
41-
expect(fib(32)).toBe(2178309);
42-
expect(fib(33)).toBe(3524578);
43-
expect(fib(34)).toBe(5702887);
44-
expect(fib(35)).toBe(9227465);
45-
expect(fib(36)).toBe(14930352);
46-
expect(fib(37)).toBe(24157817);
47-
expect(fib(38)).toBe(39088169);
48-
expect(fib(39)).toBe(63245986);
49-
expect(fib(40)).toBe(102334155);
50-
expect(fib(41)).toBe(165580141);
8+
test('Test the debounce function', () => {
9+
const mockFn = jest.fn();
10+
const debouncedFn = debounce(mockFn, 1000);
11+
12+
debouncedFn();
13+
debouncedFn();
14+
debouncedFn();
15+
16+
expect(mockFn).not.toHaveBeenCalled();
17+
18+
setTimeout(() => {
19+
expect(mockFn).toHaveBeenCalledTimes(1);
20+
}, 1000);
5121
});

0 commit comments

Comments
 (0)