Skip to content

Commit

Permalink
tests for readdirExclude with dirpath
Browse files Browse the repository at this point in the history
  • Loading branch information
mhkeller committed Jul 30, 2015
1 parent 65c1328 commit 7e0c8a1
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
22 changes: 19 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -426,16 +426,25 @@ readers.readdirIncludeSync = function (dirPath, includes, includeFullPath) {
*
* @param {String} dirPath the directory to read from
* @param {Array<String>|String} includes the file extention(s) to include
* @param {Boolean} [includeFullPath=false] return the `dirPath` in the result
* @param {Function} callback the callback that will accept the filtered files, takes an optional error and an array of file names
*/
readers.readdirExclude = function (dirPath, excludes, cb) {
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 !extensionMatches(file, excludeExtension) })
})
if (includeFullPath === true) {
filtered = filtered.map(function (fileName) {
return path.join(dirPath, fileName)
})
}
cb(err, filtered)
})
}
Expand All @@ -445,15 +454,22 @@ readers.readdirExclude = function (dirPath, excludes, cb) {
*
* @param {String} dirPath the directory to read from
* @param {Array<String>|String} includes the file extention(s) to include
* @param {Boolean} [includeFullPath=false] return the `dirPath` in the result
* @returns {Array<String>} the matching files' paths
*/
readers.readdirExcludeSync = function (dirPath, excludes, cb) {
readers.readdirExcludeSync = function (dirPath, excludes, includeFullPath) {
if (typeof excludes === 'string') {
excludes = [excludes]
}
return fs.readdirSync(dirPath).filter(function (file) {
var filtered = fs.readdirSync(dirPath).filter(function (file) {
return _.some(excludes, function (excludeExtension) { return !extensionMatches(file, excludeExtension) })
})
if (includeFullPath === true) {
filtered = filtered.map(function (fileName) {
return path.join(dirPath, fileName)
})
}
return filtered
}

/** @namespace */
Expand Down
3 changes: 3 additions & 0 deletions test/data/other/this_is_not_a_txt.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name,occupation,height
jim,land surveyor,70
francis,conductor,63
25 changes: 22 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ describe('readdirInclude()', function () {

describe('extension in filename', function () {
it('should be empty', function (done) {
var dir = path.join(__dirname, 'data', 'other')
var dir = path.join(__dirname, 'data', 'json')
io.readdirInclude(dir, 'csv', function (err, files) {
assert.lengthOf(files, 0)
if (err) {
Expand Down Expand Up @@ -174,7 +174,7 @@ describe('readdirIncludeSync()', function () {

describe('extension in filename', function () {
it('should be empty', function () {
var dir = path.join(__dirname, 'data', 'other')
var dir = path.join(__dirname, 'data', 'json')
assert.lengthOf(io.readdirIncludeSync(dir, 'csv'), 0)
})
})
Expand All @@ -184,7 +184,6 @@ describe('readdirIncludeSync()', function () {
var dir = path.join(__dirname, 'data', 'csv')
var files = io.readdirIncludeSync(dir, 'csv', true)
assert.equal(files.indexOf(path.join(dir, 'basic.csv')), 0)

})
})
})
Expand Down Expand Up @@ -228,6 +227,18 @@ describe('readdirExclude()', function () {
})
})
})

describe('dirPath in filename', function () {
it('should match expected output', function (done) {
var dir = path.join(__dirname, 'data', 'other')
io.readdirExclude(dir, 'txt', true, function (err, files) {
if (err) {
console.log(err)
}
done(assert.equal(files.indexOf(path.join(dir, 'this_is_not_a_txt.csv')), 0))
})
})
})
})

describe('readdirExcludeSync()', function () {
Expand All @@ -251,6 +262,14 @@ describe('readdirExcludeSync()', function () {
assert.isAbove(io.readdirExcludeSync(dir, 'csv').length, 0)
})
})

describe('dirPath in filename', function () {
it('should match expected output', function () {
var dir = path.join(__dirname, 'data', 'other')
var files = io.readdirExcludeSync(dir, 'csv', true)
assert.equal(files.indexOf(path.join(dir, 'this_is_not_a_csv.txt')), 0)
})
})
})

describe('discernFormat()', function () {
Expand Down

0 comments on commit 7e0c8a1

Please sign in to comment.