Skip to content

Commit

Permalink
Refactor TypeScript definition to CommonJS compatible export (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
BendingBender authored and sindresorhus committed Apr 5, 2019
1 parent 7a7706e commit 8736973
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 24 deletions.
69 changes: 50 additions & 19 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,52 @@
export interface Options {
/**
* Call the `fn` on the [leading edge of the timeout](https://css-tricks.com/debouncing-throttling-explained-examples/#article-header-id-1). Meaning immediately, instead of waiting for `wait` milliseconds.
*
* @default false
*/
readonly leading?: boolean;
declare namespace pDebounce {
interface Options {
/**
Call the `fn` on the [leading edge of the timeout](https://css-tricks.com/debouncing-throttling-explained-examples/#article-header-id-1). Meaning immediately, instead of waiting for `wait` milliseconds.
@default false
*/
readonly leading?: boolean;
}
}

/**
* [Debounce](https://css-tricks.com/debouncing-throttling-explained-examples/) promise-returning & async functions.
*
* @param fn - Promise-returning/async function to debounce.
* @param wait - Milliseconds to wait before calling `fn`.
* @returns Returns a function that delays calling `fn` until after `wait` milliseconds have elapsed since the last time it was called.
*/
export default function pDebounce<ArgumentsType extends unknown[], ReturnType>(
fn: (...arguments: ArgumentsType) => PromiseLike<ReturnType> | ReturnType,
wait: number,
options?: Options
): (...arguments: ArgumentsType) => Promise<ReturnType>;
declare const pDebounce: {
/**
[Debounce](https://css-tricks.com/debouncing-throttling-explained-examples/) promise-returning & async functions.
@param fn - Promise-returning/async function to debounce.
@param wait - Milliseconds to wait before calling `fn`.
@returns Returns a function that delays calling `fn` until after `wait` milliseconds have elapsed since the last time it was called.
@example
```
import pDebounce = require('p-debounce');
const expensiveCall = async input => input;
const debouncedFn = pDebounce(expensiveCall, 200);
for (const i of [1, 2, 3]) {
debouncedFn(i).then(console.log);
}
//=> 3
//=> 3
//=> 3
```
*/
<ArgumentsType extends unknown[], ReturnType>(
fn: (...arguments: ArgumentsType) => PromiseLike<ReturnType> | ReturnType,
wait: number,
options?: pDebounce.Options
): (...arguments: ArgumentsType) => Promise<ReturnType>;

// TODO: Remove this for the next major release, refactor the whole definition to:
// declare function pDebounce<ArgumentsType extends unknown[], ReturnType>(
// fn: (...arguments: ArgumentsType) => PromiseLike<ReturnType> | ReturnType,
// wait: number,
// options?: pDebounce.Options
// ): (...arguments: ArgumentsType) => Promise<ReturnType>;
// export = pDebounce;
default: typeof pDebounce;
};

export = pDebounce;
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ const pDebounce = (fn, wait, options = {}) => {
};

module.exports = pDebounce;
// TODO: Remove this for the next major release
module.exports.default = pDebounce;
4 changes: 2 additions & 2 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expectType} from 'tsd-check';
import pDebounce from '.';
import {expectType} from 'tsd';
import pDebounce = require('.');

const expensiveCall = async (input: number) => input;

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"node": ">=6"
},
"scripts": {
"test": "xo && ava && tsd-check"
"test": "xo && ava && tsd"
},
"files": [
"index.js",
Expand Down Expand Up @@ -41,11 +41,11 @@
"bluebird"
],
"devDependencies": {
"ava": "^1.3.1",
"ava": "^1.4.1",
"delay": "^4.1.0",
"in-range": "^1.0.0",
"time-span": "^3.0.0",
"tsd-check": "^0.3.0",
"tsd": "^0.7.2",
"xo": "^0.24.0"
}
}

0 comments on commit 8736973

Please sign in to comment.