From b7344589fa03d81a54089d17e382cd95222d906f Mon Sep 17 00:00:00 2001 From: Dimitri Benin Date: Sat, 6 Apr 2019 15:27:29 +0000 Subject: [PATCH] Refactor TypeScript definition to CommonJS compatible export (#17) --- index.d.ts | 82 +++++++++++++++++++++++++++++-------------------- index.js | 1 + index.test-d.ts | 19 ++++-------- package.json | 6 ++-- 4 files changed, 58 insertions(+), 50 deletions(-) diff --git a/index.d.ts b/index.d.ts index 9e32f59..ea18b1f 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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: Arguments -) => Promise) & { +declare namespace pThrottle { + type ThrottledFunction = (( + ...arguments: Arguments + ) => Promise) & { + /** + 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); + } + ``` + */ + ( + fn: (...arguments: Arguments) => PromiseLike | Return, + limit: number, + interval: number + ): pThrottle.ThrottledFunction; + + 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( - fn: (...arguments: Arguments) => PromiseLike | Return, - limit: number, - interval: number -): ThrottledFunction; +export = pThrottle; diff --git a/index.js b/index.js index aae9447..bbd2f5f 100644 --- a/index.js +++ b/index.js @@ -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; diff --git a/index.test-d.ts b/index.test-d.ts index 4e9be40..f909ddf 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -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(new AbortError()); +expectType(new AbortError()); expectType>(throttledUnicorn); expectType>(throttledLazyUnicorn); diff --git a/package.json b/package.json index 1e3b048..f1a9b92 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "node": ">=6" }, "scripts": { - "test": "xo && ava && tsd-check" + "test": "xo && ava && tsd" }, "files": [ "index.js", @@ -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" } }