diff --git a/README.md b/README.md index 481b768..f42b58f 100644 --- a/README.md +++ b/README.md @@ -165,6 +165,27 @@ console.log(file.path); // /test/file.js ` ``` +### stem +Gets and sets stem (filename without suffix) for the file path. + +Example: + +```javascript +var file = new File({ + cwd: "/", + base: "/test/", + path: "/test/file.coffee" +}); + +console.log(file.stem); // file + +file.stem = 'foo'; + +console.log(file.stem); // foo +console.log(file.path); // /test/foo.coffee +` +``` + ### extname Gets and sets path.extname for the file path. diff --git a/index.js b/index.js index cd733cc..f8b10e7 100644 --- a/index.js +++ b/index.js @@ -205,6 +205,22 @@ Object.defineProperty(File.prototype, 'basename', { }, }); +// Property for getting/setting stem of the filename. +Object.defineProperty(File.prototype, 'stem', { + get: function() { + if (!this.path) { + throw new Error('No path specified! Can not get stem.'); + } + return path.basename(this.path, this.extname); + }, + set: function(stem) { + if (!this.path) { + throw new Error('No PassThrough specified! Can not set stem.'); + } + this.path = path.join(path.dirname(this.path), stem + this.extname); + }, +}); + Object.defineProperty(File.prototype, 'extname', { get: function() { if (!this.path) { diff --git a/test/File.js b/test/File.js index c9b04cb..04a790d 100644 --- a/test/File.js +++ b/test/File.js @@ -795,6 +795,50 @@ describe('File', function() { }); }); + describe('stem get/set', function() { + it('should error on get when no path', function(done) { + var a; + var file = new File(); + try { + a = file.stem; + } catch (err) { + should.exist(err); + done(); + } + }); + + it('should return the stem of the path', function(done) { + var file = new File({ + cwd: '/', + base: '/test/', + path: '/test/test.coffee', + }); + file.stem.should.equal('test'); + done(); + }); + + it('should error on set when no path', function(done) { + var file = new File(); + try { + file.stem = 'test.coffee'; + } catch (err) { + should.exist(err); + done(); + } + }); + + it('should set the stem of the path', function(done) { + var file = new File({ + cwd: '/', + base: '/test/', + path: '/test/test.coffee', + }); + file.stem = 'foo'; + file.path.should.equal('/test/foo.coffee'); + done(); + }); + }); + describe('extname get/set', function() { it('should error on get when no path', function(done) { var a;