diff --git a/.gitignore b/.gitignore index b5ef13a..151dc55 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ node_modules build *.node -components \ No newline at end of file +components +coverage diff --git a/index.js b/index.js index 177420d..87e228b 100644 --- a/index.js +++ b/index.js @@ -1,13 +1,17 @@ var path = require('path'); +var clone; +try { + clone = require('node-v8-clone').clone; +} catch(e) { + clone = require('lodash').clone; +} var cloneStats = require('clone-stats'); -var cloneDeep = require('lodash').cloneDeep; - +var cloneBuffer = require('./lib/cloneBuffer'); var isBuffer = require('./lib/isBuffer'); var isStream = require('./lib/isStream'); var isNull = require('./lib/isNull'); var inspectStream = require('./lib/inspectStream'); -var cloneBuffer = require('./lib/cloneBuffer'); function File(file) { if (!file) file = {}; @@ -54,18 +58,18 @@ File.prototype.clone = function(opt) { opt.contents = opt.contents !== false; } - var clone = new File(); + var file = new File(); Object.keys(this).forEach(function(key) { if (key !== '_contents' && key !== 'stat') { - clone[key] = opt.deep === true ? cloneDeep(this[key]) : this[key]; + file[key] = opt.deep === true ? clone(this[key], true) : this[key]; } }, this); - clone.contents = opt.contents && this.isBuffer() ? cloneBuffer(this.contents) : this.contents; - clone.stat = this.stat ? cloneStats(this.stat) : null; + file.contents = opt.contents && this.isBuffer() ? cloneBuffer(this.contents) : this.contents; + file.stat = this.stat ? cloneStats(this.stat) : null; - return clone; + return file; }; File.prototype.pipe = function(stream, opt) { diff --git a/package.json b/package.json index f5decaa..7eec7db 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,9 @@ "lodash.templatesettings": "^2.4.1", "event-stream": "^3.1.0" }, + "optionalDependencies": { + "node-v8-clone": "~0.6.2" + }, "scripts": { "test": "mocha --reporter spec && jshint . --exclude node_modules", "coveralls": "istanbul cover _mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | coveralls && rm -rf ./coverage" diff --git a/test/File.js b/test/File.js index a27b2ef..0b4dda1 100644 --- a/test/File.js +++ b/test/File.js @@ -1,4 +1,3 @@ -var File = require('../'); var Stream = require('stream'); var fs = require('fs'); var path = require('path'); @@ -8,6 +7,26 @@ require('mocha'); describe('File', function() { + describe('using node-v8-clone', function() { + var File = require('../'); + testFile(File); + }); + + describe('using lodash', function() { + delete require.cache[path.join(__dirname, '../index.js')]; + var clonePath = path.join(__dirname, '../node_modules/node-v8-clone/lib/clone.js'); + var exports = require.cache[clonePath].exports; + // test lodash when node-v8-clone is not found + require.cache[clonePath].exports = undefined; + after(function() { + require.cache[clonePath].exports = exports; + }); + var File = require('../'); + testFile(File); + }); +}); + +function testFile(File) { describe('constructor()', function() { it('should default cwd to process.cwd', function(done) { var file = new File(); @@ -703,5 +722,4 @@ describe('File', function() { }).should.throw('path should be string'); }); }); - -}); +}