Skip to content

Commit

Permalink
option to not clone buffer #16
Browse files Browse the repository at this point in the history
  • Loading branch information
popomore authored and phated committed Sep 27, 2016
1 parent 1da7b3c commit 2f45470
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
9 changes: 5 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ File.prototype.isDirectory = function() {

File.prototype.clone = function(opt) {
if (typeof opt === 'boolean') {
opt = { deep: opt };
opt = { deep: opt, contents: true };
} else if (!opt) {
opt = { deep: false };
opt = { deep: false, contents: true };
} else {
opt.deep = opt.deep || false;
opt.deep = opt.deep === true;
opt.contents = opt.contents !== false;
}

var clone = new File();
Expand All @@ -61,7 +62,7 @@ File.prototype.clone = function(opt) {
}
}, this);

clone.contents = this.isBuffer() ? cloneBuffer(this.contents) : this.contents;
clone.contents = opt.contents && this.isBuffer() ? cloneBuffer(this.contents) : this.contents;
clone.stat = this.stat ? cloneStats(this.stat) : null;

return clone;
Expand Down
22 changes: 22 additions & 0 deletions test/File.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,28 @@ describe('File', function() {
done();
});

it('should copy buffer\'s reference with option contents: false', function(done) {
var options = {
cwd: '/',
base: '/test/',
path: '/test/test.js',
contents: new Buffer('test')
};

var file = new File(options);

var copy1 = file.clone({ contents: false });
copy1.contents.should.equal(file.contents);

var copy2 = file.clone({});
copy2.contents.should.not.equal(file.contents);

var copy3 = file.clone({ contents: 'any string' });
copy3.contents.should.not.equal(file.contents);

done();
});

it('should copy all attributes over with Stream', function(done) {
var options = {
cwd: '/',
Expand Down

0 comments on commit 2f45470

Please sign in to comment.