From 2086f518069734534f9eb2ffbabc5bf3fb098c89 Mon Sep 17 00:00:00 2001 From: segayuu Date: Thu, 9 Jan 2020 18:36:26 +0900 Subject: [PATCH 1/5] Useful fs.promises API --- .travis.yml | 2 +- appveyor.yml | 2 +- lib/fs.js | 85 +++++++++++++++++++++------------------------------- package.json | 2 +- 4 files changed, 37 insertions(+), 54 deletions(-) diff --git a/.travis.yml b/.travis.yml index e78421b..e6d47c8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,8 +8,8 @@ cache: - node_modules node_js: - - "8" - "10" + - "12" - "node" script: diff --git a/appveyor.yml b/appveyor.yml index 0846e74..96375b2 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -5,9 +5,9 @@ init: # Test against these versions of Node.js. environment: matrix: - - nodejs_version: "8" - nodejs_version: "10" - nodejs_version: "12" + - nodejs_version: "13" matrix: fast_finish: true diff --git a/lib/fs.js b/lib/fs.js index dcbc76b..7c6c0ed 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -3,31 +3,24 @@ const Promise = require('bluebird'); const fs = require('graceful-fs'); const { dirname, join, extname, basename } = require('path'); +const fsPromises = fs.promises; const chokidar = require('chokidar'); const escapeRegExp = require('escape-string-regexp'); const rEOL = /\r\n/g; -const statAsync = Promise.promisify(fs.stat); -const readdirAsync = Promise.promisify(fs.readdir); -const unlinkAsync = Promise.promisify(fs.unlink); -const mkdirAsync = Promise.promisify(fs.mkdir); -const writeFileAsync = Promise.promisify(fs.writeFile); -const appendFileAsync = Promise.promisify(fs.appendFile); -const rmdirAsync = Promise.promisify(fs.rmdir); -const readFileAsync = Promise.promisify(fs.readFile); -const createReadStream = fs.createReadStream; -const createWriteStream = fs.createWriteStream; -const accessSync = fs.accessSync; -const accessAsync = Promise.promisify(fs.access); - function exists(path, callback) { if (!path) throw new TypeError('path is required!'); - return accessAsync(path).then(() => true, () => false).then(exist => { + const promise = fsPromises.access(path).then(() => true, err => { + if (err.code !== 'ENOENT') throw err; + return false; + }).then(exist => { if (typeof callback === 'function') callback(exist); return exist; }); + + return Promise.resolve(promise); } function existsSync(path) { @@ -36,6 +29,7 @@ function existsSync(path) { try { fs.accessSync(path); } catch (err) { + if (err.code !== 'ENOENT') throw err; return false; } @@ -49,8 +43,8 @@ function mkdirs(path, callback) { return exists(parent).then(exist => { if (!exist) return mkdirs(parent); - }).then(() => mkdirAsync(path)).catch(err => { - if (err.cause.code !== 'EEXIST') throw err; + }).then(() => fsPromises.mkdir(path)).catch(err => { + if (err.code !== 'EEXIST') throw err; }).asCallback(callback); } @@ -58,9 +52,8 @@ function mkdirsSync(path) { if (!path) throw new TypeError('path is required!'); const parent = dirname(path); - const exist = fs.existsSync(parent); - if (!exist) mkdirsSync(parent); + if (!fs.existsSync(parent)) mkdirsSync(parent); fs.mkdirSync(path); } @@ -78,9 +71,8 @@ function checkParentSync(path) { if (!path) throw new TypeError('path is required!'); const parent = dirname(path); - const exist = fs.existsSync(parent); - if (exist) return; + if (fs.existsSync(parent)) return; try { mkdirsSync(parent); @@ -97,7 +89,7 @@ function writeFile(path, data, options, callback) { options = {}; } - return checkParent(path).then(() => writeFileAsync(path, data, options)).asCallback(callback); + return checkParent(path).then(() => fsPromises.writeFile(path, data, options)).asCallback(callback); } function writeFileSync(path, data, options) { @@ -115,7 +107,7 @@ function appendFile(path, data, options, callback) { options = {}; } - return checkParent(path).then(() => appendFileAsync(path, data, options)).asCallback(callback); + return checkParent(path).then(() => fsPromises.appendFile(path, data, options)).asCallback(callback); } function appendFileSync(path, data, options) { @@ -125,15 +117,6 @@ function appendFileSync(path, data, options) { fs.appendFileSync(path, data, options); } -const _copyFile = fs.copyFile ? Promise.promisify(fs.copyFile) : (src, dest) => new Promise((resolve, reject) => { - const rs = createReadStream(src); - const ws = createWriteStream(dest); - - rs.pipe(ws).on('error', reject); - - ws.on('close', resolve).on('error', reject); -}); - function copyFile(src, dest, flags, callback) { if (!src) throw new TypeError('src is required!'); if (!dest) throw new TypeError('dest is required!'); @@ -141,7 +124,7 @@ function copyFile(src, dest, flags, callback) { callback = flags; } - return checkParent(dest).then(() => _copyFile(src, dest, flags)).asCallback(callback); + return checkParent(dest).then(() => fsPromises.copyFile(src, dest, flags)).asCallback(callback); } function trueFn() { @@ -186,13 +169,13 @@ function reduceFiles(result, item) { } function _readAndFilterDir(path, options) { - return readdirAsync(path) + return Promise.resolve(fsPromises.readdir(path)) .filter(ignoreHiddenFiles(options.ignoreHidden == null ? true : options.ignoreHidden)) .filter(ignoreFilesRegex(options.ignorePattern)) .map(item => { const fullPath = join(path, item); - return statAsync(fullPath).then(stats => ({ + return fsPromises.stat(fullPath).then(stats => ({ isDirectory: stats.isDirectory(), path: item, fullPath @@ -306,7 +289,7 @@ function readFile(path, options = {}, callback) { if (!Object.prototype.hasOwnProperty.call(options, 'encoding')) options.encoding = 'utf8'; - return readFileAsync(path, options).then(content => { + return Promise.resolve(fs.promises.readFile(path, options)).then(content => { if (options.escape == null || options.escape) { return escapeFileContent(content); } @@ -337,12 +320,12 @@ function _emptyDir(path, options, parent) { const currentPath = join(parent, item.path); if (item.isDirectory) { - return _emptyDir(fullPath, options, currentPath).tap(() => readdirAsync(fullPath).then(files => { - if (!files.length) return rmdirAsync(fullPath); + return _emptyDir(fullPath, options, currentPath).tap(() => fs.promises.readdir(fullPath).then(files => { + if (!files.length) return fsPromises.rmdir(fullPath); })); } - return unlinkAsync(fullPath).thenReturn(currentPath); + return fsPromises.unlink(fullPath).then(() => currentPath); }).reduce(reduceFiles, []); } @@ -386,17 +369,17 @@ function emptyDirSync(path, options = {}) { } function _rmdir(path) { - return readdirAsync(path).map(item => { + return Promise.resolve(fsPromises.readdir(path)).map(item => { const childPath = join(path, item); - return statAsync(childPath).then(stats => { + return fsPromises.stat(childPath).then(stats => { if (stats.isDirectory()) { return _rmdir(childPath); } - return unlinkAsync(childPath); + return fsPromises.unlink(childPath); }); - }).then(() => rmdirAsync(path)); + }).then(() => fsPromises.rmdir(path)); } function rmdir(path, callback) { @@ -471,7 +454,7 @@ function ensurePath(path, callback) { return exists(path).then(exist => { if (!exist) return path; - return readdirAsync(dirname(path)).then(files => _findUnusedPath(path, files)); + return fsPromises.readdir(dirname(path)).then(files => _findUnusedPath(path, files)); }).asCallback(callback); } @@ -511,8 +494,8 @@ function ensureWriteStreamSync(path, options) { }); }); -exports.access = accessAsync; -exports.accessSync = accessSync; +exports.access = Promise.promisify(fs.access); +exports.accessSync = fs.accessSync; // appendFile exports.appendFile = appendFile; @@ -543,8 +526,8 @@ exports.copyDir = copyDir; exports.copyFile = copyFile; // createStream -exports.createReadStream = createReadStream; -exports.createWriteStream = createWriteStream; +exports.createReadStream = fs.createReadStream; +exports.createWriteStream = fs.createWriteStream; // emptyDir exports.emptyDir = emptyDir; @@ -575,7 +558,7 @@ exports.listDir = listDir; exports.listDirSync = listDirSync; // mkdir -exports.mkdir = mkdirAsync; +exports.mkdir = Promise.promisify(fs.mkdir); exports.mkdirSync = fs.mkdirSync; // mkdirs @@ -595,7 +578,7 @@ exports.read = Promise.promisify(fs.read); exports.readSync = fs.readSync; // readdir -exports.readdir = readdirAsync; +exports.readdir = Promise.promisify(fs.readdir); exports.readdirSync = fs.readdirSync; // readFile @@ -619,7 +602,7 @@ exports.rmdir = rmdir; exports.rmdirSync = rmdirSync; // stat -exports.stat = statAsync; +exports.stat = Promise.promisify(fs.stat); exports.statSync = fs.statSync; exports.fstat = Promise.promisify(fs.fstat); exports.fstatSync = fs.fstatSync; @@ -633,7 +616,7 @@ exports.ftruncate = Promise.promisify(fs.ftruncate); exports.ftruncateSync = fs.ftruncateSync; // unlink -exports.unlink = unlinkAsync; +exports.unlink = Promise.promisify(fs.unlink); exports.unlinkSync = fs.unlinkSync; // utimes diff --git a/package.json b/package.json index 067463f..3f5de35 100644 --- a/package.json +++ b/package.json @@ -43,6 +43,6 @@ "nyc": "^14.1.1" }, "engines": { - "node": ">=8" + "node": ">=10" } } From a843d707ba5865d37cf59c8cbd9daa7cd2acb1b3 Mon Sep 17 00:00:00 2001 From: segayuu Date: Thu, 9 Jan 2020 18:56:44 +0900 Subject: [PATCH 2/5] Useful fs.Dirent --- lib/fs.js | 91 +++++++++++++++++++++---------------------------------- 1 file changed, 34 insertions(+), 57 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index 7c6c0ed..41909d3 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -134,13 +134,13 @@ function trueFn() { function ignoreHiddenFiles(ignore) { if (!ignore) return trueFn; - return item => item[0] !== '.'; + return item => item.name[0] !== '.'; } function ignoreFilesRegex(regex) { if (!regex) return trueFn; - return item => !regex.test(item); + return item => !regex.test(item.name); } function ignoreExcludeFiles(arr, parent) { @@ -154,7 +154,7 @@ function ignoreExcludeFiles(arr, parent) { } return item => { - const path = join(parent, item.path); + const path = join(parent, item.name); return !map[path]; }; } @@ -169,44 +169,27 @@ function reduceFiles(result, item) { } function _readAndFilterDir(path, options) { - return Promise.resolve(fsPromises.readdir(path)) - .filter(ignoreHiddenFiles(options.ignoreHidden == null ? true : options.ignoreHidden)) - .filter(ignoreFilesRegex(options.ignorePattern)) - .map(item => { - const fullPath = join(path, item); - - return fsPromises.stat(fullPath).then(stats => ({ - isDirectory: stats.isDirectory(), - path: item, - fullPath - })); - }); + const { ignoreHidden = true, ignorePattern } = options; + return Promise.resolve(fsPromises.readdir(path, Object.assign(options, { withFileTypes: true }))) + .filter(ignoreHiddenFiles(ignoreHidden)) + .filter(ignoreFilesRegex(ignorePattern)); } function _readAndFilterDirSync(path, options) { - return fs.readdirSync(path) - .filter(ignoreHiddenFiles(options.ignoreHidden == null ? true : options.ignoreHidden)) - .filter(ignoreFilesRegex(options.ignorePattern)) - .map(item => { - const fullPath = join(path, item); - const stats = fs.statSync(fullPath); - - return { - isDirectory: stats.isDirectory(), - path: item, - fullPath - }; - }); + const { ignoreHidden = true, ignorePattern } = options; + return fs.readdirSync(path, Object.assign(options, { withFileTypes: true })) + .filter(ignoreHiddenFiles(ignoreHidden)) + .filter(ignoreFilesRegex(ignorePattern)); } function _copyDir(src, dest, options, parent) { return _readAndFilterDir(src, options) .map(item => { - const childSrc = item.fullPath; - const childDest = join(dest, item.path); - const currentPath = join(parent, item.path); + const childSrc = join(src, item.name); + const childDest = join(dest, item.name); + const currentPath = join(parent, item.name); - if (item.isDirectory) { + if (item.isDirectory()) { return _copyDir(childSrc, childDest, options, currentPath); } @@ -228,10 +211,10 @@ function copyDir(src, dest, options = {}, callback) { function _listDir(path, options, parent) { return _readAndFilterDir(path, options).map(item => { - const currentPath = join(parent, item.path); + const currentPath = join(parent, item.name); - if (item.isDirectory) { - return _listDir(item.fullPath, options, currentPath); + if (item.isDirectory()) { + return _listDir(join(path, item.name), options, currentPath); } return currentPath; @@ -251,10 +234,10 @@ function listDir(path, options = {}, callback) { function _listDirSync(path, options, parent) { return _readAndFilterDirSync(path, options).map(item => { - const currentPath = join(parent, item.path); + const currentPath = join(parent, item.name); - if (item.isDirectory) { - return _listDirSync(item.fullPath, options, currentPath); + if (item.isDirectory()) { + return _listDirSync(join(path, item.name), options, currentPath); } return currentPath; @@ -316,10 +299,10 @@ function _emptyDir(path, options, parent) { return _readAndFilterDir(path, options) .filter(ignoreExcludeFiles(options.exclude, parent)) .map(item => { - const fullPath = item.fullPath; - const currentPath = join(parent, item.path); + const fullPath = join(path, item.name); + const currentPath = join(parent, item.name); - if (item.isDirectory) { + if (item.isDirectory()) { return _emptyDir(fullPath, options, currentPath).tap(() => fs.promises.readdir(fullPath).then(files => { if (!files.length) return fsPromises.rmdir(fullPath); })); @@ -344,10 +327,10 @@ function _emptyDirSync(path, options, parent) { return _readAndFilterDirSync(path, options) .filter(ignoreExcludeFiles(options.exclude, parent)) .map(item => { - const childPath = item.fullPath; - const currentPath = join(parent, item.path); + const childPath = join(path, item.name); + const currentPath = join(parent, item.name); - if (item.isDirectory) { + if (item.isDirectory()) { const removed = _emptyDirSync(childPath, options, currentPath); if (!fs.readdirSync(childPath).length) { @@ -369,16 +352,10 @@ function emptyDirSync(path, options = {}) { } function _rmdir(path) { - return Promise.resolve(fsPromises.readdir(path)).map(item => { - const childPath = join(path, item); + return Promise.resolve(fsPromises.readdir(path, { withFileTypes: true })).map(item => { + const childPath = join(path, item.name); - return fsPromises.stat(childPath).then(stats => { - if (stats.isDirectory()) { - return _rmdir(childPath); - } - - return fsPromises.unlink(childPath); - }); + return item.isDirectory() ? _rmdir(childPath) : fsPromises.unlink(childPath); }).then(() => fsPromises.rmdir(path)); } @@ -389,13 +366,13 @@ function rmdir(path, callback) { } function _rmdirSync(path) { - const files = fs.readdirSync(path); + const files = fs.readdirSync(path, { withFileTypes: true }); for (let i = 0, len = files.length; i < len; i++) { - const childPath = join(path, files[i]); - const stats = fs.statSync(childPath); + const item = files[i]; + const childPath = join(path, item.name); - if (stats.isDirectory()) { + if (item.isDirectory()) { _rmdirSync(childPath); } else { fs.unlinkSync(childPath); From 4c802552579cc7294bbd1ec1899809eae0ad25cf Mon Sep 17 00:00:00 2001 From: segayuu Date: Thu, 9 Jan 2020 19:36:00 +0900 Subject: [PATCH 3/5] Useful async / await --- lib/fs.js | 142 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 79 insertions(+), 63 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index 41909d3..c803e6f 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -36,16 +36,27 @@ function existsSync(path) { return true; } -function mkdirs(path, callback) { - if (!path) throw new TypeError('path is required!'); - +async function _mkdirs(path) { const parent = dirname(path); - return exists(parent).then(exist => { - if (!exist) return mkdirs(parent); - }).then(() => fsPromises.mkdir(path)).catch(err => { + try { + await fsPromises.access(path); + } catch (err) { + if (err.code !== 'ENOENT') throw err; + await _mkdirs(parent); + } + + try { + await fsPromises.mkdir(path); + } catch (err) { if (err.code !== 'EEXIST') throw err; - }).asCallback(callback); + } +} + +function mkdirs(path, callback) { + if (!path) throw new TypeError('path is required!'); + + return Promise.resolve(_mkdirs(path)).asCallback(callback); } function mkdirsSync(path) { @@ -58,13 +69,7 @@ function mkdirsSync(path) { } function checkParent(path) { - if (!path) throw new TypeError('path is required!'); - - const parent = dirname(path); - - return exists(parent).then(exist => { - if (!exist) return mkdirs(parent); - }); + return Promise.resolve(_mkdirs(dirname(path))); } function checkParentSync(path) { @@ -168,9 +173,9 @@ function reduceFiles(result, item) { return result; } -function _readAndFilterDir(path, options) { +async function _readAndFilterDir(path, options) { const { ignoreHidden = true, ignorePattern } = options; - return Promise.resolve(fsPromises.readdir(path, Object.assign(options, { withFileTypes: true }))) + return (await fsPromises.readdir(path, Object.assign(options, { withFileTypes: true }))) .filter(ignoreHiddenFiles(ignoreHidden)) .filter(ignoreFilesRegex(ignorePattern)); } @@ -182,19 +187,19 @@ function _readAndFilterDirSync(path, options) { .filter(ignoreFilesRegex(ignorePattern)); } -function _copyDir(src, dest, options, parent) { - return _readAndFilterDir(src, options) - .map(item => { - const childSrc = join(src, item.name); - const childDest = join(dest, item.name); - const currentPath = join(parent, item.name); +async function _copyDir(src, dest, options, parent) { + const entrys = await _readAndFilterDir(src, options); + return Promise.reduce(entrys.map(item => { + const childSrc = join(src, item.name); + const childDest = join(dest, item.name); + const currentPath = join(parent, item.name); - if (item.isDirectory()) { - return _copyDir(childSrc, childDest, options, currentPath); - } + if (item.isDirectory()) { + return _copyDir(childSrc, childDest, options, currentPath); + } - return copyFile(childSrc, childDest, options).thenReturn(currentPath); - }).reduce(reduceFiles, []); + return copyFile(childSrc, childDest, options).thenReturn(currentPath); + }), reduceFiles, []); } function copyDir(src, dest, options = {}, callback) { @@ -209,8 +214,9 @@ function copyDir(src, dest, options = {}, callback) { return checkParent(dest).then(() => _copyDir(src, dest, options, '')).asCallback(callback); } -function _listDir(path, options, parent) { - return _readAndFilterDir(path, options).map(item => { +async function _listDir(path, options, parent) { + const entrys = await _readAndFilterDir(path, options); + return Promise.reduce(entrys.map(item => { const currentPath = join(parent, item.name); if (item.isDirectory()) { @@ -218,7 +224,7 @@ function _listDir(path, options, parent) { } return currentPath; - }).reduce(reduceFiles, []); + }), reduceFiles, []); } function listDir(path, options = {}, callback) { @@ -229,7 +235,7 @@ function listDir(path, options = {}, callback) { options = {}; } - return _listDir(path, options, '').asCallback(callback); + return Promise.resolve(_listDir(path, options, '')).asCallback(callback); } function _listDirSync(path, options, parent) { @@ -262,6 +268,18 @@ function escapeFileContent(content) { return escapeBOM(escapeEOL(content)); } +async function _readFile(path, options) { + if (!Object.prototype.hasOwnProperty.call(options, 'encoding')) options.encoding = 'utf8'; + + const content = await fsPromises.readFile(path, options); + + if (options.escape == null || options.escape) { + return escapeFileContent(content); + } + + return content; +} + function readFile(path, options = {}, callback) { if (!path) throw new TypeError('path is required!'); @@ -270,15 +288,7 @@ function readFile(path, options = {}, callback) { options = {}; } - if (!Object.prototype.hasOwnProperty.call(options, 'encoding')) options.encoding = 'utf8'; - - return Promise.resolve(fs.promises.readFile(path, options)).then(content => { - if (options.escape == null || options.escape) { - return escapeFileContent(content); - } - - return content; - }).asCallback(callback); + return Promise.resolve(_readFile(path, options)).asCallback(callback); } function readFileSync(path, options = {}) { @@ -295,21 +305,22 @@ function readFileSync(path, options = {}) { return content; } -function _emptyDir(path, options, parent) { - return _readAndFilterDir(path, options) - .filter(ignoreExcludeFiles(options.exclude, parent)) - .map(item => { - const fullPath = join(path, item.name); - const currentPath = join(parent, item.name); +async function _emptyDir(path, options, parent) { + const entrys = (await _readAndFilterDir(path, options)) + .filter(ignoreExcludeFiles(options.exclude, parent)); + return Promise.reduce(entrys.map(item => { + const fullPath = join(path, item.name); + const currentPath = join(parent, item.name); - if (item.isDirectory()) { - return _emptyDir(fullPath, options, currentPath).tap(() => fs.promises.readdir(fullPath).then(files => { - if (!files.length) return fsPromises.rmdir(fullPath); - })); - } + if (item.isDirectory()) { + return _emptyDir(fullPath, options, currentPath).then(files => { + if (!files.length) return fsPromises.rmdir(fullPath).then(() => files); + return files; + }); + } - return fsPromises.unlink(fullPath).then(() => currentPath); - }).reduce(reduceFiles, []); + return fsPromises.unlink(fullPath).then(() => currentPath); + }), reduceFiles, []); } function emptyDir(path, options = {}, callback) { @@ -320,7 +331,7 @@ function emptyDir(path, options = {}, callback) { options = {}; } - return _emptyDir(path, options, '').asCallback(callback); + return Promise.resolve(_emptyDir(path, options, '')).asCallback(callback); } function _emptyDirSync(path, options, parent) { @@ -351,18 +362,20 @@ function emptyDirSync(path, options = {}) { return _emptyDirSync(path, options, ''); } -function _rmdir(path) { - return Promise.resolve(fsPromises.readdir(path, { withFileTypes: true })).map(item => { +async function _rmdir(path) { + const files = await fsPromises.readdir(path, { withFileTypes: true }); + await Promise.all(files.map(item => { const childPath = join(path, item.name); return item.isDirectory() ? _rmdir(childPath) : fsPromises.unlink(childPath); - }).then(() => fsPromises.rmdir(path)); + })); + return fsPromises.rmdir(path); } function rmdir(path, callback) { if (!path) throw new TypeError('path is required!'); - return _rmdir(path).asCallback(callback); + return Promise.resolve(_rmdir(path)).asCallback(callback); } function _rmdirSync(path) { @@ -425,14 +438,17 @@ function _findUnusedPath(path, files) { return join(dirname(path), `${base}-${num + 1}${ext}`); } +async function _ensurePath(path) { + if (!await exists(path)) return path; + + const files = await fsPromises.readdir(dirname(path)); + return _findUnusedPath(path, files); +} + function ensurePath(path, callback) { if (!path) throw new TypeError('path is required!'); - return exists(path).then(exist => { - if (!exist) return path; - - return fsPromises.readdir(dirname(path)).then(files => _findUnusedPath(path, files)); - }).asCallback(callback); + return Promise.resolve(_ensurePath(path)).asCallback(callback); } function ensurePathSync(path) { From c0236933950c359f9e69042a514b34790d8fb87e Mon Sep 17 00:00:00 2001 From: segayuu Date: Thu, 9 Jan 2020 19:38:15 +0900 Subject: [PATCH 4/5] Useful Set Object --- lib/fs.js | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index c803e6f..4f39372 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -151,17 +151,9 @@ function ignoreFilesRegex(regex) { function ignoreExcludeFiles(arr, parent) { if (!arr || !arr.length) return trueFn; - // Build a map to make it faster. - const map = {}; + const set = new Set(arr); - for (let i = 0, len = arr.length; i < len; i++) { - map[arr[i]] = true; - } - - return item => { - const path = join(parent, item.name); - return !map[path]; - }; + return item => !set.has(join(parent, item.name)); } function reduceFiles(result, item) { From 9722cf476d8777e461f1c55277b6ff298bbc7255 Mon Sep 17 00:00:00 2001 From: segayuu Date: Thu, 9 Jan 2020 19:40:10 +0900 Subject: [PATCH 5/5] Remove unnecessary test function call --- lib/fs.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index 4f39372..5d4ba1e 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -417,9 +417,9 @@ function _findUnusedPath(path, files) { for (let i = 0, len = files.length; i < len; i++) { const item = files[i]; - if (!regex.test(item)) continue; - const match = item.match(regex); + + if (match == null) continue; const matchNum = match[1] ? parseInt(match[1], 10) : 0; if (matchNum > num) {