Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor TypeScript definition to CommonJS compatible export #4

Merged
merged 1 commit into from
Apr 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 61 additions & 27 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,64 @@
export interface Options {
/**
* Number of concurrently pending promises returned by `tester`. Minimum: `1`.
*
* @default Infinity
*/
readonly concurrency?: number;
declare namespace pLocate {
interface Options {
/**
Number of concurrently pending promises returned by `tester`. Minimum: `1`.

/**
* Preserve `input` order when searching.
*
* Disable this to improve performance if you don't care about the order.
*
* @default true
*/
readonly preserveOrder?: boolean;
@default Infinity
*/
readonly concurrency?: number;

/**
Preserve `input` order when searching.

Disable this to improve performance if you don't care about the order.

@default true
*/
readonly preserveOrder?: boolean;
}
}

/**
* Get the first fulfilled promise that satisfies the provided testing function.
*
* @param input - An iterable of promises/values to test.
* @param tester - This function will receive resolved values from `input` and is expected to return a `Promise<boolean>` or `boolean`.
* @returns A `Promise` that is fulfilled when `tester` resolves to `true` or the iterable is done, or rejects if any of the promises reject. The fulfilled value is the current iterable value or `undefined` if `tester` never resolved to `true`.
*/
export default function pLocate<ValueType>(
input: Iterable<PromiseLike<ValueType> | ValueType>,
tester: (element: ValueType) => PromiseLike<boolean> | boolean,
options?: Options
): Promise<ValueType | undefined>;
declare const pLocate: {
/**
Get the first fulfilled promise that satisfies the provided testing function.

@param input - An iterable of promises/values to test.
@param tester - This function will receive resolved values from `input` and is expected to return a `Promise<boolean>` or `boolean`.
@returns A `Promise` that is fulfilled when `tester` resolves to `true` or the iterable is done, or rejects if any of the promises reject. The fulfilled value is the current iterable value or `undefined` if `tester` never resolved to `true`.

@example
```
import pathExists = require('path-exists');
import pLocate = require('p-locate');

const files = [
'unicorn.png',
'rainbow.png', // Only this one actually exists on disk
'pony.png'
];

(async () => {
const foundPath = await pLocate(files, file => pathExists(file));

console.log(foundPath);
//=> 'rainbow'
})();
```
*/
<ValueType>(
input: Iterable<PromiseLike<ValueType> | ValueType>,
tester: (element: ValueType) => PromiseLike<boolean> | boolean,
options?: pLocate.Options
): Promise<ValueType | undefined>;

// TODO: Remove this for the next major release, refactor the whole definition to:
// declare function pLocate<ValueType>(
// input: Iterable<PromiseLike<ValueType> | ValueType>,
// tester: (element: ValueType) => PromiseLike<boolean> | boolean,
// options?: pLocate.Options
// ): Promise<ValueType | undefined>;
// export = pLocate;
default: typeof pLocate;
};

export = pLocate;
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,5 @@ const pLocate = async (iterable, tester, options) => {
};

module.exports = pLocate;
// TODO: Remove this for the next major release
module.exports.default = pLocate;
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 pLocate from '.';
import {expectType} from 'tsd';
import pLocate = require('.');

const files = new Set([
'unicorn.png',
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"node": ">=8"
},
"scripts": {
"test": "xo && ava && tsd-check"
"test": "xo && ava && tsd"
},
"files": [
"index.js",
Expand All @@ -40,14 +40,14 @@
"bluebird"
],
"dependencies": {
"p-limit": "^2.0.0"
"p-limit": "^2.2.0"
},
"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"
}
}