diff --git a/spec/stdlib/fs-spec.coffee b/spec/stdlib/fs-spec.coffee index 5b17662a9..ffcdaf31b 100644 --- a/spec/stdlib/fs-spec.coffee +++ b/spec/stdlib/fs-spec.coffee @@ -33,6 +33,13 @@ describe "fs", -> expect(fs.directory("a")).toBe "" expect(fs.directory("/a/b/c++")).toBe "/a/b" + describe ".base(path, ext)", -> + describe "when called with an extension", -> + it "return the base name without the extension when the path has the given extension", -> + expect(fs.base("/a/b/c.txt", '.txt')).toBe "c" + expect(fs.base("/a/b/c.txt", '.txt2')).toBe "c.txt" + expect(fs.base("/a/b/c.+", '.+')).toBe "c" + describe ".exists(path)", -> it "returns true when path exsits", -> expect(fs.exists(require.resolve('fixtures'))).toBe true diff --git a/src/stdlib/fs.coffee b/src/stdlib/fs.coffee index 054747b41..cb22ab5b1 100644 --- a/src/stdlib/fs.coffee +++ b/src/stdlib/fs.coffee @@ -15,7 +15,7 @@ module.exports = # remove a trailing extension. base: (path, ext) -> base = path.replace(/\/$/, '').split("/").pop() - if ext then base.replace(RegExp(ext + "$"), "") else base + if ext then base.replace(RegExp("#{_.escapeRegExp(ext)}$"), '') else base # Returns the path of a file's containing directory, albeit the # parent directory if the file is a directory. A terminal directory