diff --git a/.travis.yml b/.travis.yml index f3fa8cd..94ab01f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,4 @@ language: node_js node_js: + - '12' - '10' - - '8' diff --git a/index.d.ts b/index.d.ts index d3fda0c..69ecbe2 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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 */ diff --git a/index.js b/index.js index 76d26d3..9450b4e 100644 --- a/index.js +++ b/index.js @@ -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.'); @@ -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; }; diff --git a/license b/license index e7af2f7..fa7ceba 100644 --- a/license +++ b/license @@ -1,6 +1,6 @@ MIT License -Copyright (c) Sindre Sorhus (sindresorhus.com) +Copyright (c) Sindre Sorhus (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: diff --git a/package.json b/package.json index c1c3a2f..9ba6acf 100644 --- a/package.json +++ b/package.json @@ -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" @@ -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" } } diff --git a/readme.md b/readme.md index 332ed1f..57534eb 100644 --- a/readme.md +++ b/readme.md @@ -2,14 +2,12 @@ > [Debounce](https://davidwalsh.name/javascript-debounce-function) a function - ## Install ``` $ npm install debounce-fn ``` - ## Usage ```js @@ -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. @@ -37,34 +34,31 @@ Function to debounce. #### options -Type: `Object` +Type: `object` ##### wait -Type: `number`
+Type: `number`\ Default: `0` Time to wait until the `input` function is called. ##### before -Type: `boolean`
+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`
+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) diff --git a/test.js b/test.js index 9c9a4b9..c2d00ac 100644 --- a/test.js +++ b/test.js @@ -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 => {