Skip to content

Commit

Permalink
tests for including full dir in readdirinclude path
Browse files Browse the repository at this point in the history
  • Loading branch information
mhkeller committed Jul 30, 2015
1 parent d90ce6a commit 65c1328
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 18 deletions.
44 changes: 30 additions & 14 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,50 +373,66 @@ function extensionMatches (file, extension) {
/**
* Get a list of the files in a directory with selected extentions.
*
* @param {String} path the directory to read from
* @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.readdirInclude = function (path, includes, cb) {
readers.readdirInclude = function (dirPath, includes, includeFullPath, cb) {
if (typeof cb !== 'function') {
cb = includeFullPath
}
if (typeof includes === 'string') {
includes = [includes]
}
fs.readdir(path, function (err, files) {
var filtered = files.filter(function (file) {
return _.some(includes, function (includeExtension) { return extensionMatches(file, includeExtension) })
fs.readdir(dirPath, function (err, files) {
var filtered = files.filter(function (fileName) {
return _.some(includes, function (includeExtension) { return extensionMatches(fileName, includeExtension) })
})
if (includeFullPath === true) {
filtered = filtered.map(function (fileName) {
return path.join(dirPath, fileName)
})
}
cb(err, filtered)
})
}

/**
* Synchronously get a list of the files in a directory with selected extentions.
*
* @param {String} path the directory to read from
* @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.readdirIncludeSync = function (path, includes) {
readers.readdirIncludeSync = function (dirPath, includes, includeFullPath) {
if (typeof includes === 'string') {
includes = [includes]
}
return fs.readdirSync(path).filter(function (file) {
var filtered = fs.readdirSync(dirPath).filter(function (file) {
return _.some(includes, function (includeExtension) { return extensionMatches(file, includeExtension) })
})
if (includeFullPath === true) {
filtered = filtered.map(function (fileName) {
return path.join(dirPath, fileName)
})
}
return filtered
}

/**
* Get a list of the files in a directory that do not have the selected extentions.
*
* @param {String} path the directory to read from
* @param {String} dirPath the directory to read from
* @param {Array<String>|String} includes the file extention(s) to include
* @param {Function} callback the callback that will accept the filtered files, takes an optional error and an array of file names
*/
readers.readdirExclude = function (path, excludes, cb) {
readers.readdirExclude = function (dirPath, excludes, cb) {
if (typeof excludes === 'string') {
excludes = [excludes]
}
fs.readdir(path, function (err, files) {
fs.readdir(dirPath, function (err, files) {
var filtered = files.filter(function (file) {
return _.some(excludes, function (excludeExtension) { return !extensionMatches(file, excludeExtension) })
})
Expand All @@ -427,15 +443,15 @@ readers.readdirExclude = function (path, excludes, cb) {
/**
* Synchronously get a list of the files in a directory that do not have the selected extentions.
*
* @param {String} path the directory to read from
* @param {String} dirPath the directory to read from
* @param {Array<String>|String} includes the file extention(s) to include
* @returns {Array<String>} the matching files' paths
*/
readers.readdirExcludeSync = function (path, excludes, cb) {
readers.readdirExcludeSync = function (dirPath, excludes, cb) {
if (typeof excludes === 'string') {
excludes = [excludes]
}
return fs.readdirSync(path).filter(function (file) {
return fs.readdirSync(dirPath).filter(function (file) {
return _.some(excludes, function (excludeExtension) { return !extensionMatches(file, excludeExtension) })
})
}
Expand Down
29 changes: 25 additions & 4 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,20 +112,20 @@ describe('readdirInclude()', function () {
io.readdirInclude(__dirname, 'csv', function (err, files) {
assert.lengthOf(files, 0)
if (err) {
console.error(err)
console.log(err)
}
done()
})
})
})

describe('actual extension', function () {
it('should not be empty', function (done) {
it('should find csv files', function (done) {
var dir = path.join(__dirname, 'data', 'csv')
io.readdirInclude(dir, 'csv', function (err, files) {
assert.isAbove(files.length, 0)
if (err) {
console.error(err)
console.log(err)
}
done()
})
Expand All @@ -138,12 +138,24 @@ describe('readdirInclude()', function () {
io.readdirInclude(dir, 'csv', function (err, files) {
assert.lengthOf(files, 0)
if (err) {
console.error(err)
console.log(err)
}
done()
})
})
})

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

describe('readdirIncludeSync()', function () {
Expand All @@ -166,6 +178,15 @@ describe('readdirIncludeSync()', function () {
assert.lengthOf(io.readdirIncludeSync(dir, 'csv'), 0)
})
})

describe('dirPath in filename', function () {
it('should match expected output', 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)

})
})
})

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

0 comments on commit 65c1328

Please sign in to comment.