Skip to content

Commit

Permalink
Merge pull request #27 from vweevers/master
Browse files Browse the repository at this point in the history
Clone custom properties
  • Loading branch information
yocontra committed Jul 19, 2014
2 parents 83bd747 + 95710de commit f631c76
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ Returns true if file.contents is null.

### clone()

Returns a new File object with all attributes cloned.
Returns a new File object with all attributes cloned. Custom attributes are deep-cloned.

### pipe(stream[, opt])

Expand Down
23 changes: 13 additions & 10 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 @@ -43,16 +44,18 @@ File.prototype.isDirectory = function() {
};

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
});
var clone = new File();

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

clone.contents = this.isBuffer() ? cloneBuffer(this.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
26 changes: 25 additions & 1 deletion test/File.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var File = require('../');
var Stream = require('stream');
var fs = require('fs');
var path = require('path');

var should = require('should');
require('mocha');
Expand Down Expand Up @@ -250,6 +251,29 @@ 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.custom = { 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.custom.should.not.equal(file.custom);
file2.custom.a.should.equal(file.custom.a);

done();
});
});

describe('pipe()', function() {
Expand Down Expand Up @@ -532,7 +556,7 @@ describe('File', function() {
cwd: "/",
path: "/test/test.coffee"
});
file.relative.should.equal("test/test.coffee");
file.relative.should.equal(path.join("test","test.coffee"));
done();
});
});
Expand Down

0 comments on commit f631c76

Please sign in to comment.