Skip to content

Commit

Permalink
Merge pull request #32 from popomore/clone
Browse files Browse the repository at this point in the history
improve clone
  • Loading branch information
yocontra committed Aug 29, 2014
2 parents c801d3d + 037e830 commit 76165e0
Show file tree
Hide file tree
Showing 13 changed files with 274 additions and 115 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
node_modules
build
*.node
components
components
coverage
2 changes: 2 additions & 0 deletions .jshintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
coverage
36 changes: 36 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"indent": 2,
"smarttabs": true,
"latedef": "nofunc",
"undef": true,
"unused": true,
"trailing": true,
"laxcomma": false,
"eqeqeq": true,
"eqnull": true,
"boss": true,
"expr": true,
"sub": true,
"newcap": true,
"quotmark": true,
"loopfunc": true,
"lastsemic": true,
"funcscope": true,
"browser": true,
"nonstandard": true,
"jquery": true,
"devel": true,
"node": true,
"esnext": true,
"globals": {
"describe": false,
"xdescribe": false,
"it": false,
"xit": false,
"before": false,
"beforeEach": false,
"after": false,
"afterEach": false,
"define": false
}
}
40 changes: 29 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,14 +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 _ = require('lodash');
var cloneDeep = _.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 = {};
Expand Down Expand Up @@ -45,19 +48,34 @@ File.prototype.isDirectory = function() {
return this.isNull() && this.stat && this.stat.isDirectory();
};

File.prototype.clone = function() {
var clone = new File();
File.prototype.clone = function(opt) {
if (typeof opt === 'boolean') {
opt = {
deep: opt,
contents: true
};
} else if (!opt) {
opt = {
deep: false,
contents: true
};
} else {
opt.deep = opt.deep === true;
opt.contents = opt.contents !== false;
}

var file = new File();

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

clone.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) {
Expand Down Expand Up @@ -110,7 +128,7 @@ Object.defineProperty(File.prototype, 'contents', {
},
set: function(val) {
if (!isBuffer(val) && !isStream(val) && !isNull(val)) {
throw new Error("File.contents can only be a Buffer, a Stream, or null.");
throw new Error('File.contents can only be a Buffer, a Stream, or null.');
}
this._contents = val;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/cloneBuffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ module.exports = function(buf) {
var out = new Buffer(buf.length);
buf.copy(out);
return out;
};
};
5 changes: 2 additions & 3 deletions lib/inspectStream.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
var Stream = require('stream').Stream;
var isStream = require('./isStream');

module.exports = function(stream) {
if (!isStream(stream)) return;

var streamType = stream.constructor.name;
// avoid StreamStream
if (streamType === 'Stream') streamType = '';

return '<'+streamType+'Stream>';
};
};
2 changes: 1 addition & 1 deletion lib/isNull.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = function(v) {
return v === null;
};
};
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@
"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",
"test": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter spec && jshint .",
"coveralls": "istanbul cover _mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | coveralls && rm -rf ./coverage"
},
"engines": {
Expand Down
Loading

0 comments on commit 76165e0

Please sign in to comment.