Skip to content

Commit

Permalink
refactor readdir fns
Browse files Browse the repository at this point in the history
  • Loading branch information
mhkeller committed Sep 7, 2015
1 parent 8203529 commit 84e8edf
Showing 1 changed file with 48 additions and 62 deletions.
110 changes: 48 additions & 62 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,46 @@ readers.readDbf = function (path, cb) {

}

function readdir (modeInfo, dirPath, inOrExcludesStr, includeFullPath, cb) {
if (typeof cb !== 'function' && modeInfo.async === true) {
cb = includeFullPath
}
var inOrExcludesArr
if (!_.isArray(inOrExcludesStr)) {
inOrExcludesArr = [inOrExcludesStr]
} else {
inOrExcludesArr = inOrExcludesStr
}
if (modeInfo.async === true) {
fs.readdir(dirPath, filter(modeInfo.async))
} else {
// This looks slightly ugly to make it work both async and sync
// But it works nicely and is pretty succinct
return filter(modeInfo.async)(null, fs.readdirSync(dirPath))
}

function filter (async) {
return function (err, files) {
if (err) {
throw err
}
var filtered = files.filter(function (fileName) {
return _.some(inOrExcludesArr, function (includeExtension) { return _.isEqual(modeInfo.include, helpers.extensionMatches(fileName, includeExtension)) })
})
if (includeFullPath === true) {
filtered = filtered.map(function (fileName) {
return path.join(dirPath, fileName)
})
}
if (async) {
cb(err, filtered)
} else {
return filtered
}
}
}
}

/**
* Get a list of the files in a directory with selected extentions.
*
Expand All @@ -405,24 +445,8 @@ readers.readDbf = function (path, cb) {
* })
*
*/
readers.readdirInclude = function (dirPath, includes, includeFullPath, cb) {
if (typeof cb !== 'function') {
cb = includeFullPath
}
if (typeof includes === 'string') {
includes = [includes]
}
fs.readdir(dirPath, function (err, files) {
var filtered = files.filter(function (fileName) {
return _.some(includes, function (includeExtension) { return helpers.extensionMatches(fileName, includeExtension) })
})
if (includeFullPath === true) {
filtered = filtered.map(function (fileName) {
return path.join(dirPath, fileName)
})
}
cb(err, filtered)
})
readers.readdirInclude = function (dirPath, includesStr, includeFullPath, cb) {
readdir({include: true, async: true}, dirPath, includesStr, includeFullPath, cb)
}

/**
Expand All @@ -441,19 +465,8 @@ readers.readdirInclude = function (dirPath, includes, includeFullPath, cb) {
* console.log(files) // ['path/to/files/data-0.csv', 'path/to/files/data-1.csv', 'path/to/files/data-0.tsv']
*
*/
readers.readdirIncludeSync = function (dirPath, includes, includeFullPath) {
if (typeof includes === 'string') {
includes = [includes]
}
var filtered = fs.readdirSync(dirPath).filter(function (file) {
return _.some(includes, function (includeExtension) { return helpers.extensionMatches(file, includeExtension) })
})
if (includeFullPath === true) {
filtered = filtered.map(function (fileName) {
return path.join(dirPath, fileName)
})
}
return filtered
readers.readdirIncludeSync = function (dirPath, includesStr, includeFullPath) {
return readdir({include: true, async: false}, dirPath, includesStr, includeFullPath)
}

/**
Expand All @@ -474,24 +487,8 @@ readers.readdirIncludeSync = function (dirPath, includes, includeFullPath) {
* })
*
*/
readers.readdirExclude = function (dirPath, excludes, includeFullPath, cb) {
if (typeof cb !== 'function') {
cb = includeFullPath
}
if (typeof excludes === 'string') {
excludes = [excludes]
}
fs.readdir(dirPath, function (err, files) {
var filtered = files.filter(function (file) {
return _.some(excludes, function (excludeExtension) { return !helpers.extensionMatches(file, excludeExtension) })
})
if (includeFullPath === true) {
filtered = filtered.map(function (fileName) {
return path.join(dirPath, fileName)
})
}
cb(err, filtered)
})
readers.readdirExclude = function (dirPath, excludesStr, includeFullPath, cb) {
readdir({include: false, async: true}, dirPath, excludesStr, includeFullPath, cb)
}

/**
Expand All @@ -510,19 +507,8 @@ readers.readdirExclude = function (dirPath, excludes, includeFullPath, cb) {
* console.log(files) // ['path/to/files/data-0.json']
*
*/
readers.readdirExcludeSync = function (dirPath, excludes, includeFullPath) {
if (typeof excludes === 'string') {
excludes = [excludes]
}
var filtered = fs.readdirSync(dirPath).filter(function (file) {
return _.some(excludes, function (excludeExtension) { return !helpers.extensionMatches(file, excludeExtension) })
})
if (includeFullPath === true) {
filtered = filtered.map(function (fileName) {
return path.join(dirPath, fileName)
})
}
return filtered
readers.readdirExcludeSync = function (dirPath, excludesStr, includeFullPath) {
return readdir({include: false, async: false}, dirPath, excludesStr, includeFullPath)
}

/** @namespace */
Expand Down

0 comments on commit 84e8edf

Please sign in to comment.