Skip to content

Commit

Permalink
Add TypeScript definition (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
BendingBender authored and sindresorhus committed Apr 15, 2019
1 parent d6f19ff commit 133f583
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 11 deletions.
69 changes: 69 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
declare namespace locatePath {
interface Options {
/**
Current working directory.
@default process.cwd()
*/
readonly cwd?: string;
}

interface AsyncOptions extends Options {
/**
Number of concurrently pending promises. Minimum: `1`.
@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;
}
}

declare const locatePath: {
/**
Get the first path that exists on disk of multiple paths.
@param input - Paths to check.
@returns The first path that exists or `undefined` if none exists.
@example
```
import locatePath = require('locate-path');
const files = [
'unicorn.png',
'rainbow.png', // Only this one actually exists on disk
'pony.png'
];
(async () => {
console(await locatePath(files));
//=> 'rainbow'
})();
```
*/
(input: Iterable<string>, options?: locatePath.AsyncOptions): Promise<
string | undefined
>;

/**
Synchronously get the first path that exists on disk of multiple paths.
@param input - Paths to check.
@returns The first path that exists or `undefined` if none exists.
*/
sync(
input: Iterable<string>,
options?: locatePath.Options
): string | undefined;
};

export = locatePath;
16 changes: 16 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {expectType} from 'tsd';
import locatePath = require('.');

const files = [
'unicorn.png',
'rainbow.png',
'pony.png'
];

expectType<Promise<string | undefined>>(locatePath(files));
expectType<Promise<string | undefined>>(locatePath(files, {concurrency: 2}));
expectType<Promise<string | undefined>>(locatePath(files, {preserveOrder: false}));
expectType<Promise<string | undefined>>(locatePath(files, {cwd: '.'}));

expectType<string | undefined>(locatePath.sync(files));
expectType<string | undefined>(locatePath.sync(files, {cwd: '.'}));
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
"node": ">=6"
},
"scripts": {
"test": "xo && ava"
"test": "xo && ava && tsd"
},
"files": [
"index.js"
"index.js",
"index.d.ts"
],
"keywords": [
"locate",
Expand All @@ -38,7 +39,8 @@
"path-exists": "^3.0.0"
},
"devDependencies": {
"ava": "*",
"xo": "*"
"ava": "^1.4.1",
"tsd": "^0.7.2",
"xo": "^0.24.0"
}
}
14 changes: 7 additions & 7 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import m from '.';
import locatePath from '.';

const input = [
'noop.foo',
Expand All @@ -9,13 +9,13 @@ const input = [
];

test('async', async t => {
t.is(await m(input), 'index.js');
t.is(await m(['nonexistant']), undefined);
t.is(await m(['noop', 'unicorn'], {cwd: 'fixture'}), 'unicorn');
t.is(await locatePath(input), 'index.js');
t.is(await locatePath(['nonexistant']), undefined);
t.is(await locatePath(['noop', 'unicorn'], {cwd: 'fixture'}), 'unicorn');
});

test('sync', t => {
t.is(m.sync(input), 'index.js');
t.is(m.sync(['nonexistant']), undefined);
t.is(m.sync(['noop', 'unicorn'], {cwd: 'fixture'}), 'unicorn');
t.is(locatePath.sync(input), 'index.js');
t.is(locatePath.sync(['nonexistant']), undefined);
t.is(locatePath.sync(['noop', 'unicorn'], {cwd: 'fixture'}), 'unicorn');
});

0 comments on commit 133f583

Please sign in to comment.