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

bit-array

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bit-array - npm Package Compare versions

Comparing version 0.2.1 to 0.2.2

26

lib/bit-array.js

@@ -12,15 +12,25 @@ /**

*/
var BitArray = function (values) {
if (values && values.toString() === values) {
this.values = [];
var hex = values.slice(/^0x/.exec(values) ? 2 : 0);
while (hex.length % 8 !== 0) {
hex = '0' + hex;
var BitArray = function (size, hex) {
this.values = [];
if (hex) {
hex = hex.slice(/^0x/.exec(hex) ? 2 : 0);
if (hex.length * 4 > this.length) {
throw 'Hex value is too large for this bit array.'
} else if (hex.length * 4 < this.length) {
// pad it
while(hex.length * 4 < this.length) {
hex = '0' + hex;
}
}
for (var i = 0; i < (hex.length / 8); i++) {
var slice = hex.slice(i * 8, i * 8 + 8);
this.values.push(parseInt(slice, 16));
this.values[i] = parseInt(slice, 16);
}
} else {
this.values = Array((values || 32) / 32);
for (var i = 0; i < Math.ceil(size / 32); i += 1) {
this.values[i] = 0;
}
}

@@ -27,0 +37,0 @@ };

@@ -12,13 +12,19 @@ /**

*/
var BitArray = function (values) {
if (typeof values === 'string') {
var hex = values.slice(/^0x/.exec(values) ? 2 : 0);
while (hex.length % 8 !== 0) {
hex = '0' + hex;
var BitArray = function (size, hex) {
this.length = size;
this.buffer = new ArrayBuffer(Math.ceil(this.length / 32) * 4);
this.wordArray = new Uint32Array(this.buffer);
if (hex) {
hex = hex.slice(/^0x/.exec(hex) ? 2 : 0);
if (hex.length * 4 > this.length) {
throw 'Hex value is too large for this bit array.'
} else if (hex.length * 4 < this.length) {
// pad it
while(hex.length * 4 < this.length) {
hex = '0' + hex;
}
}
this.length = hex.length * 4;
this.buffer = new ArrayBuffer(Math.ceil(this.length / 32) * 4);
this.wordArray = new Uint32Array(this.buffer);
for (var i = 0; i < (hex.length / 8); i++) {

@@ -28,6 +34,2 @@ var slice = hex.slice(i * 8, i * 8 + 8);

}
} else {
this.length = values;
this.buffer = new ArrayBuffer(Math.ceil(this.length / 32) * 4);
this.wordArray = new Uint32Array(this.buffer);
}

@@ -150,3 +152,4 @@ };

for (var i = 0; i < this.wordArray.length; i += 1) {
result.push(this.wordArray[i].toString(16));
//result.push(this.wordArray[i].toString(16));
result.push(('00000000' + (this.wordArray[i] >>> 0).toString(16)).slice(-8));
}

@@ -153,0 +156,0 @@ return result.join('');

{
"name": "bit-array",
"description": "JavaScript implementation of bit arrays",
"version": "0.2.1",
"version": "0.2.2",
"author": "Bram Stein <b.l.stein@gmail.com> (http://www.bramstein.com)",

@@ -6,0 +6,0 @@ "repository": {

@@ -21,6 +21,5 @@ ## JavaScript Bit Array Library

Note that the array internally uses 32 bit integers (actually, JavaScript's number type is 64 bit, but only 32 bits can be addressed using bitwise operations.)
Note that the array internally uses 32 bit integers (actually, JavaScript's number type is 64 bit, but only 32 bits can be addressed using bitwise operations,) so going beyond the given length throws an error:
a.set(32, true);
a.toString(); // "1000000000000000000000000000000110000000000000000000000000000000"
a.set(32, true); // throws an index of range exception

@@ -34,7 +33,7 @@ Even though bit arrays are not that useful in JavaScript, there is one place where they excel; encoding large boolean sets for transfer between the browser and server. A JSON representation of a bit array is much smaller than an actual boolean array.

<dl>
<dt>BitArray(length)</dt>
<dd>Creates a new empty bit array with the given length.</dd>
<dt>BitArray(size)</dt>
<dd>Creates a new empty bit array with the given size in bits.</dd>
<dt>BitArray(string)</dt>
<dd>Creates a new bit array using the hex values in the string</dd>
<dt>BitArray(size, hex)</dt>
<dd>Creates a new bit array with the given size and using the hex string as value</dd>
</dl>

@@ -41,0 +40,0 @@

@@ -10,16 +10,34 @@ var assert = require('assert'),

exports.testHexConstructor = function () {
var binaryDeadBeef = '11011110101011011011111011101111';
var b = new BitArray('deadbeef');
var b = new BitArray(32, 'deadbeef');
// Note that the "logical order" returned by the BitArray type is reversed
// from the "mathematical order" used to convert between representations.
assert.equal(b.toString().split('').reverse().join(''), binaryDeadBeef, 'BitArray(0x...)');
assert.equal(b.toBinaryString(), binaryDeadBeef, 'BitArray(0x...)');
assert.equal(b.toBinaryString(), '11011110101011011011111011101111', 'BitArray(0x...)');
assert.equal(b.toHexString(), 'deadbeef', 'BitArray(0x...)');
var c = new BitArray('c0ffeec0ffee').and(new BitArray('ffffffffff000000'));
var c = new BitArray(64, '0000c0ffeec0ffee').and(new BitArray(64, 'ffffffffff000000'));
assert.equal(c.toHexString(), 'c0ffee000000', 'BitArray(0x...)');
assert.equal(c.toHexString(), '0000c0ffee000000', 'BitArray(0x...)');
};
exports.testHexConstructorPadding = function () {
var b = new BitArray(32, 'f');
assert.equal(b.toHexString(), '0000000f');
assert.equal(b.toBinaryString(), '00000000000000000000000000001111');
};
exports.testToHexString = function () {
var b = new BitArray(32);
b.set(5, true);
b.set(6, true);
assert.equal(b.toHexString(), '00000060');
assert.equal(b.toBinaryString(), '00000000000000000000000001100000');
var c = new BitArray(32, b.toHexString());
assert.equal(c.toHexString(), '00000060');
assert.equal(c.toBinaryString(), '00000000000000000000000001100000');
};
exports.testSimpleSet = function () {

@@ -26,0 +44,0 @@ var b = new BitArray(32);

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