From fc660c27904a74d740c097ee8ab6da70da180993 Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Tue, 3 Jul 2012 14:44:41 -0700 Subject: [PATCH] Add fs.split --- spec/stdlib/fs-spec.coffee | 5 +++++ src/stdlib/fs.coffee | 9 +++++++++ 2 files changed, 14 insertions(+) diff --git a/spec/stdlib/fs-spec.coffee b/spec/stdlib/fs-spec.coffee index c7c17fcf1..a47509374 100644 --- a/spec/stdlib/fs-spec.coffee +++ b/spec/stdlib/fs-spec.coffee @@ -41,6 +41,11 @@ 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 ".split(path)", -> + it "returns path components", -> + expect(fs.split("/a/b/c.txt")).toEqual ["", "a", "b", "c.txt"] + expect(fs.split("a/b/c.txt")).toEqual ["a", "b", "c.txt"] + describe ".extension(path)", -> it "returns the extension of a file", -> expect(fs.extension("a/b/corey.txt")).toBe '.txt' diff --git a/src/stdlib/fs.coffee b/src/stdlib/fs.coffee index f39dca80b..2763e9b41 100644 --- a/src/stdlib/fs.coffee +++ b/src/stdlib/fs.coffee @@ -77,6 +77,15 @@ module.exports = read: (path) -> $native.read(path) + # Returns an array of path components. If the path is absolute, the first + # component will be an indicator of the root of the file system; for file + # systems with drives (such as Windows), this is the drive identifier with a + # colon, like "c:"; on Unix, this is an empty string "". The intent is that + # calling "join.apply" with the result of "split" as arguments will + # reconstruct the path. + split: (path) -> + path.split("/") + # Open, write, flush, and close a file, writing the given content. write: (path, content) -> $native.write(path, content)