Skip to content

Commit

Permalink
Require Node.js 10
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Feb 19, 2020
1 parent 689f847 commit f2f4ad8
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
language: node_js
node_js:
- '12'
- '10'
- '8'
6 changes: 4 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ declare namespace debounceFn {
readonly wait?: number;

/**
Trigger the function on leading edge of `wait` interval. For example, can be useful for preventing accidental double-clicks on a "submit" button from firing a second time.
Trigger the function on the leading edge of the `wait` interval.
For example, this can be useful for preventing accidental double-clicks on a "submit" button from firing a second time.
@default false
*/
readonly before?: boolean;

/**
Trigger the function on trailing edge of `wait` interval.
Trigger the function on the trailing edge of the `wait` interval.
@default true
*/
Expand Down
36 changes: 20 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
'use strict';
const mimicFn = require('mimic-fn');

module.exports = (fn, options = {}) => {
if (typeof fn !== 'function') {
throw new TypeError(`Expected the first argument to be a function, got \`${typeof fn}\``);
module.exports = (inputFunction, options = {}) => {
if (typeof inputFunction !== 'function') {
throw new TypeError(`Expected the first argument to be a function, got \`${typeof inputFunction}\``);
}

const before = (options.before === undefined) ? false : options.before;
const after = (options.after === undefined) ? true : options.after;
const {
wait = 0,
before = false,
after = true
} = options;

if (!before && !after) {
throw new Error('Both `before` and `after` are false, function wouldn\'t be called.');
Expand All @@ -16,35 +19,36 @@ module.exports = (fn, options = {}) => {
let timeout;
let result;

const debounced = function (...args) {
const debouncedFunction = function (...arguments_) {
const context = this;

const later = () => {
timeout = null;
timeout = undefined;

if (after) {
result = fn.apply(context, args);
result = inputFunction.apply(context, arguments_);
}
};

const callNow = before && !timeout;
const shouldCallNow = before && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, options.wait || 0);
timeout = setTimeout(later, wait);

if (callNow) {
result = fn.apply(context, args);
if (shouldCallNow) {
result = inputFunction.apply(context, arguments_);
}

return result;
};

mimicFn(debounced, fn);
mimicFn(debouncedFunction, inputFunction);

debounced.cancel = () => {
debouncedFunction.cancel = () => {
if (timeout) {
clearTimeout(timeout);
timeout = null;
timeout = undefined;
}
};

return debounced;
return debouncedFunction;
};
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)

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:

Expand Down
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
"description": "Debounce a function",
"license": "MIT",
"repository": "sindresorhus/debounce-fn",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
"url": "https://sindresorhus.com"
},
"engines": {
"node": ">=8"
"node": ">=10"
},
"scripts": {
"test": "xo && ava && tsd"
Expand All @@ -30,12 +31,12 @@
"invoked"
],
"dependencies": {
"mimic-fn": "^2.1.0"
"mimic-fn": "^3.0.0"
},
"devDependencies": {
"ava": "^1.4.1",
"delay": "^4.2.0",
"tsd": "^0.7.2",
"xo": "^0.24.0"
"tsd": "^0.11.0",
"xo": "^0.26.1"
}
}
24 changes: 9 additions & 15 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

> [Debounce](https://davidwalsh.name/javascript-debounce-function) a function

## Install

```
$ npm install debounce-fn
```


## Usage

```js
Expand All @@ -20,10 +18,9 @@ window.onresize = debounceFn(() => {
}, {wait: 100});
```


## API

### debounceFn(input, [options])
### debounceFn(input, options?)

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.

Expand All @@ -37,34 +34,31 @@ Function to debounce.

#### options

Type: `Object`
Type: `object`

##### wait

Type: `number`<br>
Type: `number`\
Default: `0`

Time to wait until the `input` function is called.

##### before

Type: `boolean`<br>
Type: `boolean`\
Default: `false`

Trigger the function on leading edge of `wait` interval. For example, can be useful for preventing accidental double-clicks on a "submit" button from firing a second time.
Trigger the function on the leading edge of the `wait` interval.

For example, can be useful for preventing accidental double-clicks on a "submit" button from firing a second time.

##### after

Type: `boolean`<br>
Type: `boolean`\
Default: `true`

Trigger the function on trailing edge of `wait` interval.
Trigger the function on the trailing edge of the `wait` interval.

## Related

- [p-debounce](https://github.com/sindresorhus/p-debounce) - Debounce promise-returning & async functions


## License

MIT © [Sindre Sorhus](https://sindresorhus.com)
14 changes: 9 additions & 5 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,15 @@ test('debounces a function', async t => {
});

test('before:false after:false options', t => {
t.throws(() => debounceFn(() => null, {
wait: 20,
before: false,
after: false
}), 'Both `before` and `after` are false, function wouldn\'t be called.');
t.throws(() => {
debounceFn(() => null, {
wait: 20,
before: false,
after: false
});
}, {
message: 'Both `before` and `after` are false, function wouldn\'t be called.'
});
});

test('before:true after:false options', async t => {
Expand Down

0 comments on commit f2f4ad8

Please sign in to comment.