Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(clone): option to not clone buffer, support custom properties #15

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
## Information

<table>
<tr>
<tr>
<td>Package</td><td>vinyl</td>
</tr>
<tr>
Expand Down Expand Up @@ -34,35 +34,35 @@ var coffeeFile = new File({

#### options.cwd

Type: `String`
Type: `String`
Default: `process.cwd()`

#### options.base

Used for relative pathing. Typically where a glob starts.

Type: `String`
Type: `String`
Default: `options.cwd`

#### options.path

Full path to the file.

Type: `String`
Type: `String`
Default: `null`

#### options.stat

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

Type: `fs.Stats`
Type: `fs.Stats`
Default: `null`

#### options.contents

File contents.

Type: `Buffer, Stream, or null`
Type: `Buffer, Stream, or null`
Default: `null`

### isBuffer()
Expand All @@ -77,10 +77,15 @@ Returns true if file.contents is a Stream.

Returns true if file.contents is null.

### clone()
### clone(options)

Returns a new File object with all attributes cloned.

#### options.contents

If file.contents is a Buffer, and options.contents is false, it will
reference-copy the buffer instead of cloning it.

### pipe(stream[, opt])

If file.contents is a Buffer, it will write it to the stream.
Expand Down Expand Up @@ -120,4 +125,4 @@ console.log(file.relative); // file.coffee
[coveralls-url]: https://coveralls.io/r/wearefractal/vinyl
[coveralls-image]: https://coveralls.io/repos/wearefractal/vinyl/badge.png
[depstat-url]: https://david-dm.org/wearefractal/vinyl
[depstat-image]: https://david-dm.org/wearefractal/vinyl.png
[depstat-image]: https://david-dm.org/wearefractal/vinyl.png
33 changes: 22 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var path = require('path');

var cloneStats = require('clone-stats');
var cloneDeep = require('lodash.cloneDeep');

var isBuffer = require('./lib/isBuffer');
var isStream = require('./lib/isStream');
Expand Down Expand Up @@ -42,17 +43,27 @@ File.prototype.isDirectory = function() {
return this.isNull() && this.stat && this.stat.isDirectory();
};

File.prototype.clone = function() {
var clonedContents = this.isBuffer() ? cloneBuffer(this.contents) : this.contents;
var clonedStat = this.stat ? cloneStats(this.stat) : null;

return new File({
cwd: this.cwd,
base: this.base,
path: this.path,
stat: clonedStat,
contents: clonedContents
});
File.prototype.clone = function(opt) {
if (!opt) opt = {};

var clone = new File();

Object.keys(this).forEach(function(key) {
if (key !== '_contents' && key !== 'stat' && this.hasOwnProperty(key)) {
clone[key] = cloneDeep(this[key]);
}
}, this);

if (opt.contents !== false && this.isBuffer()) {
clone.contents = cloneBuffer(this.contents);
}
else {
clone.contents = this.contents;
}

clone.stat = this.stat ? cloneStats(this.stat) : null;

return clone;
};

File.prototype.pipe = function(stream, opt) {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"author": "Fractal <contact@wearefractal.com> (http://wearefractal.com/)",
"main": "./index.js",
"dependencies": {
"clone-stats": "~0.0.1"
"clone-stats": "~0.0.1",
"lodash.clonedeep": "^2.4.1"
},
"devDependencies": {
"mocha": "~1.17.0",
Expand Down
48 changes: 45 additions & 3 deletions test/File.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ describe('File', function() {
done();
});
});

describe('isBuffer()', function() {
it('should return true when the contents are a Buffer', function(done) {
var val = new Buffer("test");
Expand Down Expand Up @@ -250,6 +250,48 @@ describe('File', function() {

done();
});

it('should copy custom properties', function(done) {
var options = {
cwd: "/",
base: "/test/",
path: "/test/test.coffee",
contents: null
};
var file = new File(options);
file.customProp = 'a custom property';

var file2 = file.clone();

file2.should.not.equal(file, 'refs should be different');
file2.cwd.should.equal(file.cwd);
file2.base.should.equal(file.base);
file2.path.should.equal(file.path);
file2.customProp.should.equal(file.customProp);

done();
});

it('should allow to reference-copy a Buffer', function(done) {
var options = {
cwd: "/",
base: "/test/",
path: "/test/test.coffee",
contents: new Buffer("test")
};
var file = new File(options);

var file2 = file.clone({ contents: false });

file2.should.not.equal(file, 'refs should be different');
file2.cwd.should.equal(file.cwd);
file2.base.should.equal(file.base);
file2.path.should.equal(file.path);
file2.contents.should.equal(file.contents, 'buffer ref should be the same');
file2.contents.toString('utf8').should.equal(file.contents.toString('utf8'));

done();
});
});

describe('pipe()', function() {
Expand Down Expand Up @@ -382,7 +424,7 @@ describe('File', function() {
process.nextTick(done);
});
});

describe('inspect()', function() {
it('should return correct format when no contents and no path', function(done) {
var file = new File();
Expand Down Expand Up @@ -445,7 +487,7 @@ describe('File', function() {
done();
});
});

describe('contents get/set', function() {
it('should work with Buffer', function(done) {
var val = new Buffer("test");
Expand Down