Skip to content

Commit

Permalink
support regexp in readdir fns
Browse files Browse the repository at this point in the history
  • Loading branch information
mhkeller committed Sep 7, 2015
1 parent 84e8edf commit 44bfb9e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 10 deletions.
53 changes: 45 additions & 8 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,21 +122,57 @@ helpers.existsSync = function (filename) {
}

/**
* Test whether a file extension matches a given string
* Test whether a file name has the given extension
*
* @param {String} fileName the name of the file
* @returns {Boolean} extension the extension to test
* @returns {String} extension the extension to test
*
* @example
* var matches = io.extensionMatches('path/to/data.tsv', 'tsv')
* console.log(matches) // `true`
*/
helpers.extensionMatches = function (fileName, extension) {
// Chop '.' off extension returned by extname
console.log(fileName, extension)
return path.extname(fileName).slice(1) === extension
}

/**
* Test whether a string matches a given Regular Expression
*
* @param {String} fileName the name of the file
* @returns {RegExp} regEx the regEx to match with
*
* @example
* var matches = io.matchRegExp('.gitignore', /\.gitignore/)
* console.log(matches) // `true`
*/
helpers.matchRegExp = function (fileName, regEx) {
return regEx.test(fileName)
}

/**
* Test whether a file name matches a given matcher. Delegates to {@link helpers#extensionMatches} if `matcher` is a string`, {@link helpers#matchRegExp} if Regular Expression
*
* @param {String} fileName the name of the file
* @returns {String} matcher the string to match with
*
* @example
* var matches = io.matches('path/to/data.tsv', 'tsv')
* console.log(matches) // `true`
*
* var matches = io.matches('.gitignore', /\.gitignore/)
* console.log(matches) // `true`
*/
helpers.matches = function (fileName, matcher) {
if (typeof matcher === 'string') {
return helpers.extensionMatches(fileName, matcher)
} else if (_.isRegExp(matcher)) {
return helpers.matchRegExp(fileName, matcher)
} else {
throw new Error('Matcher argument must be String or Regular Expression')
}
}

/** @namespace */
var readers = {}

Expand Down Expand Up @@ -387,6 +423,7 @@ readers.readDbf = function (path, cb) {

}

// Used internally to by `readdir` functions to make more DRY
function readdir (modeInfo, dirPath, inOrExcludesStr, includeFullPath, cb) {
if (typeof cb !== 'function' && modeInfo.async === true) {
cb = includeFullPath
Expand All @@ -411,7 +448,7 @@ function readdir (modeInfo, dirPath, inOrExcludesStr, includeFullPath, cb) {
throw err
}
var filtered = files.filter(function (fileName) {
return _.some(inOrExcludesArr, function (includeExtension) { return _.isEqual(modeInfo.include, helpers.extensionMatches(fileName, includeExtension)) })
return _.some(inOrExcludesArr, function (includeExtension) { return _.isEqual(modeInfo.include, helpers.matches(fileName, includeExtension)) })
})
if (includeFullPath === true) {
filtered = filtered.map(function (fileName) {
Expand All @@ -431,7 +468,7 @@ function readdir (modeInfo, dirPath, inOrExcludesStr, includeFullPath, cb) {
* Get a list of the files in a directory with selected extentions.
*
* @param {String} dirPath the directory to read from
* @param {Array<String>|String} includes the file extention(s) to include
* @param {Array<String>|String|Array<RegExp>|RegExp} includes the file extention(s) to include or RegExp matching file name
* @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
*
Expand All @@ -453,7 +490,7 @@ readers.readdirInclude = function (dirPath, includesStr, includeFullPath, cb) {
* Synchronously get a list of the files in a directory with selected extentions.
*
* @param {String} dirPath the directory to read from
* @param {Array<String>|String} includes the file extention(s) to include
* @param {Array<String>|String|Array<RegExp>|RegExp} includes the file extention(s) to include or RegExp matching file name
* @param {Boolean} [includeFullPath=false] return the `dirPath` in the result
* @returns {Array<String>} the matching files' paths
*
Expand All @@ -473,7 +510,7 @@ readers.readdirIncludeSync = function (dirPath, includesStr, includeFullPath) {
* Get a list of the files in a directory that do not have the selected extentions.
*
* @param {String} dirPath the directory to read from
* @param {Array<String>|String} includes the file extention(s) to include
* @param {Array<String>|String|Array<RegExp>|RegExp} excludes the file extention(s) to exclude or RegExp matching file name
* @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
*
Expand All @@ -495,7 +532,7 @@ readers.readdirExclude = function (dirPath, excludesStr, includeFullPath, cb) {
* Synchronously get a list of the files in a directory that do not have the selected extentions.
*
* @param {String} dirPath the directory to read from
* @param {Array<String>|String} includes the file extention(s) to include
* @param {Array<String>|String|Array<RegExp>|RegExp} excludes the file extention(s) to exclude or RegExp matching file name
* @param {Boolean} [includeFullPath=false] return the `dirPath` in the result
* @returns {Array<String>} the matching files' paths
*
Expand Down
1 change: 1 addition & 0 deletions test/data/mixed/.hidden-file
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A hidden file
12 changes: 10 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,14 @@ describe('readdirIncludeSync()', function () {
assert.equal(files.indexOf(path.join(dir, 'basic.csv')), 0)
})
})

describe('use regex', function () {
it('should match expected output', function () {
var dir = path.join(__dirname, 'data', 'mixed')
var files = io.readdirIncludeSync(dir, /\.*/)
assert.notEqual(files.indexOf('.hidden-file'), -1)
})
})
})

describe('readdirExclude()', function () {
Expand Down Expand Up @@ -275,7 +283,7 @@ describe('readdirExclude()', function () {
if (err) {
console.log(err)
}
done(assert.equal(files.indexOf(path.join(dir, 'this_is_not_a_txt.csv')), 0))
done(assert.notEqual(files.indexOf(path.join(dir, 'this_is_not_a_txt.csv')), -1))
})
})
})
Expand Down Expand Up @@ -307,7 +315,7 @@ describe('readdirExcludeSync()', function () {
it('should match expected output', function () {
var dir = path.join(__dirname, 'data', 'mixed')
var files = io.readdirExcludeSync(dir, 'csv', true)
assert.equal(files.indexOf(path.join(dir, 'this_is_not_a_csv.txt')), 0)
assert.notEqual(files.indexOf(path.join(dir, 'this_is_not_a_csv.txt')), -1)
})
})
})
Expand Down

0 comments on commit 44bfb9e

Please sign in to comment.