Comparing version 0.1.0 to 0.2.0
@@ -0,3 +1,11 @@ | ||
## 0.2.0 - Released 2018/02/07 | ||
1. update Travis to use main coverage script, remove node 7, add node's 8 and 9 | ||
2. add 2018 to LICENSE | ||
3. remove extraneous semi-colons from lib/index.js (all but one). | ||
4. add more comments to code. | ||
5. reformat with whitespace | ||
## 0.1.0 - Released 2017/05/26 | ||
1. initial working version with tests and 100% coverage |
150
lib/index.js
@@ -1,12 +0,9 @@ | ||
var Buffer, Duplex; | ||
var Buffer = require('buffer').Buffer | ||
var Duplex = require('stream').Duplex | ||
Buffer = require('buffer').Buffer; | ||
Duplex = require('stream').Duplex; | ||
function Buffed(source) { | ||
var length; | ||
Duplex.call(this, { decodeStrings: true }); | ||
Duplex.call(this, { decodeStrings: true }) | ||
if (source) { | ||
this._source = Array.isArray(source) ? source : [source]; | ||
this._source = Array.isArray(source) ? source : [source] | ||
} | ||
@@ -16,3 +13,3 @@ } | ||
// extend Duplex | ||
Buffed.prototype = Object.create(Duplex.prototype); | ||
Buffed.prototype = Object.create(Duplex.prototype) | ||
Buffed.prototype.constructor = Buffed | ||
@@ -22,22 +19,41 @@ | ||
Buffed.prototype._read = function(size) { | ||
if (this._source) { | ||
switch (this._source.length) { | ||
case 0: this.push(null); break; | ||
case 1: this.push(this._source.pop()); break; | ||
case 0: | ||
this.push(null) | ||
break | ||
case 1: | ||
this.push(this._source.pop()) | ||
break | ||
default: // op in loop condition | ||
while (this.push(this._source.shift()) && this._source.length) ; | ||
while (this.push(this._source.shift()) && this._source.length) | ||
/* noop */ ; | ||
} | ||
} else { | ||
this.push(null); | ||
} | ||
}; | ||
else { | ||
this.push(null) | ||
} | ||
} | ||
// accumulates what's written into `buffers` | ||
Buffed.prototype._write = function(buffer, _, next) { | ||
var buffers; | ||
// if we already have some then place the new one at the end | ||
// and add its byte count. | ||
if (this.buffers) { | ||
this.buffers[this.buffers.length] = buffer | ||
this.bufferBytes += buffer.length | ||
} else { | ||
} | ||
// otherwise, create a new array with the buffer in it | ||
// and store its byte count. | ||
else { | ||
this.buffers = [buffer] | ||
@@ -47,20 +63,42 @@ this.bufferBytes = buffer.length | ||
next(); | ||
}; | ||
next() | ||
} | ||
// combine all the gathered buffers into one buffer | ||
Buffed.prototype.combine = function combine() { | ||
var combined; | ||
// will hold a single Buffer with all bytes. | ||
var combined | ||
// if we have stored buffers | ||
if (this.buffers) { | ||
// determine what to do by the number of buffers we have stored. | ||
switch (this.buffers.length) { | ||
case 0: combined = new Buffer(0); break; | ||
case 1: combined = this.buffers[0]; break; | ||
default: | ||
combined = Buffer.concat(this.buffers, this.bufferBytes); | ||
this.buffers = [combined]; | ||
case 0: // none, so, create an empty Buffer. | ||
combined = new Buffer(0) | ||
break | ||
case 1: // only one, so, just return that one Buffer. | ||
combined = this.buffers[0] | ||
break | ||
default: // more than one, so concat them all together. | ||
combined = Buffer.concat(this.buffers, this.bufferBytes) | ||
// also, take this opportunity to store our work by | ||
// replacing the array of Buffer's with our new single one. | ||
// this means calling `combine()` repeatedly will do this work once. | ||
this.buffers = [combined] | ||
} | ||
} else { | ||
combined = new Buffer(0); | ||
} | ||
return combined; | ||
// otherwise, we have nothing, so, create an empty Buffer. | ||
else { | ||
combined = new Buffer(0) | ||
} | ||
// return the Buffer result. | ||
return combined | ||
} | ||
@@ -71,19 +109,39 @@ | ||
Buffed.prototype.pipe = function pipe(target) { | ||
// if: | ||
// 1. there isn't a target to pipe to | ||
// 2. or, the target is a Buffer | ||
// 3. or the target is an array | ||
if (!target || Buffer.isBuffer(target) || Array.isArray(target)) { | ||
return this.reset(target); | ||
} else { | ||
return Duplex.prototype.pipe.call(this, target); | ||
// give the target to reset() | ||
return this.reset(target) | ||
} | ||
}; | ||
// otherwise, it's a stream, so use the Duplex constructor to | ||
// reinitialize streaming to this target. | ||
else { | ||
return Duplex.prototype.pipe.call(this, target) | ||
} | ||
} | ||
// make ready to use it again. | ||
// optionally allow a new source. | ||
Buffed.prototype.reset = function reset(source) { | ||
Duplex.call(this, { decodeStrings: true }); | ||
// initialize with the constructor. we decode strings into bytes. | ||
Duplex.call(this, { decodeStrings: true }) | ||
// if an optional source was provided then store it as an array. | ||
// if it's already an array, then use it. | ||
if (source) { | ||
this._source = Array.isArray(source) ? source : [source]; | ||
} else if (this._source){ | ||
this._source = Array.isArray(source) ? source : [source] | ||
} | ||
// else there's no new source, so if there's a current source stored | ||
// then truncate its array to get rid of it. | ||
else if (this._source){ | ||
this._source.length = 0 | ||
} | ||
// lastly, if there are stored buffers then truncate its array. | ||
if (this.buffers) { | ||
@@ -93,21 +151,25 @@ this.buffers.length = 0 | ||
return this; | ||
}; | ||
// return this for chaining. | ||
return this | ||
} | ||
// export a function which creates a Buffed instance | ||
// export a function which creates a Buffed instance. | ||
// avoids them needing to know to use the `new` keyword. | ||
module.exports = function buildBuffed(source) { | ||
return new Buffed(source); | ||
}; | ||
return new Buffed(source) | ||
} | ||
// export the class as a sub property on the function | ||
module.exports.Buffed = Buffed; | ||
module.exports.Buffed = Buffed | ||
// export a helper function on our exported function to start piping a buffer | ||
// export a helper function on our exported function to start piping a buffer. | ||
// it basically accepts a source and creates a Buffed instance with it. | ||
module.exports.pipe = function pipe(source) { | ||
if (source && ! (Buffer.isBuffer(source) || Array.isArray(source))) { | ||
throw new TypeError('must provide a buffer or array of buffers to pipe()'); | ||
throw new TypeError('must provide a buffer or array of buffers to pipe()') | ||
} | ||
return new Buffed(source); | ||
}; | ||
return new Buffed(source) | ||
} |
{ | ||
"name": "buffed", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Acts as a stream to send a buffer, gather buffers, or both.", | ||
@@ -36,7 +36,7 @@ "main": "lib/index.js", | ||
"test4": "nave use 4.8 npm test", | ||
"test6": "nave use 6.10 npm test", | ||
"test7": "nave use 7.10 npm test", | ||
"test-all": "npm run test4 && npm run test6 && npm run test7", | ||
"coverage": "istanbul cover -x 'build/**' _mocha -- -R spec test/lib", | ||
"coverage-lcov": "istanbul cover -x 'build/**' _mocha --report lcovonly -- -R spec test/lib" | ||
"test6": "nave use 6.12 npm test", | ||
"test8": "nave use 8.9 npm test", | ||
"test9": "nave use 9.5 npm test", | ||
"test-all": "npm run test4 && npm run test6 && npm run test8 && npm run test9", | ||
"coverage": "istanbul cover -x 'build/**' _mocha -- -R spec test/lib" | ||
}, | ||
@@ -49,5 +49,5 @@ "repository": { | ||
"devDependencies": { | ||
"coveralls": "^2.13.1", | ||
"coveralls": "^3.0.0", | ||
"istanbul": "^0.4.5", | ||
"mocha": "^3.4.2", | ||
"mocha": "^5.0.0", | ||
"mocha-lcov-reporter": "^1.3.0", | ||
@@ -54,0 +54,0 @@ "through2": "^2.0.3" |
Sorry, the diff of this file is not supported yet
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
11284
129