From b228c3160217ef38f007f8dbd46aa4d54baae360 Mon Sep 17 00:00:00 2001 From: Forbes Lindesay Date: Thu, 8 Nov 2012 09:25:39 +0000 Subject: [PATCH 1/8] export module --- bitarray.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/bitarray.js b/bitarray.js index 10e4217..b8789bc 100644 --- a/bitarray.js +++ b/bitarray.js @@ -1,5 +1,8 @@ +"use strict"; + function BitArray(size, value){ - if(value === undefined) value = 0; + if (typeof this === undefined) return new BitArray(size, value); + if (value === undefined) value = 0; this.size = size; this.field = new Array(~~((size - 1) / BitArray.ELEMENT_WIDTH) + 1); for(var i = 0; i < this.field.length; i++) @@ -35,4 +38,6 @@ BitArray.prototype.toString = function(){ return binary; }).reverse().join(''); return string.split('').reverse().join('').slice(0,this.size); -} \ No newline at end of file +} + +module.exports = BitArray; \ No newline at end of file From 2b1104ee2e865733869fdfb30e0e423cc967d014 Mon Sep 17 00:00:00 2001 From: Forbes Lindesay Date: Thu, 8 Nov 2012 09:28:37 +0000 Subject: [PATCH 2/8] Add component.json --- component.json | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 component.json diff --git a/component.json b/component.json new file mode 100644 index 0000000..544a39a --- /dev/null +++ b/component.json @@ -0,0 +1,15 @@ +{ + "name": "bitarray.js", + "repo": "microjs/bitarray.js", + "description": "Pure JavaScript bit array/bitfield implementation", + "version": "0.0.1", + "keywords": [], + "dependencies": {}, + "development": {}, + "scripts": [ + "bitarray.js" + ], + "main": "bitarray.js", + "twitter": "@ForbesLindesay", + "license": "MIT" +} \ No newline at end of file From 627adb3024726692b1d770b3129c88a59464d384 Mon Sep 17 00:00:00 2001 From: Forbes Lindesay Date: Sat, 24 Nov 2012 13:34:57 +0000 Subject: [PATCH 3/8] Add travis tests --- .gitignore | 1 + .travis.yml | 4 ++++ bitarray.js | 2 -- component.json | 4 ++-- package.json | 20 ++++++++++++++++++++ test.js | 42 +++++++++++++++++++++++++++--------------- 6 files changed, 54 insertions(+), 19 deletions(-) create mode 100644 .travis.yml create mode 100644 package.json diff --git a/.gitignore b/.gitignore index e43b0f9..91dfed8 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .DS_Store +node_modules \ No newline at end of file diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..8111245 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,4 @@ +language: node_js +node_js: + - 0.6 + - 0.8 \ No newline at end of file diff --git a/bitarray.js b/bitarray.js index b8789bc..0e8feba 100644 --- a/bitarray.js +++ b/bitarray.js @@ -1,5 +1,3 @@ -"use strict"; - function BitArray(size, value){ if (typeof this === undefined) return new BitArray(size, value); if (value === undefined) value = 0; diff --git a/component.json b/component.json index 544a39a..006a462 100644 --- a/component.json +++ b/component.json @@ -1,6 +1,6 @@ { - "name": "bitarray.js", - "repo": "microjs/bitarray.js", + "name": "bitarray", + "repo": "microjs/bitarray", "description": "Pure JavaScript bit array/bitfield implementation", "version": "0.0.1", "keywords": [], diff --git a/package.json b/package.json new file mode 100644 index 0000000..ad3d530 --- /dev/null +++ b/package.json @@ -0,0 +1,20 @@ +{ + "name": "bitarray", + "version": "0.0.1", + "description": "Pure JavaScript bit array/bitfield implementation", + "main": "./bitarray.js", + "scripts": { + "test": "mocha -R spec" + }, + "repository": { + "type": "git", + "url": "https://github.com/ForbesLindesay/bitarray.git" + }, + "author": "ForbesLindesay", + "license": "MIT", + "devDependencies": { + "mocha": "*", + "better-assert": "*" + }, + "dependencies": {} +} \ No newline at end of file diff --git a/test.js b/test.js index d113b28..86af5a1 100644 --- a/test.js +++ b/test.js @@ -1,18 +1,30 @@ -var fs = require('fs'), filedata = fs.readFileSync('bitarray.js','utf8'); -eval(filedata); +var assert = require('better-assert'); +var BitArray = require('./'); -var ba = new BitArray(200); -ba.set(100, 1); -console.log(ba.field); -console.log(ba.get(100), 'should be 1'); +describe('a bit array of length 200', function () { + var ba = new BitArray(200); + it('lets you set a value to 1', function () { + ba.set(100, 1); + assert(ba.get(100) === 1); + }); + it('lets you set that value back to 0', function () { + ba.set(100, 0); + assert(ba.get(100) === 0); + }); +}); -ba.set(100, 0); -console.log(ba.get(100), 'should be 0'); +describe('a bit array of length 20', function () { + it('lets you set lots of values independently', function () { + var ba = new BitArray(20); + [1,3,5,9,11,13,15].forEach(function(i){ ba.set(i, 1) }); + assert(ba.toString() === '01010100010101010000'); + }); +}); -ba = new BitArray(20); -[1,3,5,9,11,13,15].forEach(function(i){ ba.set(i, 1) }); -console.log(ba.toString() == '01010100010101010000', 'should be true'); - -ba = new BitArray(33,1); -[0].forEach(function(i){ ba.set(i, 0) }); -console.log(ba.toString() == '011111111111111111111111111111111', 'should be true'); \ No newline at end of file +describe('a bit array with a default of 1', function () { + it('lets you set a bit to 0', function () { + var ba = new BitArray(33,1); + ba.set(0, 0) + assert(ba.toString() == '011111111111111111111111111111111'); + }); +}); \ No newline at end of file From 97df540058e50d075e0f0baad0aefb3dc1436710 Mon Sep 17 00:00:00 2001 From: Forbes Lindesay Date: Sat, 24 Nov 2012 13:41:07 +0000 Subject: [PATCH 4/8] Support not using `new` --- bitarray.js | 6 ++++-- package.json | 1 - test.js | 6 +++--- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/bitarray.js b/bitarray.js index 0e8feba..6ac7948 100644 --- a/bitarray.js +++ b/bitarray.js @@ -1,6 +1,8 @@ +"use strict"; + function BitArray(size, value){ - if (typeof this === undefined) return new BitArray(size, value); - if (value === undefined) value = 0; + if (typeof this === 'undefined') return new BitArray(size, value); + if (typeof value === 'undefined') value = 0; this.size = size; this.field = new Array(~~((size - 1) / BitArray.ELEMENT_WIDTH) + 1); for(var i = 0; i < this.field.length; i++) diff --git a/package.json b/package.json index ad3d530..048973b 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,6 @@ "type": "git", "url": "https://github.com/ForbesLindesay/bitarray.git" }, - "author": "ForbesLindesay", "license": "MIT", "devDependencies": { "mocha": "*", diff --git a/test.js b/test.js index 86af5a1..785ef41 100644 --- a/test.js +++ b/test.js @@ -2,7 +2,7 @@ var assert = require('better-assert'); var BitArray = require('./'); describe('a bit array of length 200', function () { - var ba = new BitArray(200); + var ba = BitArray(200); it('lets you set a value to 1', function () { ba.set(100, 1); assert(ba.get(100) === 1); @@ -15,7 +15,7 @@ describe('a bit array of length 200', function () { describe('a bit array of length 20', function () { it('lets you set lots of values independently', function () { - var ba = new BitArray(20); + var ba = BitArray(20); [1,3,5,9,11,13,15].forEach(function(i){ ba.set(i, 1) }); assert(ba.toString() === '01010100010101010000'); }); @@ -23,7 +23,7 @@ describe('a bit array of length 20', function () { describe('a bit array with a default of 1', function () { it('lets you set a bit to 0', function () { - var ba = new BitArray(33,1); + var ba = BitArray(33,1); ba.set(0, 0) assert(ba.toString() == '011111111111111111111111111111111'); }); From 757a2e039184d62b2101dae5b87c622591c388a7 Mon Sep 17 00:00:00 2001 From: Forbes Lindesay Date: Sat, 24 Nov 2012 13:42:02 +0000 Subject: [PATCH 5/8] Add travis icon --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index b8fc5b8..a2a88ed 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ +[![Build Status](https://secure.travis-ci.org/microjs/bitarray.png?branch=master)](https://travis-ci.org/microjs/bitarray) # BitArray: A simple bit array/bit field library in pure JavaScript BitArray is based on the Ruby library BitArray by Peter Cooper (https://github.com/peterc/bitarray). From d907bc66de6bd00b9fdac8a614c7f1b8a23ff152 Mon Sep 17 00:00:00 2001 From: Forbes Lindesay Date: Sat, 24 Nov 2012 13:42:21 +0000 Subject: [PATCH 6/8] Release 1.0.0 --- component.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/component.json b/component.json index 006a462..ee927a1 100644 --- a/component.json +++ b/component.json @@ -2,7 +2,7 @@ "name": "bitarray", "repo": "microjs/bitarray", "description": "Pure JavaScript bit array/bitfield implementation", - "version": "0.0.1", + "version": "1.0.0", "keywords": [], "dependencies": {}, "development": {}, diff --git a/package.json b/package.json index 048973b..8dfcb31 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bitarray", - "version": "0.0.1", + "version": "1.0.0", "description": "Pure JavaScript bit array/bitfield implementation", "main": "./bitarray.js", "scripts": { From 6818099d99c722af25714f69c1d969e807a77b81 Mon Sep 17 00:00:00 2001 From: Forbes Lindesay Date: Sat, 24 Nov 2012 13:46:07 +0000 Subject: [PATCH 7/8] Update readme --- README.md | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index a2a88ed..f4284cd 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,21 @@ [![Build Status](https://secure.travis-ci.org/microjs/bitarray.png?branch=master)](https://travis-ci.org/microjs/bitarray) -# BitArray: A simple bit array/bit field library in pure JavaScript +# BitArray + +BitArray is a simple bit array/bit field library in pure JavaScript BitArray is based on the Ruby library BitArray by Peter Cooper (https://github.com/peterc/bitarray). -Basic, pure JavaScript bit field. +Basic, pure JavaScript bit field. + +## Installation + +Server: + + $ npm install bitarray + +Client: + + $ component install microjs/bitarray ## Examples @@ -20,10 +32,17 @@ ba.set(100, 1); ba.get(100); // => 1 ba.set(100, 0); -ba.get(100); // = > 0 +ba.get(100); // => 0 +``` + +Create a BitArray where all the bits default to 1: + +```javascript +var ba = new BitArray(100, 1); +ba.get(50); // => 1 ``` -More: +toString: ```javascript var ba = new BitArray(20); From f116c9ff2b8f76f21148a2fd44d39e78fe570a2c Mon Sep 17 00:00:00 2001 From: Forbes Lindesay Date: Sat, 24 Nov 2012 13:50:50 +0000 Subject: [PATCH 8/8] Support just using global BitArray --- bitarray.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bitarray.js b/bitarray.js index 6ac7948..9ab308d 100644 --- a/bitarray.js +++ b/bitarray.js @@ -40,4 +40,5 @@ BitArray.prototype.toString = function(){ return string.split('').reverse().join('').slice(0,this.size); } -module.exports = BitArray; \ No newline at end of file +if (typeof module != 'undefined') + module.exports = BitArray; \ No newline at end of file