Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

bitfield

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bitfield - npm Package Compare versions

Comparing version 1.0.0 to 1.0.1

16

index.js

@@ -14,7 +14,7 @@ var Container = typeof Buffer !== "undefined" ? Buffer //in node, use buffers

this.grow = (opts && opts.grow) || 0;
this.grow = opts && (isFinite(opts.grow) && getByteSize(opts.grow) || opts.grow) || 0;
if(typeof data === "number" || data === undefined){
if(data % 8 !== 0) data += 1 << 3;
data = new Container(data >> 3);
data = new Container(getByteSize(data));
if(data.fill) data.fill(0); // clear node buffers of garbage

@@ -25,2 +25,8 @@ }

function getByteSize(num){
var out = num >> 3;
if(num % 8 !== 0) out++;
return out;
}
BitField.prototype.get = function(i){

@@ -36,6 +42,6 @@ var j = i >> 3;

this._grow(j + 1);
/* Set */
// Set
this.buffer[j] |= 128 >> (i % 8);
} else if (j < this.buffer.length) {
/* Clear */
/// Clear
this.buffer[j] &= ~(128 >> (i % 8));

@@ -54,4 +60,4 @@ }

}
}
};
if(typeof module !== "undefined") module.exports = BitField;
{
"name": "bitfield",
"version": "1.0.0",
"version": "1.0.1",
"description": "a very simple bitfield implementation using buffers",

@@ -11,3 +11,3 @@ "main": "index.js",

"scripts": {
"test": "node tests.js"
"test": "tape tests.js"
},

@@ -18,2 +18,5 @@ "keywords": [

],
"devDependencies": {
"tape": "2.12"
},
"author": "Felix Boehm <me@feedic.com>",

@@ -20,0 +23,0 @@ "license": "MIT",

@@ -1,68 +0,98 @@

var assert = require("assert"),
BitField = require("./");
var test = require("tape"),
BitField = require("./");
var data = "011011100110111".split("").map(Number).map(Boolean),
field = new BitField(data.length),
growField = new BitField(data.length, { grow: Infinity }),
smallGrowField = new BitField(data.length, { grow: 50 });
field = new BitField(data.length);
for(var i = 0; i < data.length; i++){
assert.strictEqual(field.get(i), false, "bitfield should be empty when initialized");
}
test("bitfield should be empty when initialized", function(t){
for(var i = 0; i < data.length; i++){
t.strictEqual(field.get(i), false);
}
t.end();
});
//Write data
for(var i = 0; i < data.length; i++){
field.set(i, data[i]);
}
test("should reproduce written data", function(t){
for(var i = 0; i < data.length; i++){
field.set(i, data[i]);
}
for(var i = 0; i < data.length; i++){
assert.strictEqual(field.get(i), data[i], "should reproduce written data");
}
for(var i = 0; i < data.length; i++){
t.strictEqual(field.get(i), data[i]);
}
for(var i = data.length; i < 1e3; i++){
assert.strictEqual(field.get(i), false, "out-of-bounds should simply be false");
}
t.end();
});
var index = 25;
for(var i = 0; i < 100; i++) {
test("out-of-bounds should simply be false", function(t){
for(var i = data.length; i < 1e3; i++){
t.strictEqual(field.get(i), false);
}
t.end();
});
test("should not grow by default", function(t){
var index = 25;
for(var i = 0; i < 100; i++) {
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()");
t.strictEqual(field.get(index), false);
t.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++) {
t.equal(field.buffer.length, oldLength, "should not have grown for set()");
t.strictEqual(field.get(index), false);
}
t.end();
});
test("should be able to grow to infinity", function(t){
var growField = new BitField(data.length, { grow: Infinity }),
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()");
t.strictEqual(growField.get(index), false);
t.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);
}
t.equal(growField.buffer.length, newLength, "should have grown for set()");
t.strictEqual(growField.get(index), true);
}
for(var i = 0; i < 100; i++) {
t.end();
});
test("should restrict growth to growth option", function(t){
var smallGrowField = new BitField(0, { grow: 50 });
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);
smallGrowField.set(i, true);
if (i <= 55) {
t.equal(smallGrowField.buffer.length, (i >> 3) + 1, "should have grown for set()");
t.strictEqual(smallGrowField.get(i), true);
} else {
assert.equal(smallGrowField.buffer.length, oldLength, "should have grown for set()");
assert.strictEqual(smallGrowField.get(i), false);
t.equal(smallGrowField.buffer.length, oldLength, "should not have grown for set()");
t.strictEqual(smallGrowField.get(i), false, i + " bitfield " + smallGrowField.buffer.length);
}
}
}
var field2 = new BitField();
assert.ok(field2.buffer, "if no data or size passed in, should assume size 0");
t.end();
});
console.log("passed");
test("if no data or size passed in, should assume size 0", function(t){
var field2 = new BitField();
t.ok(field2.buffer);
t.end();
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc