Skip to content

Commit

Permalink
Return undefined instead of null when the path cannot be found
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Apr 18, 2019
1 parent 905e612 commit a7a9500
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 100 deletions.
14 changes: 6 additions & 8 deletions index.d.ts
Expand Up @@ -13,8 +13,8 @@ declare const findUp: {
/**
Find a file or directory by walking up parent directories.
@param filename - Filename of the file or an array of files to find.
@returns Either the first filepath found (by respecting the order of `filename`s) or `null`.
@param name - Name of the file or directory to find. Can be multiple.
@returns The first path found (by respecting the order of `name`s) or `undefined` if none could be found.
@example
```
Expand All @@ -39,17 +39,15 @@ declare const findUp: {
})();
```
*/
(filename: string | string[], options?: findUp.Options): Promise<
string | null
>;
(name: string | string[], options?: findUp.Options): Promise<string | undefined>;

/**
Synchronously find a file or directory by walking up parent directories.
@param filename - Filename of the file or an array of files to find.
@returns Either the first filepath found (by respecting the order of `filename`s) or `null`.
@param name - Name of the file or directory to find. Can be multiple.
@returns The first path found (by respecting the order of `names`s) or `undefined` if none could be found.
*/
sync(filename: string | string[], options?: findUp.Options): string | null;
sync(name: string | string[], options?: findUp.Options): string | undefined;
};

export = findUp;
24 changes: 12 additions & 12 deletions index.js
Expand Up @@ -2,19 +2,19 @@
const path = require('path');
const locatePath = require('locate-path');

module.exports = (filename, options = {}) => {
module.exports = (name, options = {}) => {
const startDirectory = path.resolve(options.cwd || '');
const {root} = path.parse(startDirectory);

const filenames = [].concat(filename);
const paths = [].concat(name);

return new Promise(resolve => {
(function find(directory) {
locatePath(filenames, {cwd: directory}).then(file => {
if (file) {
resolve(path.join(directory, file));
locatePath(paths, {cwd: directory}).then(foundPath => {
if (foundPath) {
resolve(path.join(directory, foundPath));
} else if (directory === root) {
resolve(null);
resolve();
} else {
find(path.dirname(directory));
}
Expand All @@ -23,22 +23,22 @@ module.exports = (filename, options = {}) => {
});
};

module.exports.sync = (filename, options = {}) => {
module.exports.sync = (name, options = {}) => {
let directory = path.resolve(options.cwd || '');
const {root} = path.parse(directory);

const filenames = [].concat(filename);
const paths = [].concat(name);

// eslint-disable-next-line no-constant-condition
while (true) {
const file = locatePath.sync(filenames, {cwd: directory});
const foundPath = locatePath.sync(paths, {cwd: directory});

if (file) {
return path.join(directory, file);
if (foundPath) {
return path.join(directory, foundPath);
}

if (directory === root) {
return null;
return;
}

directory = path.dirname(directory);
Expand Down
20 changes: 8 additions & 12 deletions index.test-d.ts
@@ -1,16 +1,12 @@
import {expectType} from 'tsd';
import findUp = require('.');

expectType<Promise<string | null>>(findUp('unicorn.png'));
expectType<Promise<string | null>>(findUp('unicorn.png', {cwd: ''}));
expectType<Promise<string | null>>(findUp(['rainbow.png', 'unicorn.png']));
expectType<Promise<string | null>>(
findUp(['rainbow.png', 'unicorn.png'], {cwd: ''})
);
expectType<Promise<string | undefined>>(findUp('unicorn.png'));
expectType<Promise<string | undefined>>(findUp('unicorn.png', {cwd: ''}));
expectType<Promise<string | undefined>>(findUp(['rainbow.png', 'unicorn.png']));
expectType<Promise<string | undefined>>(findUp(['rainbow.png', 'unicorn.png'], {cwd: ''}));

expectType<string | null>(findUp.sync('unicorn.png'));
expectType<string | null>(findUp.sync('unicorn.png', {cwd: ''}));
expectType<string | null>(findUp.sync(['rainbow.png', 'unicorn.png']));
expectType<string | null>(
findUp.sync(['rainbow.png', 'unicorn.png'], {cwd: ''})
);
expectType<string | undefined>(findUp.sync('unicorn.png'));
expectType<string | undefined>(findUp.sync('unicorn.png', {cwd: ''}));
expectType<string | undefined>(findUp.sync(['rainbow.png', 'unicorn.png']));
expectType<string | undefined>(findUp.sync(['rainbow.png', 'unicorn.png'], {cwd: ''}));
22 changes: 11 additions & 11 deletions readme.md
Expand Up @@ -53,31 +53,31 @@ const findUp = require('find-up');

## API

### findUp(filename, [options])
### findUp(name, [options])

Returns a `Promise` for either the filepath or `null` if it couldn't be found.
Returns a `Promise` for either the path or `undefined` if it couldn't be found.

### findUp([filenameA, filenameB], [options])
### findUp([nameA, nameB], [options])

Returns a `Promise` for either the first filepath found (by respecting the order) or `null` if none could be found.
Returns a `Promise` for either the first path found (by respecting the order) or `undefined` if none could be found.

### findUp.sync(filename, [options])
### findUp.sync(name, [options])

Returns a filepath or `null`.
Returns a path or `undefined` if it couldn't be found.

### findUp.sync([filenameA, filenameB], [options])
### findUp.sync([nameA, nameB], [options])

Returns the first filepath found (by respecting the order) or `null`.
Returns the first path found (by respecting the order) or `undefined` if none could be found.

#### filename
#### name

Type: `string`

Filename of the file to find.
Name of the file or directory to find.

#### options

Type: `Object`
Type: `object`

##### cwd

Expand Down

0 comments on commit a7a9500

Please sign in to comment.