Skip to content

Commit

Permalink
read only files or only dirs
Browse files Browse the repository at this point in the history
  • Loading branch information
mhkeller committed Nov 16, 2015
1 parent 174aefd commit 8630895
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 6 deletions.
73 changes: 68 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ function readdir (modeInfo, dirPath, _opts, cb) {
if (typeof cb === undefined && modeInfo.async === true) {
cb = _opts
}
var asyncMode = modeInfo.async
var isAsync = modeInfo.async

// Convert to array if a string
_opts.include = strToArray(_opts.include)
Expand All @@ -588,12 +588,15 @@ function readdir (modeInfo, dirPath, _opts, cb) {
_opts.includeMatchAll = (_opts.includeMatchAll) ? 'every' : 'some'
_opts.excludeMatchAll = (_opts.excludeMatchAll) ? 'every' : 'some'

if (asyncMode === true) {
if (isAsync === true) {
fs.readdir(dirPath, function (err, files) {
cb(err, filter(files))
if (err) {
throw err
}
filter(files, cb)
})
} else {
return filter(fs.readdirSync(dirPath))
return filterSync(fs.readdirSync(dirPath))
}

function strToArray (val) {
Expand All @@ -603,7 +606,35 @@ function readdir (modeInfo, dirPath, _opts, cb) {
return val
}

function filter (files) {
function filterByType (file, cb) {
var filePath = (_opts.fullPath) ? file : path.join(dirPath, file)
if (isAsync === true) {
fs.stat(filePath, function (err, stats) {
var filtered = getFiltered(stats.isDirectory())
cb(err, filtered)
})
} else {
return getFiltered(fs.statSync(filePath).isDirectory())
}

function getFiltered (isDir) {
if (_opts.skipDirectories) {
if (isDir) {
return false
}
}
if (_opts.skipFiles) {
if (!isDir) {
return false
}
}
return file
}

}

function filterByMatchers (files) {

var filtered = files.filter(function (fileName) {
var isExcluded
var isIncluded
Expand All @@ -618,13 +649,15 @@ function readdir (modeInfo, dirPath, _opts, cb) {
}
}

// Include if matches inclusion matcher, exclude if it doesn't
if (_opts.include) {
isIncluded = _opts.include[_opts.includeMatchAll](function (matcher) {
return helpers.matches(fileName, matcher)
})
return isIncluded
}

// Return true if it makes it to here
return true

})
Expand All @@ -635,7 +668,33 @@ function readdir (modeInfo, dirPath, _opts, cb) {
return path.join(dirPath, fileName)
})
}

return filtered

}

function filterSync (files) {

var filtered = filterByMatchers(files)

return filtered.map(function (file) {
return filterByType(file)
}).filter(_.identity)
}

function filter (files, cb) {
var filterQ = queue()

var filtered = filterByMatchers(files)

filtered.forEach(function (fileName) {
filterQ.defer(filterByType, fileName)
})

filterQ.awaitAll(function (err, namesOfType) {
cb(err, namesOfType.filter(_.identity))
})

}

}
Expand All @@ -650,6 +709,8 @@ function readdir (modeInfo, dirPath, _opts, cb) {
* @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 {String} [options.skipFiles=false] If `true`, only include directories. Default is `false`.
* @param {String} [options.skipDirectories=false] If `true`, only include files. Default is `false`.
* @param {Function} callback Callback fired with signature of `(err, files)` where `files` is a list of matching file names.
*
* @example
Expand Down Expand Up @@ -677,6 +738,8 @@ readers.readdirFilter = function (dirPath, _opts, cb) {
* @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 {String} [options.skipFiles=false] If `true`, only include directories. Default is `false`.
* @param {String} [options.skipDirectories=false] If `true`, only include files. Default is `false`.
* @returns {Array<String>} The matching file names
*
* @example
Expand Down
3 changes: 3 additions & 0 deletions test/data/mixed-dirs/data-0.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name,occupation,age
jim,land surveyor,70
francis,conductor,63
3 changes: 3 additions & 0 deletions test/data/mixed-dirs/data-0.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name,occupation,age
jim,land surveyor,70
francis,conductor,63
3 changes: 3 additions & 0 deletions test/data/mixed-dirs/data-0.tsv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name occupation age
jim land surveyor 70
francis conductor 63
3 changes: 3 additions & 0 deletions test/data/mixed-dirs/data-1.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name,occupation,age
erika,land surveyor,50
quentin,conductor,23
12 changes: 12 additions & 0 deletions test/data/mixed-dirs/data-1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"name": "erika",
"occupation": "land surveyor",
"age": 50
},
{
"name": "quentin",
"occupation": "conductor",
"age": 23
}
]
43 changes: 42 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,6 @@ describe('readdirFilter()', 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)
Expand Down Expand Up @@ -405,6 +404,32 @@ describe('readdirFilter()', function () {
})
})
})

describe('get dirs only', function () {
it('should match expected output', function (done) {
var dir = path.join(__dirname, 'data', 'mixed-dirs')
io.readdirFilter(dir, {skipFiles: true}, function (err, files) {
assert(_.isEqual(JSON.stringify(files), '["sub-dir-0","sub-dir-1","sub-dir-3"]'))
if (err) {
console.log(err)
}
done()
})
})
})

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

describe('readdirFilterSync()', function () {
Expand Down Expand Up @@ -436,6 +461,22 @@ describe('readdirFilterSync()', function () {
assert.notEqual(files.indexOf(path.join(dir, 'this_is_not_a_csv.txt')), -1)
})
})

describe('get dirs only', function () {
it('should match expected output', function () {
var dir = path.join(__dirname, 'data', 'mixed-dirs')
var files = io.readdirFilterSync(dir, {skipFiles: true})
assert(_.isEqual(JSON.stringify(files), '["sub-dir-0","sub-dir-1","sub-dir-3"]'))
})
})

describe('get files only', function () {
it('should match expected output', function () {
var dir = path.join(__dirname, 'data', 'mixed-dirs')
var files = io.readdirFilterSync(dir, {exclude: /^\./, skipDirectories: true})
assert(_.isEqual(JSON.stringify(files), '["data-0.csv","data-0.json","data-0.tsv","data-1.csv","data-1.json"]'))
})
})
})

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

0 comments on commit 8630895

Please sign in to comment.