From c441df954e9b2f3114a302a8decd13ec458cd754 Mon Sep 17 00:00:00 2001 From: Dimitri Benin Date: Tue, 8 Aug 2017 23:09:01 +0200 Subject: [PATCH] [p-throttle] introduce typings (#18675) --- types/p-throttle/index.d.ts | 47 ++++++++++++++++++++++++++++ types/p-throttle/p-throttle-tests.ts | 47 ++++++++++++++++++++++++++++ types/p-throttle/tsconfig.json | 22 +++++++++++++ types/p-throttle/tslint.json | 1 + 4 files changed, 117 insertions(+) create mode 100644 types/p-throttle/index.d.ts create mode 100644 types/p-throttle/p-throttle-tests.ts create mode 100644 types/p-throttle/tsconfig.json create mode 100644 types/p-throttle/tslint.json diff --git a/types/p-throttle/index.d.ts b/types/p-throttle/index.d.ts new file mode 100644 index 0000000000..ae331e1ca2 --- /dev/null +++ b/types/p-throttle/index.d.ts @@ -0,0 +1,47 @@ +// Type definitions for p-throttle 1.1 +// Project: https://github.com/sindresorhus/p-throttle#readme +// Definitions by: BendingBender +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +export = pThrottle; + +declare function pThrottle(fn: () => PromiseLike | R, limit: number, interval: number): (() => Promise) & { abort(): void; }; +declare function pThrottle(fn: (arg1: T1) => PromiseLike | R, limit: number, interval: number): ((arg1: T1) => Promise) & { abort(): void; }; +declare function pThrottle(fn: (arg1: T1, arg2: T2) => PromiseLike | R, limit: number, interval: number): ((arg1: T1, arg2: T2) => Promise) & { abort(): void; }; +declare function pThrottle(fn: (arg1: T1, arg2: T2, arg3: T3) => PromiseLike | R, + limit: number, + interval: number): ((arg1: T1, arg2: T2, arg3: T3) => Promise) & { abort(): void; }; +declare function pThrottle(fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4) => PromiseLike | R, + limit: number, + interval: number): ((arg1: T1, arg2: T2, arg3: T3, arg4: T4) => Promise) & { abort(): void; }; +declare function pThrottle(fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5) => PromiseLike | R, + limit: number, + interval: number): ((arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5) => Promise) & { abort(): void; }; +declare function pThrottle(fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6) => PromiseLike | R, + limit: number, + interval: number): ((arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6) => Promise) & { abort(): void; }; +declare function pThrottle( + fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7) => PromiseLike | R, + limit: number, + interval: number): ((arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7) => Promise) & { abort(): void; }; +declare function pThrottle( + fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7, arg8: T8) => PromiseLike | R, + limit: number, + interval: number): ((arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7, arg8: T8) => Promise) & { abort(): void; }; +declare function pThrottle( + fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7, arg8: T8, arg9: T9) => PromiseLike | R, + limit: number, + interval: number): ((arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7, arg8: T8, arg9: T9) => Promise) & { abort(): void; }; +declare function pThrottle( + fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7, arg8: T8, arg9: T9, arg10: T10) => PromiseLike | R, + limit: number, + interval: number): ((arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7, arg8: T8, arg9: T9, arg10: T10) => Promise) & { abort(): void; }; +declare function pThrottle(fn: (...args: any[]) => PromiseLike | R, limit: number, interval: number): ((...args: any[]) => Promise) & { abort(): void; }; + +declare namespace pThrottle { + class AbortError extends Error { + readonly name: 'AbortError'; + + constructor(); + } +} diff --git a/types/p-throttle/p-throttle-tests.ts b/types/p-throttle/p-throttle-tests.ts new file mode 100644 index 0000000000..713d445e6b --- /dev/null +++ b/types/p-throttle/p-throttle-tests.ts @@ -0,0 +1,47 @@ +import pThrottle = require('p-throttle'); + +const now = Date.now(); + +const throttled = pThrottle((i: number) => { + const secDiff = ((Date.now() - now) / 1000).toFixed(); + return Promise.resolve(`${i}: ${secDiff}s`); +}, 2, 1000); + +for (let i = 1; i <= 6; i++) { + throttled(i).then(res => { + const str: string = res; + }); +} + +throttled.abort(); + +pThrottle(() => true, 2, 3).abort(); +pThrottle(() => true, 2, 3)(); +pThrottle((n: number, s: string) => true, 2, 3).abort(); +pThrottle((n: number, s: string) => true, 2, 3)(1, 's'); +pThrottle((n: number, s: string, b: boolean) => true, 2, 3).abort(); +pThrottle((n: number, s: string, b: boolean) => true, 2, 3)(1, 's', true); +pThrottle((n: number, s: string, b: boolean, n2: number) => true, 2, 3).abort(); +pThrottle((n: number, s: string, b: boolean, n2: number) => true, 2, 3)(1, 's', true, 1); +pThrottle((n: number, s: string, b: boolean, n2: number, s2: string) => true, 2, 3).abort(); +pThrottle((n: number, s: string, b: boolean, n2: number, s2: string) => true, 2, 3)(1, 's', true, 1, 's'); +pThrottle((n: number, s: string, b: boolean, n2: number, s2: string, b2: boolean) => true, 2, 3).abort(); +pThrottle((n: number, s: string, b: boolean, n2: number, s2: string, b2: boolean) => true, 2, 3)(1, 's', true, 1, 's', false); +pThrottle((n: number, s: string, b: boolean, n2: number, s2: string, b2: boolean, n3: number) => true, 2, 3).abort(); +pThrottle((n: number, s: string, b: boolean, n2: number, s2: string, b2: boolean, n3: number) => true, 2, 3)(1, 's', true, 1, 's', false, 1); +pThrottle((n: number, s: string, b: boolean, n2: number, s2: string, b2: boolean, n3: number, n4: number) => true, 2, 3).abort(); +pThrottle((n: number, s: string, b: boolean, n2: number, s2: string, b2: boolean, n3: number, n4: number) => true, 2, 3)(1, 's', true, 1, 's', false, 1, 2); +pThrottle((n: number, s: string, b: boolean, n2: number, s2: string, b2: boolean, n3: number, n4: number, n5: number) => true, 2, 3).abort(); +pThrottle((n: number, s: string, b: boolean, n2: number, s2: string, b2: boolean, n3: number, n4: number, n5: number) => true, 2, 3)(1, 's', true, 1, 's', false, 1, 2, 3); +pThrottle((n: number, s: string, b: boolean, n2: number, s2: string, b2: boolean, n3: number, n4: number, n5: number, n6: number) => true, 2, 3).abort(); +pThrottle((n: number, s: string, b: boolean, n2: number, s2: string, b2: boolean, n3: number, n4: number, n5: number, n6: number) => true, 2, 3)(1, 's', true, 1, 's', false, 1, 2, 3, 4); +pThrottle( + (n: number, s: string, b: boolean, n2: number, s2: string, b2: boolean, n3: number, n4: number, n5: number, n6: number, s3: string) => true, + 2, + 3).abort(); +pThrottle( + (n: number, s: string, b: boolean, n2: number, s2: string, b2: boolean, n3: number, n4: number, n5: number, n6: number, s3: string) => true, + 2, + 3)(1, 's', true, 1, 's', false, 1, 2, 3, 4, 'as'); + +throw new pThrottle.AbortError(); diff --git a/types/p-throttle/tsconfig.json b/types/p-throttle/tsconfig.json new file mode 100644 index 0000000000..5344df0c1c --- /dev/null +++ b/types/p-throttle/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "p-throttle-tests.ts" + ] +} diff --git a/types/p-throttle/tslint.json b/types/p-throttle/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/p-throttle/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }