Skip to content

Commit

Permalink
Merge 806854b into 7196640
Browse files Browse the repository at this point in the history
  • Loading branch information
laurelnaiad committed Apr 19, 2014
2 parents 7196640 + 806854b commit e8f6a55
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 6 deletions.
21 changes: 18 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,32 @@ 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;
File.prototype.clone = function(opt) {
if (!opt) opt = {};

var clonedContents = this.isBuffer() && opt.contents !== false ?
cloneBuffer(this.contents) :
this.contents;

var clonedStat = this.stat ? cloneStats(this.stat) : null;

return new File({
var newFile = new File({
cwd: this.cwd,
base: this.base,
path: this.path,
stat: clonedStat,
contents: clonedContents
});

// pick up custom properties, if any
Object.keys(this).forEach(function(key) {
// avoid private props and prototype props
if (this.hasOwnProperty(key) && newFile[key] === undefined) {
newFile[key] = this[key];
}
}, this);

return newFile;
};

File.prototype.pipe = function(stream, opt) {
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

0 comments on commit e8f6a55

Please sign in to comment.