Skip to content

Commit

Permalink
Refactor TypeScript definition to CommonJS compatible export (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
BendingBender authored and sindresorhus committed Apr 6, 2019
1 parent 05609e0 commit b734458
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 50 deletions.
82 changes: 48 additions & 34 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,57 @@
export class AbortError extends Error {
declare class AbortErrorClass extends Error {
readonly name: 'AbortError';

/**
* Abort pending execution. All unresolved promised are rejected with a `AbortError` error.
*/
Abort pending execution. All unresolved promised are rejected with a `AbortError` error.
*/
constructor();
}

export type ThrottledFunction<Arguments extends unknown[], Return> = ((
...arguments: Arguments
) => Promise<Return>) & {
declare namespace pThrottle {
type ThrottledFunction<Arguments extends unknown[], Return> = ((
...arguments: Arguments
) => Promise<Return>) & {
/**
Abort pending executions. All unresolved promises are rejected with a `pThrottle.AbortError` error.
*/
abort(): void;
};

type AbortError = AbortErrorClass;
}

declare const pThrottle: {
/**
* Abort pending executions. All unresolved promises are rejected with a `pThrottle.AbortError` error.
*/
abort(): void;
[Throttle](https://css-tricks.com/debouncing-throttling-explained-examples/) promise-returning/async/normal functions.
@param fn - Promise-returning/async function or a normal function.
@param limit - Maximum number of calls within an `interval`.
@param interval - Timespan for `limit` in milliseconds.
@returns A throttled version of `fn`.
@example
```
import pThrottle from 'p-throttle';
const throttled = pThrottle(async index => {
return index * 2;
}, 2, 1000);
for (let i = 1; i <= 6; i++) {
throttled(i).then(console.log);
}
```
*/
<Arguments extends unknown[], Return>(
fn: (...arguments: Arguments) => PromiseLike<Return> | Return,
limit: number,
interval: number
): pThrottle.ThrottledFunction<Arguments, Return>;

AbortError: typeof AbortErrorClass;

// TODO: Remove this for the next major release
default: typeof pThrottle;
};

/**
* [Throttle](https://css-tricks.com/debouncing-throttling-explained-examples/) promise-returning/async/normal functions.
*
* @param fn - Promise-returning/async function or a normal function.
* @param limit - Maximum number of calls within an `interval`.
* @param interval - Timespan for `limit` in milliseconds.
* @returns A throttled version of `fn`.
*
* @example
*
* import pThrottle from 'p-throttle';
*
* const throttled = pThrottle(async index => {
* return index * 2;
* }, 2, 1000);
*
* for (let i = 1; i <= 6; i++) {
* throttled(i).then(console.log);
* }
*/
export default function<Arguments extends unknown[], Return>(
fn: (...arguments: Arguments) => PromiseLike<Return> | Return,
limit: number,
interval: number
): ThrottledFunction<Arguments, Return>;
export = pThrottle;
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,6 @@ const pThrottle = (fn, limit, interval) => {
};

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

const throttledUnicorn = pThrottle(
(index: string) => '🦄',
1,
1000
);
const throttledUnicorn = pThrottle((index: string) => '🦄', 1, 1000);

const throttledLazyUnicorn = pThrottle(
async (index: string) => '🦄',
1,
1000
);
const throttledLazyUnicorn = pThrottle(async (index: string) => '🦄', 1, 1000);

expectType<Error>(new AbortError());
expectType<AbortError>(new AbortError());

expectType<ThrottledFunction<[string], string>>(throttledUnicorn);
expectType<ThrottledFunction<[string], string>>(throttledLazyUnicorn);
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 All @@ -40,11 +40,11 @@
"bluebird"
],
"devDependencies": {
"ava": "^1.2.1",
"ava": "^1.4.1",
"delay": "^4.1.0",
"in-range": "^1.0.0",
"time-span": "^2.0.0",
"tsd-check": "^0.3.0",
"tsd": "^0.7.2",
"xo": "^0.24.0"
}
}

0 comments on commit b734458

Please sign in to comment.