Comparing version 0.3.3 to 0.4.0
57
index.js
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'); | ||
@@ -11,3 +15,3 @@ var isStream = require('./lib/isStream'); | ||
var inspectStream = require('./lib/inspectStream'); | ||
var cloneBuffer = require('./lib/cloneBuffer'); | ||
var Stream = require('stream'); | ||
@@ -49,15 +53,38 @@ function File(file) { | ||
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; | ||
if (this.isStream()) { | ||
file.contents = this.contents.pipe(new Stream.PassThrough()); | ||
this.contents = this.contents.pipe(new Stream.PassThrough()); | ||
} else if (opt.contents && this.isBuffer()) { | ||
file.contents = cloneBuffer(this.contents); | ||
} else { | ||
file.contents = this.contents; | ||
} | ||
return clone; | ||
file.stat = this.stat ? cloneStats(this.stat) : null; | ||
return file; | ||
}; | ||
@@ -80,6 +107,6 @@ | ||
} | ||
if (this.isNull()) { | ||
if (opt.end) stream.end(); | ||
return stream; | ||
} | ||
// isNull | ||
if (opt.end) stream.end(); | ||
return stream; | ||
}; | ||
@@ -116,3 +143,3 @@ | ||
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.'); | ||
} | ||
@@ -119,0 +146,0 @@ this._contents = val; |
@@ -7,2 +7,2 @@ var Buffer = require('buffer').Buffer; | ||
return out; | ||
}; | ||
}; |
@@ -1,2 +0,1 @@ | ||
var Stream = require('stream').Stream; | ||
var isStream = require('./isStream'); | ||
@@ -6,3 +5,3 @@ | ||
if (!isStream(stream)) return; | ||
var streamType = stream.constructor.name; | ||
@@ -13,2 +12,2 @@ // avoid StreamStream | ||
return '<'+streamType+'Stream>'; | ||
}; | ||
}; |
module.exports = function(v) { | ||
return v === null; | ||
}; | ||
}; |
{ | ||
"name": "vinyl", | ||
"description": "A virtual file format", | ||
"version": "0.3.3", | ||
"version": "0.4.0", | ||
"homepage": "http://github.com/wearefractal/vinyl", | ||
@@ -25,4 +25,7 @@ "repository": "git://github.com/wearefractal/vinyl.git", | ||
}, | ||
"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" | ||
@@ -29,0 +32,0 @@ }, |
var cloneBuffer = require('../lib/cloneBuffer'); | ||
var Stream = require('stream'); | ||
var should = require('should'); | ||
@@ -28,2 +27,2 @@ require('mocha'); | ||
}); | ||
}); | ||
}); |
295
test/File.js
@@ -1,5 +0,5 @@ | ||
var File = require('../'); | ||
var Stream = require('stream'); | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var es = require('event-stream'); | ||
@@ -11,2 +11,22 @@ var should = require('should'); | ||
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() { | ||
@@ -20,3 +40,3 @@ it('should default cwd to process.cwd', function(done) { | ||
it('should default base to cwd', function(done) { | ||
var cwd = "/"; | ||
var cwd = '/'; | ||
var file = new File({cwd: cwd}); | ||
@@ -52,3 +72,3 @@ file.base.should.equal(cwd); | ||
it('should set base to given value', function(done) { | ||
var val = "/"; | ||
var val = '/'; | ||
var file = new File({base: val}); | ||
@@ -60,3 +80,3 @@ file.base.should.equal(val); | ||
it('should set cwd to given value', function(done) { | ||
var val = "/"; | ||
var val = '/'; | ||
var file = new File({cwd: val}); | ||
@@ -68,3 +88,3 @@ file.cwd.should.equal(val); | ||
it('should set path to given value', function(done) { | ||
var val = "/test.coffee"; | ||
var val = '/test.coffee'; | ||
var file = new File({path: val}); | ||
@@ -83,3 +103,3 @@ file.path.should.equal(val); | ||
it('should set contents to given value', function(done) { | ||
var val = new Buffer("test"); | ||
var val = new Buffer('test'); | ||
var file = new File({contents: val}); | ||
@@ -93,3 +113,3 @@ file.contents.should.equal(val); | ||
it('should return true when the contents are a Buffer', function(done) { | ||
var val = new Buffer("test"); | ||
var val = new Buffer('test'); | ||
var file = new File({contents: val}); | ||
@@ -116,3 +136,3 @@ file.isBuffer().should.equal(true); | ||
it('should return false when the contents are a Buffer', function(done) { | ||
var val = new Buffer("test"); | ||
var val = new Buffer('test'); | ||
var file = new File({contents: val}); | ||
@@ -139,3 +159,3 @@ file.isStream().should.equal(false); | ||
it('should return false when the contents are a Buffer', function(done) { | ||
var val = new Buffer("test"); | ||
var val = new Buffer('test'); | ||
var file = new File({contents: val}); | ||
@@ -168,3 +188,3 @@ file.isNull().should.equal(false); | ||
it('should return false when the contents are a Buffer', function(done) { | ||
var val = new Buffer("test"); | ||
var val = new Buffer('test'); | ||
var file = new File({contents: val, stat: fakeStat}); | ||
@@ -192,6 +212,6 @@ file.isDirectory().should.equal(false); | ||
var options = { | ||
cwd: "/", | ||
base: "/test/", | ||
path: "/test/test.coffee", | ||
contents: new Buffer("test") | ||
cwd: '/', | ||
base: '/test/', | ||
path: '/test/test.coffee', | ||
contents: new Buffer('test') | ||
}; | ||
@@ -210,8 +230,31 @@ var file = new File(options); | ||
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 contents = new Stream.PassThrough(); | ||
var options = { | ||
cwd: "/", | ||
base: "/test/", | ||
path: "/test/test.coffee", | ||
contents: new Stream() | ||
cwd: '/', | ||
base: '/test/', | ||
path: '/test/test.coffee', | ||
contents: contents | ||
}; | ||
@@ -221,2 +264,9 @@ var file = new File(options); | ||
contents.write(new Buffer('wa')); | ||
process.nextTick(function() { | ||
contents.write(new Buffer('dup')); | ||
contents.end(); | ||
}); | ||
file2.should.not.equal(file, 'refs should be different'); | ||
@@ -226,3 +276,9 @@ file2.cwd.should.equal(file.cwd); | ||
file2.path.should.equal(file.path); | ||
file2.contents.should.equal(file.contents, 'stream ref should be the same'); | ||
file2.contents.should.not.equal(file.contents, 'stream ref should not be the same'); | ||
file.contents.pipe(es.wait(function(err, data) { | ||
file2.contents.pipe(es.wait(function(err, data2) { | ||
data2.should.not.equal(data, 'stream contents ref should not be the same'); | ||
data2.should.eql(data, 'stream contents should be the same'); | ||
})); | ||
})); | ||
done(); | ||
@@ -233,5 +289,5 @@ }); | ||
var options = { | ||
cwd: "/", | ||
base: "/test/", | ||
path: "/test/test.coffee", | ||
cwd: '/', | ||
base: '/test/', | ||
path: '/test/test.coffee', | ||
contents: null | ||
@@ -252,6 +308,6 @@ }; | ||
var options = { | ||
cwd: "/", | ||
base: "/test/", | ||
path: "/test/test.js", | ||
contents: new Buffer("test"), | ||
cwd: '/', | ||
base: '/test/', | ||
path: '/test/test.js', | ||
contents: new Buffer('test'), | ||
stat: fs.statSync(__filename) | ||
@@ -270,8 +326,8 @@ }; | ||
}); | ||
it('should copy custom properties', function(done) { | ||
var options = { | ||
cwd: "/", | ||
base: "/test/", | ||
path: "/test/test.coffee", | ||
cwd: '/', | ||
base: '/test/', | ||
path: '/test/test.coffee', | ||
contents: null | ||
@@ -289,5 +345,67 @@ }; | ||
file2.path.should.equal(file.path); | ||
file2.custom.should.equal(file.custom); | ||
file2.custom.a.should.equal(file.custom.a); | ||
done(); | ||
}); | ||
it('should copy history', function(done) { | ||
var options = { | ||
cwd: '/', | ||
base: '/test/', | ||
path: '/test/test.coffee', | ||
contents: null | ||
}; | ||
var file = new File(options); | ||
file.path = '/test/test.js'; | ||
file.path = '/test/test-938di2s.js'; | ||
var file2 = file.clone(); | ||
file2.history.should.eql([ | ||
'/test/test.coffee', | ||
'/test/test.js', | ||
'/test/test-938di2s.js' | ||
]); | ||
file2.history.should.not.equal([ | ||
'/test/test.coffee', | ||
'/test/test.js', | ||
'/test/test-938di2s.js' | ||
]); | ||
file2.path.should.eql('/test/test-938di2s.js'); | ||
done(); | ||
}); | ||
it('should copy all attributes deeply', 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(true); | ||
file2.custom.should.eql(file.custom); | ||
file2.custom.should.not.equal(file.custom); | ||
file2.custom.a.should.equal(file.custom.a); | ||
var file3 = file.clone({ deep: true }); | ||
file3.custom.should.eql(file.custom); | ||
file3.custom.should.not.equal(file.custom); | ||
file3.custom.a.should.equal(file.custom.a); | ||
var file4 = file.clone(false); | ||
file4.custom.should.eql(file.custom); | ||
file4.custom.should.equal(file.custom); | ||
file4.custom.a.should.equal(file.custom.a); | ||
var file5 = file.clone({ deep: false }); | ||
file5.custom.should.eql(file.custom); | ||
file5.custom.should.equal(file.custom); | ||
file5.custom.a.should.equal(file.custom.a); | ||
done(); | ||
@@ -300,6 +418,6 @@ }); | ||
var options = { | ||
cwd: "/", | ||
base: "/test/", | ||
path: "/test/test.coffee", | ||
contents: new Buffer("test") | ||
cwd: '/', | ||
base: '/test/', | ||
path: '/test/test.coffee', | ||
contents: new Buffer('test') | ||
}; | ||
@@ -313,3 +431,3 @@ var file = new File(options); | ||
}); | ||
stream.on('end', function(chunk) { | ||
stream.on('end', function() { | ||
done(); | ||
@@ -322,7 +440,7 @@ }); | ||
it('should pipe to stream with Stream', function(done) { | ||
var testChunk = new Buffer("test"); | ||
var testChunk = new Buffer('test'); | ||
var options = { | ||
cwd: "/", | ||
base: "/test/", | ||
path: "/test/test.coffee", | ||
cwd: '/', | ||
base: '/test/', | ||
path: '/test/test.coffee', | ||
contents: new Stream.PassThrough() | ||
@@ -346,5 +464,5 @@ }; | ||
var options = { | ||
cwd: "/", | ||
base: "/test/", | ||
path: "/test/test.coffee", | ||
cwd: '/', | ||
base: '/test/', | ||
path: '/test/test.coffee', | ||
contents: null | ||
@@ -354,4 +472,4 @@ }; | ||
var stream = new Stream.PassThrough(); | ||
stream.on('data', function(chunk) { | ||
throw new Error("should not write"); | ||
stream.on('data', function() { | ||
throw new Error('should not write'); | ||
}); | ||
@@ -367,6 +485,6 @@ stream.on('end', function() { | ||
var options = { | ||
cwd: "/", | ||
base: "/test/", | ||
path: "/test/test.coffee", | ||
contents: new Buffer("test") | ||
cwd: '/', | ||
base: '/test/', | ||
path: '/test/test.coffee', | ||
contents: new Buffer('test') | ||
}; | ||
@@ -381,4 +499,4 @@ var file = new File(options); | ||
}); | ||
stream.on('end', function(chunk) { | ||
throw new Error("should not end"); | ||
stream.on('end', function() { | ||
throw new Error('should not end'); | ||
}); | ||
@@ -390,7 +508,7 @@ var ret = file.pipe(stream, {end: false}); | ||
it('should pipe to stream with Stream', function(done) { | ||
var testChunk = new Buffer("test"); | ||
var testChunk = new Buffer('test'); | ||
var options = { | ||
cwd: "/", | ||
base: "/test/", | ||
path: "/test/test.coffee", | ||
cwd: '/', | ||
base: '/test/', | ||
path: '/test/test.coffee', | ||
contents: new Stream.PassThrough() | ||
@@ -406,4 +524,4 @@ }; | ||
}); | ||
stream.on('end', function(chunk) { | ||
throw new Error("should not end"); | ||
stream.on('end', function() { | ||
throw new Error('should not end'); | ||
}); | ||
@@ -418,5 +536,5 @@ var ret = file.pipe(stream, {end: false}); | ||
var options = { | ||
cwd: "/", | ||
base: "/test/", | ||
path: "/test/test.coffee", | ||
cwd: '/', | ||
base: '/test/', | ||
path: '/test/test.coffee', | ||
contents: null | ||
@@ -426,7 +544,7 @@ }; | ||
var stream = new Stream.PassThrough(); | ||
stream.on('data', function(chunk) { | ||
throw new Error("should not write"); | ||
stream.on('data', function() { | ||
throw new Error('should not write'); | ||
}); | ||
stream.on('end', function(chunk) { | ||
throw new Error("should not end"); | ||
stream.on('end', function() { | ||
throw new Error('should not end'); | ||
}); | ||
@@ -447,3 +565,3 @@ var ret = file.pipe(stream, {end: false}); | ||
it('should return correct format when Buffer and no path', function(done) { | ||
var val = new Buffer("test"); | ||
var val = new Buffer('test'); | ||
var file = new File({ | ||
@@ -457,7 +575,7 @@ contents: val | ||
it('should return correct format when Buffer and relative path', function(done) { | ||
var val = new Buffer("test"); | ||
var val = new Buffer('test'); | ||
var file = new File({ | ||
cwd: "/", | ||
base: "/test/", | ||
path: "/test/test.coffee", | ||
cwd: '/', | ||
base: '/test/', | ||
path: '/test/test.coffee', | ||
contents: val | ||
@@ -470,6 +588,6 @@ }); | ||
it('should return correct format when Buffer and only path and no base', function(done) { | ||
var val = new Buffer("test"); | ||
var val = new Buffer('test'); | ||
var file = new File({ | ||
cwd: "/", | ||
path: "/test/test.coffee", | ||
cwd: '/', | ||
path: '/test/test.coffee', | ||
contents: val | ||
@@ -484,5 +602,5 @@ }); | ||
var file = new File({ | ||
cwd: "/", | ||
base: "/test/", | ||
path: "/test/test.coffee", | ||
cwd: '/', | ||
base: '/test/', | ||
path: '/test/test.coffee', | ||
contents: new Stream.PassThrough() | ||
@@ -496,5 +614,5 @@ }); | ||
var file = new File({ | ||
cwd: "/", | ||
base: "/test/", | ||
path: "/test/test.coffee", | ||
cwd: '/', | ||
base: '/test/', | ||
path: '/test/test.coffee', | ||
contents: null | ||
@@ -509,3 +627,3 @@ }); | ||
it('should work with Buffer', function(done) { | ||
var val = new Buffer("test"); | ||
var val = new Buffer('test'); | ||
var file = new File(); | ||
@@ -534,3 +652,3 @@ file.contents = val; | ||
it('should not work with string', function(done) { | ||
var val = "test"; | ||
var val = 'test'; | ||
var file = new File(); | ||
@@ -550,3 +668,3 @@ try { | ||
try { | ||
file.relative = "test"; | ||
file.relative = 'test'; | ||
} catch (err) { | ||
@@ -583,7 +701,7 @@ should.exist(err); | ||
var file = new File({ | ||
cwd: "/", | ||
base: "/test/", | ||
path: "/test/test.coffee" | ||
cwd: '/', | ||
base: '/test/', | ||
path: '/test/test.coffee' | ||
}); | ||
file.relative.should.equal("test.coffee"); | ||
file.relative.should.equal('test.coffee'); | ||
done(); | ||
@@ -594,6 +712,6 @@ }); | ||
var file = new File({ | ||
cwd: "/", | ||
path: "/test/test.coffee" | ||
cwd: '/', | ||
path: '/test/test.coffee' | ||
}); | ||
file.relative.should.equal(path.join("test","test.coffee")); | ||
file.relative.should.equal(path.join('test','test.coffee')); | ||
done(); | ||
@@ -661,3 +779,2 @@ }); | ||
}); | ||
}); | ||
} |
var isBuffer = require('../lib/isBuffer'); | ||
var Stream = require('stream'); | ||
var should = require('should'); | ||
require('should'); | ||
require('mocha'); | ||
@@ -29,2 +29,2 @@ | ||
}); | ||
}); | ||
}); |
var isNull = require('../lib/isNull'); | ||
var Stream = require('stream'); | ||
var should = require('should'); | ||
require('should'); | ||
require('mocha'); | ||
@@ -20,5 +19,5 @@ | ||
isNull(1).should.equal(false); | ||
isNull("test").should.equal(false); | ||
isNull('test').should.equal(false); | ||
done(); | ||
}); | ||
}); | ||
}); |
var isStream = require('../lib/isStream'); | ||
var Stream = require('stream'); | ||
var should = require('should'); | ||
require('should'); | ||
require('mocha'); | ||
@@ -29,2 +29,2 @@ | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
201754
32
1016
3