@common.js/debounce-fn 5.1.2 → 6.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/index.d.ts +11 -13
- package/index.js +7 -4
- package/package.json +10 -8
- package/readme.md +0 -73
package/README.md
CHANGED
|
@@ -2,4 +2,4 @@
|
|
|
2
2
|
|
|
3
3
|
The [debounce-fn](https://www.npmjs.com/package/debounce-fn) package exported as CommonJS modules.
|
|
4
4
|
|
|
5
|
-
Exported from [debounce-fn@
|
|
5
|
+
Exported from [debounce-fn@6.0.0](https://www.npmjs.com/package/debounce-fn/v/6.0.0) using https://github.com/etienne-martin/common.js.
|
package/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export
|
|
1
|
+
export type Options = {
|
|
2
2
|
/**
|
|
3
3
|
Time in milliseconds to wait until the `input` function is called.
|
|
4
4
|
|
|
@@ -30,21 +30,21 @@ export interface Options {
|
|
|
30
30
|
@default true
|
|
31
31
|
*/
|
|
32
32
|
readonly after?: boolean;
|
|
33
|
-
}
|
|
33
|
+
};
|
|
34
34
|
|
|
35
|
-
export
|
|
35
|
+
export type BeforeOptions = {
|
|
36
36
|
readonly before: true;
|
|
37
|
-
}
|
|
37
|
+
} & Options;
|
|
38
38
|
|
|
39
|
-
export
|
|
39
|
+
export type NoBeforeNoAfterOptions = {
|
|
40
40
|
readonly after: false;
|
|
41
41
|
readonly before?: false;
|
|
42
|
-
}
|
|
42
|
+
} & Options;
|
|
43
43
|
|
|
44
|
-
export
|
|
44
|
+
export type DebouncedFunction<ArgumentsType extends unknown[], ReturnType> = {
|
|
45
45
|
(...arguments: ArgumentsType): ReturnType;
|
|
46
46
|
cancel(): void;
|
|
47
|
-
}
|
|
47
|
+
};
|
|
48
48
|
|
|
49
49
|
/**
|
|
50
50
|
[Debounce](https://davidwalsh.name/javascript-debounce-function) a function.
|
|
@@ -63,19 +63,17 @@ window.onresize = debounceFn(() => {
|
|
|
63
63
|
}, {wait: 100});
|
|
64
64
|
```
|
|
65
65
|
*/
|
|
66
|
-
|
|
66
|
+
export default function debounceFn<ArgumentsType extends unknown[], ReturnType>(
|
|
67
67
|
input: (...arguments: ArgumentsType) => ReturnType,
|
|
68
68
|
options: BeforeOptions
|
|
69
69
|
): DebouncedFunction<ArgumentsType, ReturnType>;
|
|
70
70
|
|
|
71
|
-
|
|
71
|
+
export default function debounceFn<ArgumentsType extends unknown[], ReturnType>(
|
|
72
72
|
input: (...arguments: ArgumentsType) => ReturnType,
|
|
73
73
|
options: NoBeforeNoAfterOptions
|
|
74
74
|
): DebouncedFunction<ArgumentsType, undefined>;
|
|
75
75
|
|
|
76
|
-
|
|
76
|
+
export default function debounceFn<ArgumentsType extends unknown[], ReturnType>(
|
|
77
77
|
input: (...arguments: ArgumentsType) => ReturnType,
|
|
78
78
|
options?: Options
|
|
79
79
|
): DebouncedFunction<ArgumentsType, ReturnType | undefined>;
|
|
80
|
-
|
|
81
|
-
export default debounceFn;
|
package/index.js
CHANGED
|
@@ -8,7 +8,7 @@ Object.defineProperty(exports, "default", {
|
|
|
8
8
|
return _default;
|
|
9
9
|
}
|
|
10
10
|
});
|
|
11
|
-
var
|
|
11
|
+
var _mimicFunction = /*#__PURE__*/ _interopRequireDefault(require("mimic-function"));
|
|
12
12
|
function _interopRequireDefault(obj) {
|
|
13
13
|
return obj && obj.__esModule ? obj : {
|
|
14
14
|
default: obj
|
|
@@ -18,12 +18,15 @@ var _typeof = function(obj) {
|
|
|
18
18
|
"@swc/helpers - typeof";
|
|
19
19
|
return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
|
|
20
20
|
};
|
|
21
|
-
var
|
|
21
|
+
var debounceFunction = function(inputFunction) {
|
|
22
22
|
var options = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
|
|
23
23
|
if (typeof inputFunction !== "function") {
|
|
24
24
|
throw new TypeError("Expected the first argument to be a function, got `".concat(typeof inputFunction === "undefined" ? "undefined" : _typeof(inputFunction), "`"));
|
|
25
25
|
}
|
|
26
26
|
var _wait = options.wait, wait = _wait === void 0 ? 0 : _wait, _maxWait = options.maxWait, maxWait = _maxWait === void 0 ? Number.POSITIVE_INFINITY : _maxWait, _before = options.before, before = _before === void 0 ? false : _before, _after = options.after, after = _after === void 0 ? true : _after;
|
|
27
|
+
if (wait < 0 || maxWait < 0) {
|
|
28
|
+
throw new RangeError("`wait` and `maxWait` must not be negative.");
|
|
29
|
+
}
|
|
27
30
|
if (!before && !after) {
|
|
28
31
|
throw new Error("Both `before` and `after` are false, function wouldn't be called.");
|
|
29
32
|
}
|
|
@@ -66,7 +69,7 @@ var debounceFn = function(inputFunction) {
|
|
|
66
69
|
}
|
|
67
70
|
return result;
|
|
68
71
|
};
|
|
69
|
-
(0,
|
|
72
|
+
(0, _mimicFunction.default)(debouncedFunction, inputFunction);
|
|
70
73
|
debouncedFunction.cancel = function() {
|
|
71
74
|
if (timeout) {
|
|
72
75
|
clearTimeout(timeout);
|
|
@@ -79,4 +82,4 @@ var debounceFn = function(inputFunction) {
|
|
|
79
82
|
};
|
|
80
83
|
return debouncedFunction;
|
|
81
84
|
};
|
|
82
|
-
var _default =
|
|
85
|
+
var _default = debounceFunction;
|
package/package.json
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@common.js/debounce-fn",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "6.0.0",
|
|
4
|
+
"description": "debounce-fn package exported as CommonJS modules",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": "etienne-martin/common.js",
|
|
7
7
|
"funding": "https://github.com/sponsors/sindresorhus",
|
|
8
8
|
"type": "commonjs",
|
|
9
|
+
"sideEffects": false,
|
|
9
10
|
"engines": {
|
|
10
|
-
"node": ">=
|
|
11
|
+
"node": ">=18"
|
|
11
12
|
},
|
|
12
13
|
"scripts": {
|
|
13
14
|
"test": "xo && ava && tsd"
|
|
@@ -17,14 +18,15 @@
|
|
|
17
18
|
"index.d.ts"
|
|
18
19
|
],
|
|
19
20
|
"dependencies": {
|
|
20
|
-
"@common.js/mimic-
|
|
21
|
+
"@common.js/mimic-function": "^5.0.0"
|
|
21
22
|
},
|
|
22
23
|
"devDependencies": {
|
|
23
|
-
"ava": "^3.
|
|
24
|
-
"delay": "^
|
|
25
|
-
"tsd": "^0.
|
|
26
|
-
"xo": "^0.
|
|
24
|
+
"ava": "^5.3.1",
|
|
25
|
+
"delay": "^6.0.0",
|
|
26
|
+
"tsd": "^0.29.0",
|
|
27
|
+
"xo": "^0.56.0"
|
|
27
28
|
},
|
|
28
29
|
"homepage": "https://github.com/etienne-martin/common.js#readme",
|
|
30
|
+
"types": "./index.d.ts",
|
|
29
31
|
"main": "./index.js"
|
|
30
32
|
}
|
package/readme.md
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
# debounce-fn
|
|
2
|
-
|
|
3
|
-
> [Debounce](https://davidwalsh.name/javascript-debounce-function) a function
|
|
4
|
-
|
|
5
|
-
## Install
|
|
6
|
-
|
|
7
|
-
```sh
|
|
8
|
-
npm install debounce-fn
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
## Usage
|
|
12
|
-
|
|
13
|
-
```js
|
|
14
|
-
import debounceFn from 'debounce-fn';
|
|
15
|
-
|
|
16
|
-
window.onresize = debounceFn(() => {
|
|
17
|
-
// Do something on window resize
|
|
18
|
-
}, {wait: 100});
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
## API
|
|
22
|
-
|
|
23
|
-
### debounceFn(input, options?)
|
|
24
|
-
|
|
25
|
-
Returns a debounced function that delays calling the `input` function until after `wait` milliseconds have elapsed since the last time the debounced function was called.
|
|
26
|
-
|
|
27
|
-
It comes with a `.cancel()` method to cancel any scheduled `input` function calls.
|
|
28
|
-
|
|
29
|
-
#### input
|
|
30
|
-
|
|
31
|
-
Type: `Function`
|
|
32
|
-
|
|
33
|
-
Function to debounce.
|
|
34
|
-
|
|
35
|
-
#### options
|
|
36
|
-
|
|
37
|
-
Type: `object`
|
|
38
|
-
|
|
39
|
-
##### wait
|
|
40
|
-
|
|
41
|
-
Type: `number`\
|
|
42
|
-
Default: `0`
|
|
43
|
-
|
|
44
|
-
Time in milliseconds to wait until the `input` function is called.
|
|
45
|
-
|
|
46
|
-
##### maxWait
|
|
47
|
-
|
|
48
|
-
Type: `number`\
|
|
49
|
-
Default: `Infinity`
|
|
50
|
-
|
|
51
|
-
The maximum time the `input` function is allowed to be delayed before it's invoked.
|
|
52
|
-
|
|
53
|
-
This can be used to limit the number of calls handled in a constant stream. For example, a media player sending updates every few milliseconds but wants to be handled only once a second.
|
|
54
|
-
|
|
55
|
-
##### before
|
|
56
|
-
|
|
57
|
-
Type: `boolean`\
|
|
58
|
-
Default: `false`
|
|
59
|
-
|
|
60
|
-
Trigger the function on the leading edge of the `wait` interval.
|
|
61
|
-
|
|
62
|
-
For example, can be useful for preventing accidental double-clicks on a "submit" button from firing a second time.
|
|
63
|
-
|
|
64
|
-
##### after
|
|
65
|
-
|
|
66
|
-
Type: `boolean`\
|
|
67
|
-
Default: `true`
|
|
68
|
-
|
|
69
|
-
Trigger the function on the trailing edge of the `wait` interval.
|
|
70
|
-
|
|
71
|
-
## Related
|
|
72
|
-
|
|
73
|
-
- [p-debounce](https://github.com/sindresorhus/p-debounce) - Debounce promise-returning & async functions
|