Skip to content

Commit

Permalink
change match all params
Browse files Browse the repository at this point in the history
  • Loading branch information
mhkeller committed Nov 16, 2015
1 parent 750a149 commit 174aefd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
20 changes: 10 additions & 10 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -585,8 +585,8 @@ function readdir (modeInfo, dirPath, _opts, cb) {
_opts.exclude = strToArray(_opts.exclude)

// Set defaults if not provided
_opts.includeMode = _opts.includeMode || 'some'
_opts.excludeMode = _opts.excludeMode || 'some'
_opts.includeMatchAll = (_opts.includeMatchAll) ? 'every' : 'some'
_opts.excludeMatchAll = (_opts.excludeMatchAll) ? 'every' : 'some'

if (asyncMode === true) {
fs.readdir(dirPath, function (err, files) {
Expand All @@ -610,7 +610,7 @@ function readdir (modeInfo, dirPath, _opts, cb) {

// Don't include if matches exclusion matcher
if (_opts.exclude) {
isExcluded = _opts.exclude[_opts.excludeMode](function (matcher) {
isExcluded = _opts.exclude[_opts.excludeMatchAll](function (matcher) {
return helpers.matches(fileName, matcher)
})
if (isExcluded === true) {
Expand All @@ -619,7 +619,7 @@ function readdir (modeInfo, dirPath, _opts, cb) {
}

if (_opts.include) {
isIncluded = _opts.include[_opts.includeMode](function (matcher) {
isIncluded = _opts.include[_opts.includeMatchAll](function (matcher) {
return helpers.matches(fileName, matcher)
})
return isIncluded
Expand Down Expand Up @@ -647,8 +647,8 @@ function readdir (modeInfo, dirPath, _opts, cb) {
* @param {Object} options Filter options, see below
* @param {String|RegExp|Array<String>|Array<RegExp>} options.include If given a string, return files that have that string as their extension. If given a Regular Expression, return the file that matches the pattern. Can also take a list of both. List matching behavior is described in `includeAll`.
* @param {String|RegExp|Array<String>|Array<RegExp>} options.exclude If given a string, return files that do not have that string as their extension. If given a Regular Expression, return the file that matches the pattern. Can also take a list of both. List matching behavior is described in `excludeAll`.
* @param {String} [options.includeMode='some'] Can be `'every'` or `'some'`. The former will require every include conditions to be met for the target to be included. The latter will only require one. Default is `'some'`.
* @param {String} [options.excludeMode='some'] Can be `'every'` or `'some'`. The former will require some exclude conditions to be met for the target to be excluded. The latter will only require one. Default is `'some'`.
* @param {String} [options.includeMatchAll=false] If true, require all include conditions to be met for a file to be included. Default is `false`.
* @param {String} [options.excludeMatchAll=false] If true, require all exclude conditions to be met for a file to be excluded. Default is `false`.
* @param {String} [options.fullPath=false] If `true` the full path of the file, otherwise return just the file name.
* @param {Function} callback Callback fired with signature of `(err, files)` where `files` is a list of matching file names.
*
Expand All @@ -674,8 +674,8 @@ readers.readdirFilter = function (dirPath, _opts, cb) {
* @param {Object} options filter options, see below
* @param {String|RegExp|Array<String>|Array<RegExp>} options.include if given a string, return files that have that string as their extension. If given a Regular Expression, return the file that matches the pattern. Can also take a list of both. List matching behavior is described in `includeAll`.
* @param {String|RegExp|Array<String>|Array<RegExp>} options.exclude if given a string, return files that do not have that string as their extension. If given a Regular Expression, return the file that matches the pattern. Can also take a list of both. List matching behavior is described in `excludeAll`.
* @param {String} [options.includeMode='some'] Can be `'every'` or `'some'`. The former will require every include conditions to be met for the target to be included. The latter will only require one. Default is `'some'`.
* @param {String} [options.excludeMode='some'] Can be `'every'` or `'some'`. The former will require some exclude conditions to be met for the target to be excluded. The latter will only require one. Default is `'some'`.
* @param {String} [options.includeMatchAll=false] If true, require all include conditions to be met for a file to be included. Default is `false`.
* @param {String} [options.excludeMatchAll=false] If true, require all exclude conditions to be met for a file to be excluded. Default is `false`.
* @param {String} [options.fullPath=false] If `true` the full path of the file, otherwise return just the file name.
* @returns {Array<String>} The matching file names
*
Expand All @@ -687,8 +687,8 @@ readers.readdirFilter = function (dirPath, _opts, cb) {
* var files = io.readdirFilterSync('path/to/files', {include: [/^data/], exclude: 'json', fullPath: true})
* console.log(files) // ['path/to/files/data-0.csv', 'path/to/files/data-1.csv', 'path/to/files/data-0.tsv']
*
* var files = io.readdirFilterSync('path/to/files', {include: [/^data/], exclude: 'json', fullPath: true, includeMode: 'some'})
* console.log(files) // ['path/to/files/data-0.csv', 'path/to/files/data-1.csv', 'path/to/files/data-0.tsv']
* var files = io.readdirFilterSync('path/to/files', {include: [/^data/, 'json'], fullPath: true, includeMatchAll: true})
* console.log(files) // ['path/to/files/data-0.json', 'path/to/files/data-1.json']
*
*/
readers.readdirFilterSync = function (dirPath, _opts) {
Expand Down
20 changes: 17 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,11 +352,10 @@ describe('readdirFilter()', function () {
})
})

describe('include every', function () {
describe('includeMatchAll', function () {
it('match expected output', function (done) {
var dir = path.join(__dirname, 'data', 'mixed')
io.readdirFilter(dir, {include: [/^data-1/, 'json'], includeMode: 'every'}, function (err, files) {
console.log(JSON.stringify(files))
io.readdirFilter(dir, {include: [/^data-1/, 'json'], includeMatchAll: true}, function (err, files) {
assert(_.isEqual(JSON.stringify(files), '["data-1.json"]'))
if (err) {
console.error(err)
Expand All @@ -367,6 +366,21 @@ describe('readdirFilter()', function () {
})
})

describe('excludeMatchAll', function () {
it('match expected output', function (done) {
var dir = path.join(__dirname, 'data', 'mixed')
io.readdirFilter(dir, {exclude: [/^data-1/, 'json'], excludeMatchAll: true}, function (err, files) {
console.log(JSON.stringify(files))
assert(_.isEqual(JSON.stringify(files), '[".hidden-file","data-0.csv","data-0.json","data-0.tsv","data-1.csv"]'))
if (err) {
console.error(err)
}
done()
})

})
})

describe('exclude by extension list and regex', function () {
it('match expected output', function (done) {
var dir = path.join(__dirname, 'data', 'mixed')
Expand Down

0 comments on commit 174aefd

Please sign in to comment.