Skip to content

Commit

Permalink
copy all attributes deeply #29
Browse files Browse the repository at this point in the history
  • Loading branch information
popomore authored and phated committed Sep 27, 2016
1 parent 63e3c45 commit 548e618
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
15 changes: 11 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
var path = require('path');

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

var isBuffer = require('./lib/isBuffer');
var isStream = require('./lib/isStream');
Expand Down Expand Up @@ -45,12 +44,20 @@ File.prototype.isDirectory = function() {
return this.isNull() && this.stat && this.stat.isDirectory();
};

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

var clone = new File();

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

Expand Down
36 changes: 35 additions & 1 deletion test/File.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ describe('File', function() {
file2.cwd.should.equal(file.cwd);
file2.base.should.equal(file.base);
file2.path.should.equal(file.path);
file2.custom.should.not.equal(file.custom);
file2.custom.should.equal(file.custom);
file2.custom.a.should.equal(file.custom.a);

done();
Expand Down Expand Up @@ -302,6 +302,40 @@ describe('File', function() {

done();
});

it('should copy all attributes deeply', function(done) {
var options = {
cwd: '/',
base: '/test/',
path: '/test/test.coffee',
contents: null
};

var file = new File(options);
file.custom = { a: 'custom property' };

var file2 = file.clone(true);
file2.custom.should.eql(file.custom);
file2.custom.should.not.equal(file.custom);
file2.custom.a.should.equal(file.custom.a);

var file3 = file.clone({ deep: true });
file3.custom.should.eql(file.custom);
file3.custom.should.not.equal(file.custom);
file3.custom.a.should.equal(file.custom.a);

var file4 = file.clone(false);
file4.custom.should.eql(file.custom);
file4.custom.should.equal(file.custom);
file4.custom.a.should.equal(file.custom.a);

var file5 = file.clone({ deep: false });
file5.custom.should.eql(file.custom);
file5.custom.should.equal(file.custom);
file5.custom.a.should.equal(file.custom.a);

done();
});
});

describe('pipe()', function() {
Expand Down

0 comments on commit 548e618

Please sign in to comment.