Add fs.extension

This commit is contained in:
Corey Johnson
2012-05-01 14:07:05 -07:00
parent 7dbcb80339
commit cb0e576949
2 changed files with 19 additions and 0 deletions

View File

@@ -18,6 +18,14 @@ describe "fs", ->
expect(fs.join('/a/b/', 'c', 'd')).toBe '/a/b/c/d'
expect(fs.join('a', 'b/c/', 'd/')).toBe 'a/b/c/d/'
describe ".extension(path)", ->
it "returns the extension of a file", ->
expect(fs.extension("a/b/corey.txt")).toBe '.txt'
expect(fs.extension("a/b/corey.txt.coffee")).toBe '.coffee'
it "returns an empty string for paths without an extension", ->
expect(fs.extension("a/b.not-extension/a-dir")).toBe ''
describe ".async", ->
directoryPath = null
beforeEach ->

View File

@@ -27,6 +27,17 @@ module.exports =
exists: (path) ->
$native.exists path
# Returns the extension of a file. The extension of a file is the
# last dot (excluding any number of initial dots) followed by one or
# more non-dot characters. Returns an empty string if no valid
# extension exists.
extension: (path) ->
match = @base(path).match(/\.[^\.]+$/)
if match
match[0]
else
""
join: (paths...) ->
return paths[0] if paths.length == 1
[first, rest...] = paths