From 2f454702fa4978bee87c99e7650a1daaf5439e63 Mon Sep 17 00:00:00 2001 From: popomore Date: Thu, 28 Aug 2014 20:47:13 +0800 Subject: [PATCH] option to not clone buffer #16 --- index.js | 9 +++++---- test/File.js | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 6faa797..177420d 100644 --- a/index.js +++ b/index.js @@ -46,11 +46,12 @@ File.prototype.isDirectory = function() { File.prototype.clone = function(opt) { if (typeof opt === 'boolean') { - opt = { deep: opt }; + opt = { deep: opt, contents: true }; } else if (!opt) { - opt = { deep: false }; + opt = { deep: false, contents: true }; } else { - opt.deep = opt.deep || false; + opt.deep = opt.deep === true; + opt.contents = opt.contents !== false; } var clone = new File(); @@ -61,7 +62,7 @@ File.prototype.clone = function(opt) { } }, this); - clone.contents = this.isBuffer() ? cloneBuffer(this.contents) : this.contents; + clone.contents = opt.contents && this.isBuffer() ? cloneBuffer(this.contents) : this.contents; clone.stat = this.stat ? cloneStats(this.stat) : null; return clone; diff --git a/test/File.js b/test/File.js index 58ab1d1..a27b2ef 100644 --- a/test/File.js +++ b/test/File.js @@ -196,6 +196,28 @@ describe('File', function() { done(); }); + it('should copy buffer\'s reference with option contents: false', function(done) { + var options = { + cwd: '/', + base: '/test/', + path: '/test/test.js', + contents: new Buffer('test') + }; + + var file = new File(options); + + var copy1 = file.clone({ contents: false }); + copy1.contents.should.equal(file.contents); + + var copy2 = file.clone({}); + copy2.contents.should.not.equal(file.contents); + + var copy3 = file.clone({ contents: 'any string' }); + copy3.contents.should.not.equal(file.contents); + + done(); + }); + it('should copy all attributes over with Stream', function(done) { var options = { cwd: '/',