From dc07a973967e97a7b455fbd074e854611190471a Mon Sep 17 00:00:00 2001 From: contra Date: Mon, 1 Sep 2014 11:17:29 -0700 Subject: [PATCH] Fix: Allow history to be set in the constructor (closes #35) --- index.js | 23 +++++++++++++---------- test/File.js | 15 +++++++++++++++ 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index b37e253..09913c3 100644 --- a/index.js +++ b/index.js @@ -12,7 +12,8 @@ function File(file) { if (!file) file = {}; // record path change - this.history = file.path ? [file.path] : []; + var history = file.path ? [file.path] : file.history; + this.history = history || []; // TODO: should this be moved to vinyl-fs? this.cwd = file.cwd || process.cwd(); @@ -59,21 +60,23 @@ File.prototype.clone = function(opt) { opt.contents = opt.contents !== false; } - 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 + var contents; if (this.isStream()) { - file.contents = this.contents.pipe(new Stream.PassThrough()); + contents = this.contents.pipe(new Stream.PassThrough()); this.contents = this.contents.pipe(new Stream.PassThrough()); } else if (this.isBuffer()) { - file.contents = opt.contents ? cloneBuffer(this.contents) : this.contents; + contents = opt.contents ? cloneBuffer(this.contents) : this.contents; } + var file = new File({ + cwd: this.cwd, + base: this.base, + stat: (this.stat ? cloneStats(this.stat) : null), + history: this.history.slice(), + contents: contents + }); + // clone our custom properties Object.keys(this).forEach(function(key) { // ignore built-in fields diff --git a/test/File.js b/test/File.js index fd8832d..419e7aa 100644 --- a/test/File.js +++ b/test/File.js @@ -34,6 +34,12 @@ describe('File', function() { done(); }); + it('should default history to []', function(done) { + var file = new File(); + file.history.should.eql([]); + done(); + }); + it('should default stat to null', function(done) { var file = new File(); should.not.exist(file.stat); @@ -64,6 +70,15 @@ describe('File', function() { var val = '/test.coffee'; var file = new File({path: val}); file.path.should.equal(val); + file.history.should.eql([val]); + done(); + }); + + it('should set history to given value', function(done) { + var val = '/test.coffee'; + var file = new File({history: [val]}); + file.path.should.equal(val); + file.history.should.eql([val]); done(); });