From 8e9a7355bcdfda02867da12d7ee79ea59291c64c Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Fri, 29 Mar 2013 13:36:17 -0400 Subject: [PATCH] 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. --- spec/stdlib/fs-utils-spec.coffee | 6 +++--- src/stdlib/fs-utils.coffee | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/spec/stdlib/fs-utils-spec.coffee b/spec/stdlib/fs-utils-spec.coffee index 1e214e1dc..5317be474 100644 --- a/spec/stdlib/fs-utils-spec.coffee +++ b/spec/stdlib/fs-utils-spec.coffee @@ -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']) diff --git a/src/stdlib/fs-utils.coffee b/src/stdlib/fs-utils.coffee index 1acf41169..a422f1e10 100644 --- a/src/stdlib/fs-utils.coffee +++ b/src/stdlib/fs-utils.coffee @@ -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)