From a7a9500b1ed48628452976cc6303932f25ceb623 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Thu, 18 Apr 2019 17:45:38 +0700 Subject: [PATCH] Return `undefined` instead of `null` when the path cannot be found --- index.d.ts | 14 +++--- index.js | 24 +++++----- index.test-d.ts | 20 ++++----- readme.md | 22 +++++----- test.js | 114 ++++++++++++++++++++++++------------------------ 5 files changed, 94 insertions(+), 100 deletions(-) diff --git a/index.d.ts b/index.d.ts index d1e1aaf..426866c 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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 ``` @@ -39,17 +39,15 @@ declare const findUp: { })(); ``` */ - (filename: string | string[], options?: findUp.Options): Promise< - string | null - >; + (name: string | string[], options?: findUp.Options): Promise; /** 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; diff --git a/index.js b/index.js index de62b65..13fe765 100644 --- a/index.js +++ b/index.js @@ -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)); } @@ -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); diff --git a/index.test-d.ts b/index.test-d.ts index 6ae68d1..93e298f 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -1,16 +1,12 @@ import {expectType} from 'tsd'; import findUp = require('.'); -expectType>(findUp('unicorn.png')); -expectType>(findUp('unicorn.png', {cwd: ''})); -expectType>(findUp(['rainbow.png', 'unicorn.png'])); -expectType>( - findUp(['rainbow.png', 'unicorn.png'], {cwd: ''}) -); +expectType>(findUp('unicorn.png')); +expectType>(findUp('unicorn.png', {cwd: ''})); +expectType>(findUp(['rainbow.png', 'unicorn.png'])); +expectType>(findUp(['rainbow.png', 'unicorn.png'], {cwd: ''})); -expectType(findUp.sync('unicorn.png')); -expectType(findUp.sync('unicorn.png', {cwd: ''})); -expectType(findUp.sync(['rainbow.png', 'unicorn.png'])); -expectType( - findUp.sync(['rainbow.png', 'unicorn.png'], {cwd: ''}) -); +expectType(findUp.sync('unicorn.png')); +expectType(findUp.sync('unicorn.png', {cwd: ''})); +expectType(findUp.sync(['rainbow.png', 'unicorn.png'])); +expectType(findUp.sync(['rainbow.png', 'unicorn.png'], {cwd: ''})); diff --git a/readme.md b/readme.md index 4bc084e..949f2b4 100644 --- a/readme.md +++ b/readme.md @@ -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 diff --git a/test.js b/test.js index 6b1e224..3a0623a 100644 --- a/test.js +++ b/test.js @@ -43,207 +43,207 @@ test.afterEach(t => { }); test('async (child file)', async t => { - const filePath = await findUp(name.packageJson); + const foundPath = await findUp(name.packageJson); - t.is(filePath, absolute.packageJson); + t.is(foundPath, absolute.packageJson); }); test('sync (child file)', t => { - const filePath = findUp.sync(name.packageJson); + const foundPath = findUp.sync(name.packageJson); - t.is(filePath, absolute.packageJson); + t.is(foundPath, absolute.packageJson); }); test('async (child dir)', async t => { - const filePath = await findUp(name.fixtureDirectory); + const foundPath = await findUp(name.fixtureDirectory); - t.is(filePath, absolute.fixtureDirectory); + t.is(foundPath, absolute.fixtureDirectory); }); test('sync (child dir)', t => { - const filePath = findUp.sync(name.fixtureDirectory); + const foundPath = findUp.sync(name.fixtureDirectory); - t.is(filePath, absolute.fixtureDirectory); + t.is(foundPath, absolute.fixtureDirectory); }); test('async (child file, custom cwd)', async t => { - const filePath = await findUp(name.baz, { + const foundPath = await findUp(name.baz, { cwd: relative.fixtureDirectory }); - t.is(filePath, absolute.baz); + t.is(foundPath, absolute.baz); }); test('sync (child file, custom cwd)', t => { - const filePath = findUp.sync(name.baz, { + const foundPath = findUp.sync(name.baz, { cwd: relative.fixtureDirectory }); - t.is(filePath, absolute.baz); + t.is(foundPath, absolute.baz); }); test('async (child file, array, custom cwd)', async t => { - const filePath = await findUp([name.baz], { + const foundPath = await findUp([name.baz], { cwd: relative.fixtureDirectory }); - t.is(filePath, absolute.baz); + t.is(foundPath, absolute.baz); }); test('sync (child file, array, custom cwd)', t => { - const filePath = findUp.sync([name.baz], { + const foundPath = findUp.sync([name.baz], { cwd: relative.fixtureDirectory }); - t.is(filePath, absolute.baz); + t.is(foundPath, absolute.baz); }); test('async (first child file, array, custom cwd)', async t => { - const filePath = await findUp([name.qux, name.baz], { + const foundPath = await findUp([name.qux, name.baz], { cwd: relative.fixtureDirectory }); - t.is(filePath, absolute.qux); + t.is(foundPath, absolute.qux); }); test('sync (first child file, array, custom cwd)', t => { - const filePath = findUp.sync([name.qux, name.baz], { + const foundPath = findUp.sync([name.qux, name.baz], { cwd: relative.fixtureDirectory }); - t.is(filePath, absolute.qux); + t.is(foundPath, absolute.qux); }); test('async (second child file, array, custom cwd)', async t => { - const filePath = await findUp(['fake', name.baz], { + const foundPath = await findUp(['fake', name.baz], { cwd: relative.fixtureDirectory }); - t.is(filePath, absolute.baz); + t.is(foundPath, absolute.baz); }); test('sync (second child file, array, custom cwd)', t => { - const filePath = findUp.sync(['fake', name.baz], { + const foundPath = findUp.sync(['fake', name.baz], { cwd: relative.fixtureDirectory }); - t.is(filePath, absolute.baz); + t.is(foundPath, absolute.baz); }); test('async (cwd)', async t => { - const filePath = await findUp(name.packageDirectory, { + const foundPath = await findUp(name.packageDirectory, { cwd: absolute.packageDirectory }); - t.is(filePath, absolute.packageDirectory); + t.is(foundPath, absolute.packageDirectory); }); test('sync (cwd)', t => { - const filePath = findUp.sync(name.packageDirectory, { + const foundPath = findUp.sync(name.packageDirectory, { cwd: absolute.packageDirectory }); - t.is(filePath, absolute.packageDirectory); + t.is(foundPath, absolute.packageDirectory); }); test('async (cousin file, custom cwd)', async t => { - const filePath = await findUp(name.baz, { + const foundPath = await findUp(name.baz, { cwd: relative.barDir }); - t.is(filePath, absolute.baz); + t.is(foundPath, absolute.baz); }); test('sync (cousin file, custom cwd)', t => { - const filePath = findUp.sync(name.baz, { + const foundPath = findUp.sync(name.baz, { cwd: relative.barDir }); - t.is(filePath, absolute.baz); + t.is(foundPath, absolute.baz); }); test('async (nested descendant file)', async t => { - const filePath = await findUp(relative.baz); + const foundPath = await findUp(relative.baz); - t.is(filePath, absolute.baz); + t.is(foundPath, absolute.baz); }); test('sync (nested descendant file)', t => { - const filePath = findUp.sync(relative.baz); + const foundPath = findUp.sync(relative.baz); - t.is(filePath, absolute.baz); + t.is(foundPath, absolute.baz); }); test('async (nested descendant dir)', async t => { - const filePath = await findUp(relative.barDir); + const foundPath = await findUp(relative.barDir); - t.is(filePath, absolute.barDir); + t.is(foundPath, absolute.barDir); }); test('sync (nested descendant dir)', t => { - const filePath = findUp.sync(relative.barDir); + const foundPath = findUp.sync(relative.barDir); - t.is(filePath, absolute.barDir); + t.is(foundPath, absolute.barDir); }); test('async (nested cousin dir, custom cwd)', async t => { - const filePath = await findUp(relative.barDir, { + const foundPath = await findUp(relative.barDir, { cwd: relative.fixtureDirectory }); - t.is(filePath, absolute.barDir); + t.is(foundPath, absolute.barDir); }); test('sync (nested cousin dir, custom cwd)', t => { - const filePath = findUp.sync(relative.barDir, { + const foundPath = findUp.sync(relative.barDir, { cwd: relative.fixtureDirectory }); - t.is(filePath, absolute.barDir); + t.is(foundPath, absolute.barDir); }); test('async (ancestor dir, custom cwd)', async t => { - const filePath = await findUp(name.fixtureDirectory, { + const foundPath = await findUp(name.fixtureDirectory, { cwd: relative.barDir }); - t.is(filePath, absolute.fixtureDirectory); + t.is(foundPath, absolute.fixtureDirectory); }); test('sync (ancestor dir, custom cwd)', t => { - const filePath = findUp.sync(name.fixtureDirectory, { + const foundPath = findUp.sync(name.fixtureDirectory, { cwd: relative.barDir }); - t.is(filePath, absolute.fixtureDirectory); + t.is(foundPath, absolute.fixtureDirectory); }); test('async (not found)', async t => { - const filePath = await findUp('somenonexistentfile.js'); + const foundPath = await findUp('somenonexistentfile.js'); - t.is(filePath, null); + t.is(foundPath, undefined); }); test('sync (not found)', t => { - const filePath = findUp.sync('somenonexistentfile.js'); + const foundPath = findUp.sync('somenonexistentfile.js'); - t.is(filePath, null); + t.is(foundPath, undefined); }); // Both tests start in a disjoint directory. `package.json` should not be found -// and `null` should be returned. +// and `undefined` should be returned. test('async (not found, custom cwd)', async t => { - const filePath = await findUp(name.packageJson, { + const foundPath = await findUp(name.packageJson, { cwd: t.context.disjoint }); - t.is(filePath, null); + t.is(foundPath, undefined); }); test('sync (not found, custom cwd)', t => { - const filePath = findUp.sync(name.packageJson, { + const foundPath = findUp.sync(name.packageJson, { cwd: t.context.disjoint }); - t.is(filePath, null); + t.is(foundPath, undefined); });