Return an empty array when the fs-utils.list() path isn't a directory

This makes the common case of iterating over an array of paths and
listing them cleaner since the return value doesn't need to be checked
before it is iterated over.
This commit is contained in:
Kevin Sawicki
2013-03-29 13:36:17 -04:00
parent 0a6963a965
commit 8e9a7355bc
2 changed files with 4 additions and 4 deletions

View File

@@ -130,9 +130,9 @@ describe "fs", ->
expect(paths).toContain project.resolve('coffee.coffee')
expect(paths).toContain project.resolve('two-hundred.txt')
it "returns undefined for paths that aren't directories or don't exist", ->
expect(fs.list(project.resolve('sample.js'))).toBeUndefined()
expect(fs.list('/non/existent/directory')).toBeUndefined()
it "returns an empty array for paths that aren't directories or don't exist", ->
expect(fs.list(project.resolve('sample.js'))).toEqual []
expect(fs.list('/non/existent/directory')).toEqual []
it "can filter the paths by an optional array of file extensions", ->
paths = fs.list(project.getPath(), ['.css', 'coffee'])

View File

@@ -90,7 +90,7 @@ module.exports =
# Returns an array with all the names of files contained
# in the directory path.
list: (rootPath, extensions) ->
return unless @isDirectory(rootPath)
return [] unless @isDirectory(rootPath)
paths = fs.readdirSync(rootPath)
paths = @filterExtensions(paths, extensions) if extensions
paths = paths.map (path) => @join(rootPath, path)