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/README.md b/README.md index b8fc5b8..f4284cd 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,21 @@ -# BitArray: A simple bit array/bit field library in pure JavaScript +[![Build Status](https://secure.travis-ci.org/microjs/bitarray.png?branch=master)](https://travis-ci.org/microjs/bitarray) +# 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 @@ -19,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); diff --git a/bitarray.js b/bitarray.js index 10e4217..9ab308d 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 (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++) @@ -35,4 +38,7 @@ BitArray.prototype.toString = function(){ return binary; }).reverse().join(''); return string.split('').reverse().join('').slice(0,this.size); -} \ No newline at end of file +} + +if (typeof module != 'undefined') + module.exports = BitArray; \ No newline at end of file diff --git a/component.json b/component.json new file mode 100644 index 0000000..ee927a1 --- /dev/null +++ b/component.json @@ -0,0 +1,15 @@ +{ + "name": "bitarray", + "repo": "microjs/bitarray", + "description": "Pure JavaScript bit array/bitfield implementation", + "version": "1.0.0", + "keywords": [], + "dependencies": {}, + "development": {}, + "scripts": [ + "bitarray.js" + ], + "main": "bitarray.js", + "twitter": "@ForbesLindesay", + "license": "MIT" +} \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..8dfcb31 --- /dev/null +++ b/package.json @@ -0,0 +1,19 @@ +{ + "name": "bitarray", + "version": "1.0.0", + "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" + }, + "license": "MIT", + "devDependencies": { + "mocha": "*", + "better-assert": "*" + }, + "dependencies": {} +} \ No newline at end of file diff --git a/test.js b/test.js index d113b28..785ef41 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 = 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 = 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 = BitArray(33,1); + ba.set(0, 0) + assert(ba.toString() == '011111111111111111111111111111111'); + }); +}); \ No newline at end of file