Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

refctored and added test suite.

  • Loading branch information...
commit a6d931e0759274e2f24a4f7fdccbf5253e9c28de 1 parent b625a06
@kilianc authored
View
17 Makefile
@@ -0,0 +1,17 @@
+REPORTER = spec
+
+test:
+ @NODE_ENV=test ./node_modules/.bin/mocha test/*.test.js $(OPT) --reporter $(REPORTER)
+
+test-bail:
+ @rm -rf lib-cov
+ @jscoverage lib lib-cov
+ $(MAKE) test OPT=--bail
+
+test-cov:
+ @rm -rf lib-cov
+ @jscoverage lib lib-cov
+ @BUFFER_JOINER_COV=1 $(MAKE) test REPORTER=html-cov > coverage.html
+ @open -g coverage.html
+
+.PHONY: test test-bail test-cov
View
41 README.md
@@ -1,29 +1,36 @@
-# node-bufferjoiner ![project status](http://dl.dropbox.com/u/2208502/maintained.png)
+# BufferJoiner [![build status](https://secure.travis-ci.org/kilianc/node-bufferjoiner.png?branch=master)](http://travis-ci.org/kilianc/node-bufferjoiner)
A small utility for merging binary data.
-## Dependencies
+## Installation
-- nodejs v0.4.12+
+ ⚡ npm install bufferjoiner
-## Installation as submodule
+## Usage
- $ git clone git://github.com/kilian/node-bufferjoiner.git
+```js
-## Installation with npm
+var bf = new BufferJoiner();
+bf.add(buff1);
+bf.add(buff2);
+bf.add(buff3);
- $ npm install bufferjoiner
+// the last parameter is a Boolean, if true the merged buffer will be re-added to the buffers list.
+var mergedBuffer = bf.join(true);
-## Usage
+```
+
+# How to contribute
+
+This repository follows (more or less) the [Felix's Node.js Style Guide](http://nodeguide.com/style.html), your contribution must be consistent with this style.
- var reAddMerged;
- var bufferJoiner = new BufferJoiner();
+The test suite is written on top of [visionmedia/mocha](http://visionmedia.github.com/mocha/) and it took hours of hard work. Please use the tests to check if your contribution is breaking some part of the library and add new tests for each new feature.
- bufferJoiner.add(buff1);
- bufferJoiner.add(buff2);
- bufferJoiner.add(buff3);
+ ⚡ make test
- var merged = bufferJoiner.join(reAddMerged = true);
+and for your test coverage
+
+ ⚡ make test-cov
## License
@@ -39,10 +46,10 @@ _This software is released under the MIT license cited below_.
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
-
+
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
-
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@@ -50,4 +57,4 @@ _This software is released under the MIT license cited below_.
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
- OTHER DEALINGS IN THE SOFTWARE.
+ OTHER DEALINGS IN THE SOFTWARE.
View
3  index.js
@@ -0,0 +1,3 @@
+module.exports = process.env.BUFFER_JOINER_COV
+ ? require('./lib-cov/bufferjoiner')
+ : require('./lib/bufferjoiner')
View
75 lib-cov/bufferjoiner.js
@@ -0,0 +1,75 @@
+/* automatically generated by JSCoverage - do not edit */
+if (typeof _$jscoverage === 'undefined') _$jscoverage = {};
+if (! _$jscoverage['bufferjoiner.js']) {
+ _$jscoverage['bufferjoiner.js'] = [];
+ _$jscoverage['bufferjoiner.js'][7] = 0;
+ _$jscoverage['bufferjoiner.js'][8] = 0;
+ _$jscoverage['bufferjoiner.js'][9] = 0;
+ _$jscoverage['bufferjoiner.js'][12] = 0;
+ _$jscoverage['bufferjoiner.js'][18] = 0;
+ _$jscoverage['bufferjoiner.js'][32] = 0;
+ _$jscoverage['bufferjoiner.js'][33] = 0;
+ _$jscoverage['bufferjoiner.js'][43] = 0;
+ _$jscoverage['bufferjoiner.js'][44] = 0;
+ _$jscoverage['bufferjoiner.js'][45] = 0;
+ _$jscoverage['bufferjoiner.js'][46] = 0;
+ _$jscoverage['bufferjoiner.js'][58] = 0;
+ _$jscoverage['bufferjoiner.js'][59] = 0;
+ _$jscoverage['bufferjoiner.js'][60] = 0;
+ _$jscoverage['bufferjoiner.js'][61] = 0;
+ _$jscoverage['bufferjoiner.js'][63] = 0;
+ _$jscoverage['bufferjoiner.js'][64] = 0;
+ _$jscoverage['bufferjoiner.js'][65] = 0;
+ _$jscoverage['bufferjoiner.js'][68] = 0;
+ _$jscoverage['bufferjoiner.js'][69] = 0;
+ _$jscoverage['bufferjoiner.js'][71] = 0;
+}
+_$jscoverage['bufferjoiner.js'][7]++;
+var BufferJoiner = module.exports = (function BufferJoiner() {
+ _$jscoverage['bufferjoiner.js'][8]++;
+ if (! (this instanceof BufferJoiner)) {
+ _$jscoverage['bufferjoiner.js'][9]++;
+ return new BufferJoiner();
+ }
+ _$jscoverage['bufferjoiner.js'][12]++;
+ Object.defineProperty(this, "_buffersList", {value: [], writable: true, enumerable: false, configurable: false});
+ _$jscoverage['bufferjoiner.js'][18]++;
+ Object.defineProperty(this, "_length", {value: 0, writable: true, enumerable: false, configurable: false});
+});
+_$jscoverage['bufferjoiner.js'][32]++;
+BufferJoiner.prototype.__defineGetter__("length", (function length() {
+ _$jscoverage['bufferjoiner.js'][33]++;
+ return this._length;
+}));
+_$jscoverage['bufferjoiner.js'][43]++;
+BufferJoiner.prototype.add = (function add(buffer) {
+ _$jscoverage['bufferjoiner.js'][44]++;
+ this._buffersList.push(buffer);
+ _$jscoverage['bufferjoiner.js'][45]++;
+ this._length += buffer.length;
+ _$jscoverage['bufferjoiner.js'][46]++;
+ return this;
+});
+_$jscoverage['bufferjoiner.js'][58]++;
+BufferJoiner.prototype.join = (function join(reAdd) {
+ _$jscoverage['bufferjoiner.js'][59]++;
+ var result = new Buffer(this._length);
+ _$jscoverage['bufferjoiner.js'][60]++;
+ var lastFreeIndex = 0;
+ _$jscoverage['bufferjoiner.js'][61]++;
+ var buffer;
+ _$jscoverage['bufferjoiner.js'][63]++;
+ while (buffer = this._buffersList.shift()) {
+ _$jscoverage['bufferjoiner.js'][64]++;
+ buffer.copy(result, lastFreeIndex);
+ _$jscoverage['bufferjoiner.js'][65]++;
+ lastFreeIndex += buffer.length;
+}
+ _$jscoverage['bufferjoiner.js'][68]++;
+ this._length = 0;
+ _$jscoverage['bufferjoiner.js'][69]++;
+ reAdd && this.add(result);
+ _$jscoverage['bufferjoiner.js'][71]++;
+ return result;
+});
+_$jscoverage['bufferjoiner.js'].source = ["/**"," * Setup BufferJoiner."," *"," * @api public"," */","","var BufferJoiner = module.exports = function BufferJoiner() {"," if (!(this instanceof BufferJoiner)) {"," return new BufferJoiner()"," }",""," Object.defineProperty(this, '_buffersList', {"," value: [],"," writable: true,"," enumerable: false,"," configurable: false"," })"," Object.defineProperty(this, '_length', {"," value: 0,"," writable: true,"," enumerable: false,"," configurable: false"," })","}","","/**"," * Return `this._length`"," *"," * @api public"," */","","BufferJoiner.prototype.__defineGetter__('length', function length() {"," return this._length","})","","/**"," * Add buffer into the buffers list."," *"," * @param {Buffer} buffer"," * @api public"," */","","BufferJoiner.prototype.add = function add(buffer) {"," this._buffersList.push(buffer)"," this._length += buffer.length"," return this","}","","/**"," * Return a joined version of the buffers list"," * add reset status. If `reAdd` return value"," * will be readded to the buffers list."," *"," * @param {Boolean} reAdd"," * @api public"," */","","BufferJoiner.prototype.join = function join(reAdd) {"," var result = new Buffer(this._length)"," var lastFreeIndex = 0"," var buffer",""," while (buffer = this._buffersList.shift()) {"," buffer.copy(result, lastFreeIndex)"," lastFreeIndex += buffer.length"," }",""," this._length = 0"," reAdd && this.add(result)",""," return result","}"];
View
93 lib/bufferjoiner.js
@@ -1,29 +1,72 @@
-var BufferJoiner = function(){
- this.buffersList = [];
- this.length = 0;
-};
-
-BufferJoiner.prototype.add = function(buffer){
- this.buffersList.push(buffer);
- this.length += buffer.length;
- return this;
-};
-
-BufferJoiner.prototype.join = function(reAdd){
-
- var totalBuffer = new Buffer(this.length);
- var lastFreeIndex = 0;
- var buffer;
-
- while(buffer = this.buffersList.shift()){
- buffer.copy(totalBuffer, lastFreeIndex);
- lastFreeIndex += buffer.length;
+/**
+ * Setup BufferJoiner.
+ *
+ * @api public
+ */
+
+var BufferJoiner = module.exports = function BufferJoiner() {
+ if (!(this instanceof BufferJoiner)) {
+ return new BufferJoiner()
}
- this.length = 0;
- reAdd && this.add(totalBuffer);
+ Object.defineProperty(this, '_buffersList', {
+ value: [],
+ writable: true,
+ enumerable: false,
+ configurable: false
+ })
+ Object.defineProperty(this, '_length', {
+ value: 0,
+ writable: true,
+ enumerable: false,
+ configurable: false
+ })
+}
+
+/**
+ * Return `this._length`
+ *
+ * @api public
+ */
+
+BufferJoiner.prototype.__defineGetter__('length', function length() {
+ return this._length
+})
+
+/**
+ * Add buffer into the buffers list.
+ *
+ * @param {Buffer} buffer
+ * @api public
+ */
+
+BufferJoiner.prototype.add = function add(buffer) {
+ this._buffersList.push(buffer)
+ this._length += buffer.length
+ return this
+}
+
+/**
+ * Return a joined version of the buffers list
+ * add reset status. If `reAdd` return value
+ * will be readded to the buffers list.
+ *
+ * @param {Boolean} reAdd
+ * @api public
+ */
+
+BufferJoiner.prototype.join = function join(reAdd) {
+ var result = new Buffer(this._length)
+ var lastFreeIndex = 0
+ var buffer
+
+ while (buffer = this._buffersList.shift()) {
+ buffer.copy(result, lastFreeIndex)
+ lastFreeIndex += buffer.length
+ }
- return totalBuffer;
-};
+ this._length = 0
+ reAdd && this.add(result)
-module.exports = BufferJoiner;
+ return result
+}
View
15 package.json
@@ -1,12 +1,21 @@
{
"author": "Kilian Ciuffolo <me@nailik.org> (http://nailik.org)",
"name": "bufferjoiner",
- "main" : "./lib/bufferjoiner.js",
+ "main": "./",
"description": "A nodejs binary buffer utility",
- "keywords": ["buffer", "merge", "join", "binary"],
- "version": "0.1.1",
+ "keywords": [
+ "buffer",
+ "merge",
+ "join",
+ "binary"
+ ],
+ "version": "0.1.2",
"repository": {
"type": "git",
"url": "http://github.com/kilianc/node-bufferjoiner.git"
+ },
+ "devDependencies": {
+ "mocha": "1.0.x",
+ "chai": "1.0.x"
}
}
View
71 test/bufferjoiner.test.js
@@ -0,0 +1,71 @@
+var BufferJoiner = require('../'),
+ expect = require('chai').expect
+
+describe('BufferJoiner', function () {
+ describe('#()', function () {
+ it('should work as function', function () {
+ expect(BufferJoiner()).to.be.instanceOf(BufferJoiner)
+ })
+ it('should work as constructor', function () {
+ expect(new BufferJoiner()).to.be.instanceOf(BufferJoiner)
+ })
+ it('should create properties', function () {
+ var bf = new BufferJoiner()
+ expect(bf._length).to.be.equal(0)
+ expect(bf._buffersList).to.be.instanceOf(Array)
+ expect(bf._buffersList).to.be.lengthOf(0)
+ })
+ })
+ describe('#add()', function () {
+ it('should store chunks and update length', function () {
+ var bf = BufferJoiner()
+ var buff1 = Buffer('aaa')
+ var buff2 = Buffer('bbb')
+
+ bf.add(buff1)
+ bf.add(buff2)
+
+ expect(bf._buffersList).to.be.lengthOf(2)
+ expect(bf._buffersList[0]).to.be.equal(buff1)
+ expect(bf._buffersList[1]).to.be.equal(buff2)
+ expect(bf._length).to.be.equal(6)
+ })
+ })
+ describe('#join()', function () {
+ before(function () {
+ var bf = BufferJoiner()
+ var buff1 = Buffer('aaa')
+ var buff2 = Buffer('bbb')
+
+ bf.add(buff1)
+ bf.add(buff2)
+
+ this.merged = bf.join()
+ this.bf = bf
+ })
+ it('should return merged chunks', function () {
+ expect(this.merged).to.be.lengthOf(6)
+ expect(this.merged.toString()).to.be.equal('aaabbb')
+ })
+ it('should reset status', function () {
+ expect(this.bf._length).to.be.equal(0)
+ expect(this.bf._buffersList).to.be.eql([])
+ })
+ it('should reAdd if asked', function () {
+ this.bf.add(Buffer('a'))
+ this.bf.add(Buffer('b'))
+ this.merged = this.bf.join(true)
+
+ expect(this.bf._buffersList).to.be.lengthOf(1)
+ expect(this.bf._length).to.be.equal(2)
+ expect(this.bf._buffersList[0]).to.be.equal(this.merged)
+ })
+ })
+ describe('#length', function () {
+ it('should return total length', function () {
+ var bf = BufferJoiner()
+ bf.add(Buffer('aaa'))
+ expect(bf.length).to.be.equal(3)
+ })
+ })
+})
Please sign in to comment.
Something went wrong with that request. Please try again.