Skip to content

Commit

Permalink
Update: Code cleanup and minor speed improvements in clone
Browse files Browse the repository at this point in the history
  • Loading branch information
yocontra authored and phated committed Sep 27, 2016
1 parent 7680c82 commit cb09ad0
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,14 @@ File.prototype.clone = function(opt) {
opt.contents = opt.contents !== false;
}

var file = new File();

Object.keys(this).forEach(function(key) {
// ignore these fields
if (key === '_contents' || key === 'stat' ||
key === 'history' || key === 'path') {
return;
}
file[key] = opt.deep === true ? clone(this[key], true) : this[key];
}, this);
var file = new File({
cwd: this.cwd,
base: this.base,
stat: (this.stat ? cloneStats(this.stat) : null)
});
file.history = this.history.slice();

// clone our file contents
if (this.isStream()) {
file.contents = this.contents.pipe(new Stream.PassThrough());
this.contents = this.contents.pipe(new Stream.PassThrough());
Expand All @@ -85,10 +82,16 @@ File.prototype.clone = function(opt) {
file.contents = this.contents;
}

// always clone these deep
file.stat = this.stat ? cloneStats(this.stat) : null;
file.history = this.history.slice();

// clone our custom properties
Object.keys(this).forEach(function(key) {
// ignore built-in fields
if (key === '_contents' || key === 'stat' ||
key === 'history' || key === 'path' ||
key === 'base' || key === 'cwd') {
return;
}
file[key] = opt.deep ? clone(this[key], true) : this[key];
}, this);
return file;
};

Expand Down

0 comments on commit cb09ad0

Please sign in to comment.