From b4d3f5ab5b07268e7175d088f7ca1f5da5dc1e6a Mon Sep 17 00:00:00 2001 From: phated Date: Sun, 31 Oct 2021 19:30:00 +0000 Subject: [PATCH] chore: Run prettier --- README.md | 26 +++-- index.js | 14 +-- test/fixtures/fined/package.json | 3 +- test/index.js | 167 +++++++++++++++---------------- test/utils/create-symlinks.js | 4 +- 5 files changed, 101 insertions(+), 113 deletions(-) diff --git a/README.md b/README.md index 2df4576..13dbcef 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ var opts = { name: '.app', cwd: '.', extensions: { - 'rc': 'default-rc-loader', + rc: 'default-rc-loader', '.yml': 'default-yml-loader', }, }; @@ -31,7 +31,7 @@ var opts = { fined({ path: '.' }, opts); // => { path: '/absolute/of/cwd/.app.yml', extension: { '.yml': 'default-yml-loader' } } -fined({ path: '~', extensions: { 'rc': 'some-special-rc-loader' } }, opts); +fined({ path: '~', extensions: { rc: 'some-special-rc-loader' } }, opts); // => { path: '/User/home/.apprc', extension: { 'rc': 'some-special-rc-loader' } } ``` @@ -41,24 +41,23 @@ fined({ path: '~', extensions: { 'rc': 'some-special-rc-loader' } }, opts); #### Arguments: -* **pathObj** [string | object] : a path setting for finding a file. -* **opts** [object] : a plain object supplements `pathObj`. +- **pathObj** [string | object] : a path setting for finding a file. +- **opts** [object] : a plain object supplements `pathObj`. - `pathObj` and `opts` can have same properties: + `pathObj` and `opts` can have same properties: - * **path** [string] : a path string. - * **name** [string] : a basename. - * **extensions**: [string | array | object] : extensions. - * **cwd**: a base directory of `path` and for finding up. - * **findUp**: [boolean] : a flag to find up. + - **path** [string] : a path string. + - **name** [string] : a basename. + - **extensions**: [string | array | object] : extensions. + - **cwd**: a base directory of `path` and for finding up. + - **findUp**: [boolean] : a flag to find up. #### Return: This function returns a plain object which consists of following properties if a file exists otherwise null. - * **path** : an absolute path - * **extension** : a string or a plain object of extension. - +- **path** : an absolute path +- **extension** : a string or a plain object of extension. ## License @@ -75,4 +74,3 @@ MIT [coveralls-url]: https://coveralls.io/r/gulpjs/fined [coveralls-image]: https://img.shields.io/coveralls/gulpjs/fined/master.svg - diff --git a/index.js b/index.js index efd8863..7bb0f2f 100644 --- a/index.js +++ b/index.js @@ -9,7 +9,6 @@ var defaults = require('object.defaults/immutable'); var expandTilde = require('expand-tilde'); var parsePath = require('parse-filepath'); - function fined(pathObj, defaultObj) { var expandedPath = expandPath(pathObj, defaultObj); return expandedPath ? findWithExpandedPath(expandedPath) : null; @@ -59,7 +58,8 @@ function expandPath(pathObj, defaultObj) { filePath = filePath.slice(parsed.root.length); findUp = false; basedir = parsed.root; - } else /* istanbul ignore next */ if (parsed.root) { // Expanded path has a drive letter on Windows. + } /* istanbul ignore next */ else if (parsed.root) { + // Expanded path has a drive letter on Windows. filePath = filePath.slice(parsed.root.length); basedir = path.resolve(parsed.root); } @@ -80,9 +80,9 @@ function expandPath(pathObj, defaultObj) { } function findWithExpandedPath(expanded) { - var found = expanded.findUp ? - findUpFile(expanded.basedir, expanded.path, expanded.extArr) : - findFile(expanded.basedir, expanded.path, expanded.extArr); + var found = expanded.findUp + ? findUpFile(expanded.basedir, expanded.path, expanded.extArr) + : findFile(expanded.basedir, expanded.path, expanded.extArr); if (!found) { return null; @@ -131,12 +131,12 @@ function createExtensionArray(exts) { if (Array.isArray(exts)) { exts = exts.filter(isString); - return (exts.length > 0) ? exts : ['']; + return exts.length > 0 ? exts : ['']; } if (isPlainObject(exts)) { exts = Object.keys(exts); - return (exts.length > 0) ? exts : ['']; + return exts.length > 0 ? exts : ['']; } return ['']; diff --git a/test/fixtures/fined/package.json b/test/fixtures/fined/package.json index 2c63c08..0967ef4 100644 --- a/test/fixtures/fined/package.json +++ b/test/fixtures/fined/package.json @@ -1,2 +1 @@ -{ -} +{} diff --git a/test/index.js b/test/index.js index adfb663..37c1258 100644 --- a/test/index.js +++ b/test/index.js @@ -5,7 +5,7 @@ var path = require('path'); var expect = require('expect'); var cwd = process.cwd(); -var isWindows = (os.platform() === 'win32'); +var isWindows = os.platform() === 'win32'; var userHomeFile = require('./utils/get-userhome-file'); var symlinkedFiles = require('./utils/create-symlinks'); @@ -17,9 +17,8 @@ if (isWindows) { process.chdir(process.cwd()); } -describe('Basic behaviors', function() { - - it('returns object when target file exists', function(done) { +describe('Basic behaviors', function () { + it('returns object when target file exists', function (done) { var pathObj = { path: 'test/fixtures/fined', extensions: ['.json', '.js'], @@ -41,7 +40,7 @@ describe('Basic behaviors', function() { done(); }); - it('returns null when target file does not exist', function(done) { + it('returns null when target file does not exist', function (done) { var pathObj = { path: 'test/fixtures/fined', extensions: ['.json', '.js'], @@ -60,7 +59,7 @@ describe('Basic behaviors', function() { done(); }); - it('normalizes a string as 1st argument to an object', function(done) { + it('normalizes a string as 1st argument to an object', function (done) { var pathObj = 'test/fixtures/fined'; var defaultObj = { @@ -81,7 +80,7 @@ describe('Basic behaviors', function() { done(); }); - it('returns null when both arguments are empty', function(done) { + it('returns null when both arguments are empty', function (done) { var pathObj = {}; var defaultObj = {}; @@ -94,7 +93,7 @@ describe('Basic behaviors', function() { done(); }); - it('returns null when both arguments are null', function(done) { + it('returns null when both arguments are null', function (done) { var pathObj = null; var defaultObj = null; @@ -107,7 +106,7 @@ describe('Basic behaviors', function() { done(); }); - it('treats 1st argument as an empty object when it is invalid', function(done) { + it('treats 1st argument as an empty object when it is invalid', function (done) { var pathObj = 123; var defaultObj = { @@ -129,8 +128,8 @@ describe('Basic behaviors', function() { done(); }); - it('returns null when both arguments are invalid', function(done) { - var pathObj = function() { + it('returns null when both arguments are invalid', function (done) { + var pathObj = function () { return { path: 'test/fixtures/fined', name: 'app', @@ -150,7 +149,7 @@ describe('Basic behaviors', function() { done(); }); - it('accepts paths with extensions already', function(done) { + it('accepts paths with extensions already', function (done) { var pathObj = { path: 'test/fixtures/fined/app.js', cwd: cwd, @@ -168,7 +167,7 @@ describe('Basic behaviors', function() { done(); }); - it('only matches the extension specified in path', function(done) { + it('only matches the extension specified in path', function (done) { var pathObj = { path: 'test/fixtures/fined/appfile.js', cwd: cwd, @@ -186,7 +185,7 @@ describe('Basic behaviors', function() { done(); }); - it('accepts name with extensions already', function(done) { + it('accepts name with extensions already', function (done) { var pathObj = { path: 'test/fixtures/fined', extensions: ['.json', '.js'], @@ -208,7 +207,7 @@ describe('Basic behaviors', function() { done(); }); - it('only matches the extension specified in name', function(done) { + it('only matches the extension specified in name', function (done) { var pathObj = { path: 'test/fixtures/fined', extensions: ['.json', '.js'], @@ -230,7 +229,7 @@ describe('Basic behaviors', function() { done(); }); - it('only ignores the extension at the end of the path', function(done) { + it('only ignores the extension at the end of the path', function (done) { var pathObj = { path: 'test/fixtures/fined/.js/app.js', cwd: cwd, @@ -247,12 +246,10 @@ describe('Basic behaviors', function() { expect(result).toEqual(expected); done(); }); - }); -describe('Argument defaulting', function() { - - it('does not default when 2nd argument is empty', function(done) { +describe('Argument defaulting', function () { + it('does not default when 2nd argument is empty', function (done) { var pathObj = { name: 'package', path: 'test/fixtures/fined', @@ -274,7 +271,7 @@ describe('Argument defaulting', function() { done(); }); - it('defaults all properties when 1st argument is empty', function(done) { + it('defaults all properties when 1st argument is empty', function (done) { var pathObj = {}; var defaultObj = { @@ -296,7 +293,7 @@ describe('Argument defaulting', function() { done(); }); - it('defaults missing properties in 1st argument', function(done) { + it('defaults missing properties in 1st argument', function (done) { var pathObj = { name: 'app', cwd: path.resolve(cwd, 'test'), @@ -319,7 +316,7 @@ describe('Argument defaulting', function() { done(); }); - it('defaults null properties in the 1st argument', function(done) { + it('defaults null properties in the 1st argument', function (done) { var pathObj = { name: null, path: null, @@ -347,7 +344,7 @@ describe('Argument defaulting', function() { done(); }); - it('does not default when both arguments are complete', function(done) { + it('does not default when both arguments are complete', function (done) { var pathObj = { name: 'README', path: '.', @@ -375,7 +372,7 @@ describe('Argument defaulting', function() { done(); }); - it('defaults everything if 1st argument is null', function(done) { + it('defaults everything if 1st argument is null', function (done) { var pathObj = null; var defaultObj = { @@ -397,7 +394,7 @@ describe('Argument defaulting', function() { done(); }); - it('ignores 2nd argument if it is null', function(done) { + it('ignores 2nd argument if it is null', function (done) { var pathObj = { path: 'test/fixtures/fined', name: 'app', @@ -419,7 +416,7 @@ describe('Argument defaulting', function() { done(); }); - it('ignores 2nd argument if it is invalid', function(done) { + it('ignores 2nd argument if it is invalid', function (done) { var pathObj = { path: 'test/fixtures/fined', name: 'app', @@ -442,9 +439,8 @@ describe('Argument defaulting', function() { }); }); -describe('Properties: `path`', function() { - - it('defaults `path` when it is null', function(done) { +describe('Properties: `path`', function () { + it('defaults `path` when it is null', function (done) { var pathObj = { path: null, }; @@ -468,7 +464,7 @@ describe('Properties: `path`', function() { done(); }); - it('returns null when `path` is null after defaulting', function(done) { + it('returns null when `path` is null after defaulting', function (done) { var pathObj = { path: null, }; @@ -489,7 +485,7 @@ describe('Properties: `path`', function() { done(); }); - it('resolves to cwd + name + extension when `path` is an empty string', function(done) { + it('resolves to cwd + name + extension when `path` is an empty string', function (done) { var pathObj = { path: '', }; @@ -512,7 +508,7 @@ describe('Properties: `path`', function() { done(); }); - it('resolves to cwd when `path` and `name` are empty strings', function(done) { + it('resolves to cwd when `path` and `name` are empty strings', function (done) { var pathObj = { path: '', name: '', @@ -535,7 +531,7 @@ describe('Properties: `path`', function() { done(); }); - it('returns null when `path` is an invalid type', function(done) { + it('returns null when `path` is an invalid type', function (done) { var pathObj = { path: function noop() {}, }; @@ -555,7 +551,7 @@ describe('Properties: `path`', function() { done(); }); - it('resolves properly when `path` is a String object', function(done) { + it('resolves properly when `path` is a String object', function (done) { var pathObj = { path: new String('test/fixtures/fined'), }; @@ -598,7 +594,7 @@ describe('Properties: `path`', function() { done(); }); - it('resolves `~` to homedir', function(done) { + it('resolves `~` to homedir', function (done) { // ~ var pathObj = { path: '~', @@ -664,7 +660,7 @@ describe('Properties: `path`', function() { done(); }); - it('resolves `~+` to process.cwd()', function(done) { + it('resolves `~+` to process.cwd()', function (done) { // ~+ var pathObj = { path: '~+', @@ -730,7 +726,7 @@ describe('Properties: `path`', function() { done(); }); - it('ignores `cwd` when `path` is absolute', function(done) { + it('ignores `cwd` when `path` is absolute', function (done) { var pathObj = { path: cwd, cwd: path.resolve(cwd, 'test/fixtures/fined'), @@ -753,7 +749,7 @@ describe('Properties: `path`', function() { done(); }); - it('ignores `cwd` when `path` has a drive letter (Windows only)', function(done) { + it('ignores `cwd` when `path` has a drive letter (Windows only)', function (done) { if (!isWindows) { this.skip(); return; @@ -783,9 +779,8 @@ describe('Properties: `path`', function() { }); }); -describe('Properties: `name`', function() { - - it('ignores `name` when null', function(done) { +describe('Properties: `name`', function () { + it('ignores `name` when null', function (done) { var pathObj = { name: null, }; @@ -807,7 +802,7 @@ describe('Properties: `name`', function() { done(); }); - it('ignores `name` when it is an empty string', function(done) { + it('ignores `name` when it is an empty string', function (done) { var pathObj = { name: '', }; @@ -829,7 +824,7 @@ describe('Properties: `name`', function() { done(); }); - it('ignores `name` when it is an invalid type', function(done) { + it('ignores `name` when it is an invalid type', function (done) { var pathObj = { name: 123, }; @@ -851,7 +846,7 @@ describe('Properties: `name`', function() { done(); }); - it('searches for file with `name` when it is a String object', function(done) { + it('searches for file with `name` when it is a String object', function (done) { var pathObj = { name: new String('app'), }; @@ -892,7 +887,7 @@ describe('Properties: `name`', function() { done(); }); - it('resolves `name` even when it is a directory', function(done) { + it('resolves `name` even when it is a directory', function (done) { var pathObj = { name: 'fined', }; @@ -914,7 +909,7 @@ describe('Properties: `name`', function() { done(); }); - it('resolves `name` when it is an absolute path and `path` is empty', function(done) { + it('resolves `name` when it is an absolute path and `path` is empty', function (done) { var pathObj = { name: path.resolve(cwd, 'test/fixtures/fined/app'), }; @@ -955,7 +950,7 @@ describe('Properties: `name`', function() { done(); }); - it('returns null when `name` is an absolute path but `path` is not empty', function(done) { + it('returns null when `name` is an absolute path but `path` is not empty', function (done) { var pathObj = { name: path.resolve(cwd, 'test/fixtures/fined/app'), path: 'test/fixtures/fined', @@ -974,7 +969,7 @@ describe('Properties: `name`', function() { done(); }); - it('will not expand `~` as part of `name`', function(done) { + it('will not expand `~` as part of `name`', function (done) { var pathObj = { name: '~/' + userHomeFile.name, }; @@ -994,9 +989,8 @@ describe('Properties: `name`', function() { }); }); -describe('Properties: `extensions`', function() { - - it('resolves to the extension if it is a string', function(done) { +describe('Properties: `extensions`', function () { + it('resolves to the extension if it is a string', function (done) { var pathObj = { extensions: '.js', }; @@ -1018,7 +1012,7 @@ describe('Properties: `extensions`', function() { done(); }); - it('resolves to the first found extension if it is an array', function(done) { + it('resolves to the first found extension if it is an array', function (done) { var pathObj = { extensions: ['.json', '.txt', '.js'], }; @@ -1040,7 +1034,7 @@ describe('Properties: `extensions`', function() { done(); }); - it('resolves to the first found extension if it is an object', function(done) { + it('resolves to the first found extension if it is an object', function (done) { var pathObj = { extensions: { '.json': 1, @@ -1067,7 +1061,7 @@ describe('Properties: `extensions`', function() { done(); }); - it('resolves to the first found extension if multiple match', function(done) { + it('resolves to the first found extension if multiple match', function (done) { var pathObj = { extensions: ['.json', '.js'], }; @@ -1108,7 +1102,7 @@ describe('Properties: `extensions`', function() { done(); }); - it('treats a null value as an empty array', function(done) { + it('treats a null value as an empty array', function (done) { var pathObj = { extensions: null, }; @@ -1130,7 +1124,7 @@ describe('Properties: `extensions`', function() { done(); }); - it('treats an empty string value as an empty array', function(done) { + it('treats an empty string value as an empty array', function (done) { var pathObj = { extensions: '', }; @@ -1152,7 +1146,7 @@ describe('Properties: `extensions`', function() { done(); }); - it('treats an empty array as an empty array', function(done) { + it('treats an empty array as an empty array', function (done) { var pathObj = { extensions: [], }; @@ -1174,7 +1168,7 @@ describe('Properties: `extensions`', function() { done(); }); - it('treats an empty object as an object with only key being an empty string', function(done) { + it('treats an empty object as an object with only key being an empty string', function (done) { var pathObj = { extensions: {}, }; @@ -1196,7 +1190,7 @@ describe('Properties: `extensions`', function() { done(); }); - it('treats an invalid type as an empty array', function(done) { + it('treats an invalid type as an empty array', function (done) { var pathObj = { extensions: 123, }; @@ -1218,7 +1212,7 @@ describe('Properties: `extensions`', function() { done(); }); - it('supports String objects', function(done) { + it('supports String objects', function (done) { var pathObj = { extensions: [new String('.json'), new String('.js')], }; @@ -1264,9 +1258,8 @@ describe('Properties: `extensions`', function() { }); }); -describe('Properties: `cwd`', function() { - - it('can be absolute', function(done) { +describe('Properties: `cwd`', function () { + it('can be absolute', function (done) { var pathObj = { cwd: path.resolve('.'), }; @@ -1288,7 +1281,7 @@ describe('Properties: `cwd`', function() { done(); }); - it('can be relative', function(done) { + it('can be relative', function (done) { var pathObj = { cwd: '.', }; @@ -1329,7 +1322,7 @@ describe('Properties: `cwd`', function() { done(); }); - it('treats a null value as `.`', function(done) { + it('treats a null value as `.`', function (done) { var pathObj = { cwd: null, }; @@ -1351,7 +1344,7 @@ describe('Properties: `cwd`', function() { done(); }); - it('treats an empty string as `.`', function(done) { + it('treats an empty string as `.`', function (done) { var pathObj = { cwd: '', }; @@ -1373,7 +1366,7 @@ describe('Properties: `cwd`', function() { done(); }); - it('treats an invalid type as `.`', function(done) { + it('treats an invalid type as `.`', function (done) { var pathObj = { cwd: 123, }; @@ -1395,7 +1388,7 @@ describe('Properties: `cwd`', function() { done(); }); - it('supports String objects', function(done) { + it('supports String objects', function (done) { var pathObj = { cwd: new String(cwd), }; @@ -1417,7 +1410,7 @@ describe('Properties: `cwd`', function() { done(); }); - it('expands `~` to homedir', function(done) { + it('expands `~` to homedir', function (done) { var pathObj = { cwd: '~', }; @@ -1440,9 +1433,8 @@ describe('Properties: `cwd`', function() { }); }); -describe('Properties: `findUp`', function() { - - it('finds a file up in the tree', function(done) { +describe('Properties: `findUp`', function () { + it('finds a file up in the tree', function (done) { var pathObj = { path: '', findUp: true, @@ -1465,7 +1457,7 @@ describe('Properties: `findUp`', function() { done(); }); - it('finds a directory up in the tree', function(done) { + it('finds a directory up in the tree', function (done) { var pathObj = { path: '', findUp: true, @@ -1488,7 +1480,7 @@ describe('Properties: `findUp`', function() { done(); }); - it('does not search up the tree if file exists in cwd', function(done) { + it('does not search up the tree if file exists in cwd', function (done) { var pathObj = { path: '', extensions: '.json', @@ -1511,7 +1503,7 @@ describe('Properties: `findUp`', function() { done(); }); - it('does not search up the tree if `path` is absolute', function(done) { + it('does not search up the tree if `path` is absolute', function (done) { var pathObj = { findUp: true, path: path.resolve(cwd, 'test'), @@ -1531,7 +1523,7 @@ describe('Properties: `findUp`', function() { done(); }); - it('does not search up the tree if `cwd` has a drive letter (Windows only)', function(done) { + it('does not search up the tree if `cwd` has a drive letter (Windows only)', function (done) { if (!isWindows) { this.skip(); return; @@ -1560,7 +1552,7 @@ describe('Properties: `findUp`', function() { done(); }); - it('does not search up the tree any more if file with another extension candidate exists', function(done) { + it('does not search up the tree any more if file with another extension candidate exists', function (done) { var pathObj = { findUp: true, path: '.', @@ -1584,9 +1576,8 @@ describe('Properties: `findUp`', function() { }); }); -describe('Symbolic links', function() { - - it('returns symlink path when found link points to a file', function(done) { +describe('Symbolic links', function () { + it('returns symlink path when found link points to a file', function (done) { var pathObj = { path: '.', name: symlinkedFiles[0].name, @@ -1621,10 +1612,10 @@ describe('Symbolic links', function() { done(); }); - it('returns symlink path when found link points to a directory', function(done) { + it('returns symlink path when found link points to a directory', function (done) { // Fail on Windows and Node v10,v11, because of: // https://github.com/nodejs/node/pull/23724 - if (isWindows && ['v10','v11'].includes(process.version.split('.')[0])) { + if (isWindows && ['v10', 'v11'].includes(process.version.split('.')[0])) { this.skip(); } @@ -1662,7 +1653,7 @@ describe('Symbolic links', function() { done(); }); - it('returns null when found link is an invalid symlink', function(done) { + it('returns null when found link is an invalid symlink', function (done) { var pathObj = { path: '.', name: symlinkedFiles[2].name, @@ -1691,7 +1682,7 @@ describe('Symbolic links', function() { done(); }); - it('returns symlink path during findUp when symlink points to a file', function(done) { + it('returns symlink path during findUp when symlink points to a file', function (done) { var pathObj = { path: path.basename(symlinkedFiles[0].dir), name: symlinkedFiles[0].name, @@ -1728,10 +1719,10 @@ describe('Symbolic links', function() { done(); }); - it('returns symlink path during findUp when symlink points to a directory', function(done) { + it('returns symlink path during findUp when symlink points to a directory', function (done) { // Fail on Windows and Node v10,v11, because of: // https://github.com/nodejs/node/pull/23724 - if (isWindows && ['v10','v11'].includes(process.version.split('.')[0])) { + if (isWindows && ['v10', 'v11'].includes(process.version.split('.')[0])) { this.skip(); } @@ -1771,7 +1762,7 @@ describe('Symbolic links', function() { done(); }); - it('returns null during findUp when symlink is invalid', function(done) { + it('returns null during findUp when symlink is invalid', function (done) { var pathObj = { path: path.basename(symlinkedFiles[2].dir), name: symlinkedFiles[2].name, diff --git a/test/utils/create-symlinks.js b/test/utils/create-symlinks.js index cc88c94..d153497 100644 --- a/test/utils/create-symlinks.js +++ b/test/utils/create-symlinks.js @@ -6,7 +6,7 @@ var path = require('path'); var dir = path.resolve(__dirname, '../fixtures/fined'); var basedir = path.resolve(__dirname, '../../'); -var symlinkedFiles = [0,1,2,3,4,5].map(function(v, j) { +var symlinkedFiles = [0, 1, 2, 3, 4, 5].map(function (v, j) { return path.resolve(dir, 'symlink' + j + '.json'); }); @@ -22,7 +22,7 @@ for (var i = 0, n = linkedFiles.length; i < n; i++) { fs.symlinkSync(symlinkedFiles[i * 2], symlinkedFiles[i * 2 + 1]); } -module.exports = symlinkedFiles.map(function(pth) { +module.exports = symlinkedFiles.map(function (pth) { var ext = path.extname(pth); return { path: pth,