Skip to content

Commit 57b770c

Browse files
jeremyruppelphated
authored andcommitted
New: Add dirname, basename and extname accessors
1 parent 9e2dcfe commit 57b770c

File tree

4 files changed

+240
-10
lines changed

4 files changed

+240
-10
lines changed

README.md

Lines changed: 72 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
## Information
55

66
<table>
7-
<tr>
7+
<tr>
88
<td>Package</td><td>vinyl</td>
99
</tr>
1010
<tr>
@@ -38,42 +38,42 @@ var coffeeFile = new File({
3838

3939
#### options.cwd
4040

41-
Type: `String`
41+
Type: `String`
4242
Default: `process.cwd()`
4343

4444
#### options.base
4545

4646
Used for relative pathing. Typically where a glob starts.
4747

48-
Type: `String`
48+
Type: `String`
4949
Default: `options.cwd`
5050

5151
#### options.path
5252

5353
Full path to the file.
5454

55-
Type: `String`
55+
Type: `String`
5656
Default: `undefined`
5757

5858
#### options.history
5959

6060
Path history. Has no effect if `options.path` is passed.
6161

62-
Type: `Array`
62+
Type: `Array`
6363
Default: `options.path ? [options.path] : []`
6464

6565
#### options.stat
6666

6767
The result of an fs.stat call. See [fs.Stats](http://nodejs.org/api/fs.html#fs_class_fs_stats) for more information.
6868

69-
Type: `fs.Stats`
69+
Type: `fs.Stats`
7070
Default: `null`
7171

7272
#### options.contents
7373

7474
File contents.
7575

76-
Type: `Buffer, Stream, or null`
76+
Type: `Buffer, Stream, or null`
7777
Default: `null`
7878

7979
### isBuffer()
@@ -117,6 +117,10 @@ Returns a pretty String interpretation of the File. Useful for console.log.
117117

118118
Absolute pathname string or `undefined`. Setting to a different value pushes the old value to `history`.
119119

120+
### history
121+
122+
Array of `path` values the file object has had, from `history[0]` (original) through `history[history.length - 1]` (current). `history` and its elements should normally be treated as read-only and only altered indirectly by setting `path`.
123+
120124
### relative
121125

122126
Returns path.relative for the file base and file path.
@@ -133,9 +137,68 @@ var file = new File({
133137
console.log(file.relative); // file.coffee
134138
```
135139

136-
### history
140+
### dirname
137141

138-
Array of `path` values the file object has had, from `history[0]` (original) through `history[history.length - 1]` (current). `history` and its elements should normally be treated as read-only and only altered indirectly by setting `path`.
142+
Gets and sets path.dirname for the file path.
143+
144+
Example:
145+
146+
```javascript
147+
var file = new File({
148+
cwd: "/",
149+
base: "/test/",
150+
path: "/test/file.coffee"
151+
});
152+
153+
console.log(file.dirname); // /test
154+
155+
file.dirname = '/specs';
156+
157+
console.log(file.dirname); // /specs
158+
console.log(file.path); // /specs/file.coffee
159+
````
160+
161+
### basename
162+
163+
Gets and sets path.basename for the file path.
164+
165+
Example:
166+
167+
```javascript
168+
var file = new File({
169+
cwd: "/",
170+
base: "/test/",
171+
path: "/test/file.coffee"
172+
});
173+
174+
console.log(file.basename); // file.coffee
175+
176+
file.basename = 'file.js';
177+
178+
console.log(file.basename); // file.js
179+
console.log(file.path); // /test/file.js
180+
````
181+
182+
### extname
183+
184+
Gets and sets path.extname for the file path.
185+
186+
Example:
187+
188+
```javascript
189+
var file = new File({
190+
cwd: "/",
191+
base: "/test/",
192+
path: "/test/file.coffee"
193+
});
194+
195+
console.log(file.extname); // .coffee
196+
197+
file.extname = '.js';
198+
199+
console.log(file.extname); // .js
200+
console.log(file.path); // /test/file.js
201+
````
139202

140203
[npm-url]: https://npmjs.org/package/vinyl
141204
[npm-image]: https://badge.fury.io/js/vinyl.png

index.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var isStream = require('./lib/isStream');
77
var isNull = require('./lib/isNull');
88
var inspectStream = require('./lib/inspectStream');
99
var Stream = require('stream');
10+
var replaceExt = require('replace-ext');
1011

1112
function File(file) {
1213
if (!file) file = {};
@@ -158,6 +159,39 @@ Object.defineProperty(File.prototype, 'relative', {
158159
}
159160
});
160161

162+
Object.defineProperty(File.prototype, 'dirname', {
163+
get: function() {
164+
if (!this.path) throw new Error('No path specified! Can not get dirname.');
165+
return path.dirname(this.path);
166+
},
167+
set: function(dirname) {
168+
if (!this.path) throw new Error('No path specified! Can not set dirname.');
169+
this.path = path.join(dirname, path.basename(this.path));
170+
}
171+
});
172+
173+
Object.defineProperty(File.prototype, 'basename', {
174+
get: function() {
175+
if (!this.path) throw new Error('No path specified! Can not get basename.');
176+
return path.basename(this.path);
177+
},
178+
set: function(basename) {
179+
if (!this.path) throw new Error('No path specified! Can not set basename.');
180+
this.path = path.join(path.dirname(this.path), basename);
181+
}
182+
});
183+
184+
Object.defineProperty(File.prototype, 'extname', {
185+
get: function() {
186+
if (!this.path) throw new Error('No path specified! Can not get extname.');
187+
return path.extname(this.path);
188+
},
189+
set: function(extname) {
190+
if (!this.path) throw new Error('No path specified! Can not set extname.');
191+
this.path = replaceExt(this.path, extname);
192+
}
193+
});
194+
161195
Object.defineProperty(File.prototype, 'path', {
162196
get: function() {
163197
return this.history[this.history.length - 1];

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
],
1313
"dependencies": {
1414
"clone": "^1.0.0",
15-
"clone-stats": "^0.0.1"
15+
"clone-stats": "^0.0.1",
16+
"replace-ext": "0.0.1"
1617
},
1718
"devDependencies": {
1819
"buffer-equal": "0.0.1",

test/File.js

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,138 @@ describe('File', function() {
692692
});
693693
});
694694

695+
describe('dirname get/set', function() {
696+
it('should error on get when no path', function(done) {
697+
var a;
698+
var file = new File();
699+
try {
700+
a = file.dirname;
701+
} catch (err) {
702+
should.exist(err);
703+
done();
704+
}
705+
});
706+
707+
it('should return the dirname of the path', function(done) {
708+
var file = new File({
709+
cwd: '/',
710+
base: '/test/',
711+
path: '/test/test.coffee'
712+
});
713+
file.dirname.should.equal('/test');
714+
done();
715+
});
716+
717+
it('should error on set when no path', function(done) {
718+
var file = new File();
719+
try {
720+
file.dirname = '/test';
721+
} catch (err) {
722+
should.exist(err);
723+
done();
724+
}
725+
});
726+
727+
it('should set the dirname of the path', function(done) {
728+
var file = new File({
729+
cwd: '/',
730+
base: '/test/',
731+
path: '/test/test.coffee'
732+
});
733+
file.dirname = '/test/foo';
734+
file.path.should.equal('/test/foo/test.coffee');
735+
done();
736+
});
737+
});
738+
739+
describe('basename get/set', function() {
740+
it('should error on get when no path', function(done) {
741+
var a;
742+
var file = new File();
743+
try {
744+
a = file.basename;
745+
} catch (err) {
746+
should.exist(err);
747+
done();
748+
}
749+
});
750+
751+
it('should return the basename of the path', function(done) {
752+
var file = new File({
753+
cwd: '/',
754+
base: '/test/',
755+
path: '/test/test.coffee'
756+
});
757+
file.basename.should.equal('test.coffee');
758+
done();
759+
});
760+
761+
it('should error on set when no path', function(done) {
762+
var file = new File();
763+
try {
764+
file.basename = 'test.coffee';
765+
} catch (err) {
766+
should.exist(err);
767+
done();
768+
}
769+
});
770+
771+
it('should set the basename of the path', function(done) {
772+
var file = new File({
773+
cwd: '/',
774+
base: '/test/',
775+
path: '/test/test.coffee'
776+
});
777+
file.basename = 'foo.png';
778+
file.path.should.equal('/test/foo.png');
779+
done();
780+
});
781+
});
782+
783+
describe('extname get/set', function() {
784+
it('should error on get when no path', function(done) {
785+
var a;
786+
var file = new File();
787+
try {
788+
a = file.extname;
789+
} catch (err) {
790+
should.exist(err);
791+
done();
792+
}
793+
});
794+
795+
it('should return the extname of the path', function(done) {
796+
var file = new File({
797+
cwd: '/',
798+
base: '/test/',
799+
path: '/test/test.coffee'
800+
});
801+
file.extname.should.equal('.coffee');
802+
done();
803+
});
804+
805+
it('should error on set when no path', function(done) {
806+
var file = new File();
807+
try {
808+
file.extname = '.coffee';
809+
} catch (err) {
810+
should.exist(err);
811+
done();
812+
}
813+
});
814+
815+
it('should set the extname of the path', function(done) {
816+
var file = new File({
817+
cwd: '/',
818+
base: '/test/',
819+
path: '/test/test.coffee'
820+
});
821+
file.extname = '.png';
822+
file.path.should.equal('/test/test.png');
823+
done();
824+
});
825+
});
826+
695827
describe('path get/set', function() {
696828

697829
it('should record history when instantiation', function() {

0 commit comments

Comments
 (0)