Skip to content

Commit

Permalink
New: Add stem accessor
Browse files Browse the repository at this point in the history
  • Loading branch information
soslan authored and phated committed Sep 27, 2016
1 parent 6c0f046 commit 94dbdeb
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
16 changes: 16 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
44 changes: 44 additions & 0 deletions test/File.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 94dbdeb

Please sign in to comment.