@common.js/debounce-fn 5.1.2

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 ADDED
@@ -0,0 +1,5 @@
1
+ # @common.js/debounce-fn
2
+
3
+ The [debounce-fn](https://www.npmjs.com/package/debounce-fn) package exported as CommonJS modules.
4
+
5
+ Exported from [debounce-fn@5.1.2](https://www.npmjs.com/package/debounce-fn/v/5.1.2) using https://github.com/etienne-martin/common.js.
package/index.d.ts ADDED
@@ -0,0 +1,81 @@
1
+ export interface Options {
2
+ /**
3
+ Time in milliseconds to wait until the `input` function is called.
4
+
5
+ @default 0
6
+ */
7
+ readonly wait?: number;
8
+
9
+ /**
10
+ The maximum time the `input` function is allowed to be delayed before it's invoked.
11
+
12
+ This can be used to control the rate 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.
13
+
14
+ @default Infinity
15
+ */
16
+ readonly maxWait?: number;
17
+
18
+ /**
19
+ Trigger the function on the leading edge of the `wait` interval.
20
+
21
+ For example, this can be useful for preventing accidental double-clicks on a "submit" button from firing a second time.
22
+
23
+ @default false
24
+ */
25
+ readonly before?: boolean;
26
+
27
+ /**
28
+ Trigger the function on the trailing edge of the `wait` interval.
29
+
30
+ @default true
31
+ */
32
+ readonly after?: boolean;
33
+ }
34
+
35
+ export interface BeforeOptions extends Options {
36
+ readonly before: true;
37
+ }
38
+
39
+ export interface NoBeforeNoAfterOptions extends Options {
40
+ readonly after: false;
41
+ readonly before?: false;
42
+ }
43
+
44
+ export interface DebouncedFunction<ArgumentsType extends unknown[], ReturnType> {
45
+ (...arguments: ArgumentsType): ReturnType;
46
+ cancel(): void;
47
+ }
48
+
49
+ /**
50
+ [Debounce](https://davidwalsh.name/javascript-debounce-function) a function.
51
+
52
+ @param input - Function to debounce.
53
+ @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.
54
+
55
+ It comes with a `.cancel()` method to cancel any scheduled `input` function calls.
56
+
57
+ @example
58
+ ```
59
+ import debounceFn from 'debounce-fn';
60
+
61
+ window.onresize = debounceFn(() => {
62
+ // Do something on window resize
63
+ }, {wait: 100});
64
+ ```
65
+ */
66
+ declare function debounceFn<ArgumentsType extends unknown[], ReturnType>(
67
+ input: (...arguments: ArgumentsType) => ReturnType,
68
+ options: BeforeOptions
69
+ ): DebouncedFunction<ArgumentsType, ReturnType>;
70
+
71
+ declare function debounceFn<ArgumentsType extends unknown[], ReturnType>(
72
+ input: (...arguments: ArgumentsType) => ReturnType,
73
+ options: NoBeforeNoAfterOptions
74
+ ): DebouncedFunction<ArgumentsType, undefined>;
75
+
76
+ declare function debounceFn<ArgumentsType extends unknown[], ReturnType>(
77
+ input: (...arguments: ArgumentsType) => ReturnType,
78
+ options?: Options
79
+ ): DebouncedFunction<ArgumentsType, ReturnType | undefined>;
80
+
81
+ export default debounceFn;
package/index.js ADDED
@@ -0,0 +1,82 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "default", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return _default;
9
+ }
10
+ });
11
+ var _mimicFn = /*#__PURE__*/ _interopRequireDefault(require("mimic-fn"));
12
+ function _interopRequireDefault(obj) {
13
+ return obj && obj.__esModule ? obj : {
14
+ default: obj
15
+ };
16
+ }
17
+ var _typeof = function(obj) {
18
+ "@swc/helpers - typeof";
19
+ return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
20
+ };
21
+ var debounceFn = function(inputFunction) {
22
+ var options = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
23
+ if (typeof inputFunction !== "function") {
24
+ throw new TypeError("Expected the first argument to be a function, got `".concat(typeof inputFunction === "undefined" ? "undefined" : _typeof(inputFunction), "`"));
25
+ }
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 (!before && !after) {
28
+ throw new Error("Both `before` and `after` are false, function wouldn't be called.");
29
+ }
30
+ var timeout;
31
+ var maxTimeout;
32
+ var result;
33
+ var debouncedFunction = function debouncedFunction() {
34
+ for(var _len = arguments.length, arguments_ = new Array(_len), _key = 0; _key < _len; _key++){
35
+ arguments_[_key] = arguments[_key];
36
+ }
37
+ var context = this; // eslint-disable-line unicorn/no-this-assignment
38
+ var later = function() {
39
+ timeout = undefined;
40
+ if (maxTimeout) {
41
+ clearTimeout(maxTimeout);
42
+ maxTimeout = undefined;
43
+ }
44
+ if (after) {
45
+ result = inputFunction.apply(context, arguments_);
46
+ }
47
+ };
48
+ var maxLater = function() {
49
+ maxTimeout = undefined;
50
+ if (timeout) {
51
+ clearTimeout(timeout);
52
+ timeout = undefined;
53
+ }
54
+ if (after) {
55
+ result = inputFunction.apply(context, arguments_);
56
+ }
57
+ };
58
+ var shouldCallNow = before && !timeout;
59
+ clearTimeout(timeout);
60
+ timeout = setTimeout(later, wait);
61
+ if (maxWait > 0 && maxWait !== Number.POSITIVE_INFINITY && !maxTimeout) {
62
+ maxTimeout = setTimeout(maxLater, maxWait);
63
+ }
64
+ if (shouldCallNow) {
65
+ result = inputFunction.apply(context, arguments_);
66
+ }
67
+ return result;
68
+ };
69
+ (0, _mimicFn.default)(debouncedFunction, inputFunction);
70
+ debouncedFunction.cancel = function() {
71
+ if (timeout) {
72
+ clearTimeout(timeout);
73
+ timeout = undefined;
74
+ }
75
+ if (maxTimeout) {
76
+ clearTimeout(maxTimeout);
77
+ maxTimeout = undefined;
78
+ }
79
+ };
80
+ return debouncedFunction;
81
+ };
82
+ var _default = debounceFn;
package/license ADDED
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
4
+
5
+ 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:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ 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.
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "@common.js/debounce-fn",
3
+ "version": "5.1.2",
4
+ "description": "Debounce a function",
5
+ "license": "MIT",
6
+ "repository": "etienne-martin/common.js",
7
+ "funding": "https://github.com/sponsors/sindresorhus",
8
+ "type": "commonjs",
9
+ "engines": {
10
+ "node": ">=12"
11
+ },
12
+ "scripts": {
13
+ "test": "xo && ava && tsd"
14
+ },
15
+ "files": [
16
+ "index.js",
17
+ "index.d.ts"
18
+ ],
19
+ "dependencies": {
20
+ "@common.js/mimic-fn": "^4.0.0"
21
+ },
22
+ "devDependencies": {
23
+ "ava": "^3.15.0",
24
+ "delay": "^5.0.0",
25
+ "tsd": "^0.19.1",
26
+ "xo": "^0.47.0"
27
+ },
28
+ "homepage": "https://github.com/etienne-martin/common.js#readme",
29
+ "main": "./index.js"
30
+ }
package/readme.md ADDED
@@ -0,0 +1,73 @@
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