Comparing version 0.3.0 to 1.0.0
@@ -5,3 +5,3 @@ var Container = typeof Buffer !== "undefined" ? Buffer //in node, use buffers | ||
function BitField(data){ | ||
function BitField(data, opts){ | ||
if(!(this instanceof BitField)) { | ||
@@ -15,3 +15,5 @@ return new BitField(data); | ||
if(typeof data === "number"){ | ||
this.grow = (opts && opts.grow) || 0; | ||
if(typeof data === "number" || data === undefined){ | ||
if(data % 8 !== 0) data += 1 << 3; | ||
@@ -43,3 +45,3 @@ data = new Container(data >> 3); | ||
BitField.prototype._grow = function(length) { | ||
if (this.buffer.length < length) { | ||
if (this.buffer.length < length && length <= this.grow) { | ||
var newBuffer = new Container(length); | ||
@@ -46,0 +48,0 @@ if (newBuffer.fill) newBuffer.fill(0); |
{ | ||
"name": "bitfield", | ||
"version": "0.3.0", | ||
"version": "1.0.0", | ||
"description": "a very simple bitfield implementation using buffers", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -33,2 +33,6 @@ #bitfield | ||
##### Auto-grow mode | ||
`Bitfield(data, { grow: size })`: If you `set` an index that is out-of-bounds, the Bitfield will automatically grow so that the bitfield is big enough to contain the given index, up to the given `size`. If you want the Bitfield to grow indefinitely, pass `Infinity` as the size. | ||
####Properties | ||
@@ -35,0 +39,0 @@ `Bitfield#buffer`: The contents of the bitfield. |
47
tests.js
var assert = require("assert"), | ||
BitField = require("./"); | ||
BitField = require("./"); | ||
var data = "011011100110111".split("").map(Number).map(Boolean), | ||
field = new BitField(data.length); | ||
field = new BitField(data.length), | ||
growField = new BitField(data.length, { grow: Infinity }), | ||
smallGrowField = new BitField(data.length, { grow: 50 }); | ||
@@ -27,13 +29,38 @@ for(var i = 0; i < data.length; i++){ | ||
for(var i = 0; i < 100; i++) { | ||
index += 8 + Math.floor(32 * Math.random()); | ||
index += 8 + Math.floor(32 * Math.random()); | ||
var oldLength = field.buffer.length; | ||
assert.strictEqual(field.get(index), false); | ||
assert.equal(field.buffer.length, oldLength, "should not have grown for get()"); | ||
field.set(index, true); | ||
var newLength = Math.ceil((index + 1) / 8); | ||
assert.equal(field.buffer.length, newLength, "should have grown for set()"); | ||
assert.strictEqual(field.get(index), true); | ||
var oldLength = field.buffer.length; | ||
assert.strictEqual(field.get(index), false); | ||
assert.equal(field.buffer.length, oldLength, "should not have grown for get()"); | ||
field.set(index, true); | ||
var newLength = Math.ceil((index + 1) / 8); | ||
assert.equal(field.buffer.length, oldLength, "should not have grown for set()"); | ||
assert.strictEqual(field.get(index), false); | ||
} | ||
index = 25; | ||
for(var i = 0; i < 100; i++) { | ||
index += 8 + Math.floor(32 * Math.random()); | ||
var oldLength = growField.buffer.length; | ||
assert.strictEqual(growField.get(index), false); | ||
assert.equal(growField.buffer.length, oldLength, "should not have grown for get()"); | ||
growField.set(index, true); | ||
var newLength = Math.ceil((index + 1) / 8); | ||
assert.equal(growField.buffer.length, newLength, "should have grown for set()"); | ||
assert.strictEqual(growField.get(index), true); | ||
} | ||
for(var i = 0; i < 100; i++) { | ||
var oldLength = smallGrowField.buffer.length; | ||
smallGrowField.set(index, true); | ||
if (index <= 50) { | ||
assert.equal(smallGrowField.buffer.length, i, "should have grown for set()"); | ||
assert.strictEqual(smallGrowField.get(i), true); | ||
} else { | ||
assert.equal(smallGrowField.buffer.length, oldLength, "should have grown for set()"); | ||
assert.strictEqual(smallGrowField.get(i), false); | ||
} | ||
} | ||
var field2 = new BitField(); | ||
@@ -40,0 +67,0 @@ assert.ok(field2.buffer, "if no data or size passed in, should assume size 0"); |
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
6592
100
1
43