From ee51a727219f8ce21881dfbdd63214a38cf49bca Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Tue, 1 May 2012 09:59:14 -0700 Subject: [PATCH] fs.directory matches commonjs spec --- spec/stdlib/fs-spec.coffee | 4 ++-- src/stdlib/fs.coffee | 11 ++++------- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/spec/stdlib/fs-spec.coffee b/spec/stdlib/fs-spec.coffee index 4d82f6848..4d56330d7 100644 --- a/spec/stdlib/fs-spec.coffee +++ b/spec/stdlib/fs-spec.coffee @@ -8,8 +8,8 @@ describe "fs", -> describe "when called with a directory path", -> it "return the path it was given", -> - expect(fs.directory(require.resolve('fixtures/dir'))).toBe require.resolve('fixtures/dir') - expect(fs.directory(require.resolve('fixtures/dir/'))).toBe require.resolve('fixtures/dir') + expect(fs.directory(require.resolve('fixtures/dir'))).toBe require.resolve('fixtures') + expect(fs.directory(require.resolve('fixtures/dir/'))).toBe require.resolve('fixtures') describe ".join(paths...)", -> it "concatenates the given paths with the directory seperator", -> diff --git a/src/stdlib/fs.coffee b/src/stdlib/fs.coffee index b595b6ec4..1e0a9be80 100644 --- a/src/stdlib/fs.coffee +++ b/src/stdlib/fs.coffee @@ -14,17 +14,14 @@ module.exports = # any leading directory components removed. If specified, also # remove a trailing extension. base: (path, ext) -> - path = base = path.replace(/\/$/, '').split("/").pop() if ext then base.replace(RegEx(ext + "$"), "") else base - # Return the dirname of the given path. That is the path with any trailing - # non-directory component removed. + # Returns the path of a file's containing directory, albeit the + # parent directory if the file is a directory. A terminal directory + # separator is ignored. directory: (path) -> - if @isDirectory(path) - path.replace(/\/?$/, '') - else - path.replace(new RegExp("/#{@base(path)}$"), '') + path.replace(new RegExp("/#{@base(path)}\/?$"), '') # Returns true if the file specified by path exists exists: (path) ->