Comparing version 1.1.2 to 2.0.0
103
index.js
@@ -1,61 +0,64 @@ | ||
var Container = typeof Buffer !== "undefined" ? Buffer //in node, use buffers | ||
: typeof Int8Array !== "undefined" ? Int8Array //in newer browsers, use webgl int8arrays | ||
: function(l){ var a = new Array(l); for(var i = 0; i < l; i++) a[i]=0; }; //else, do something similar | ||
var Container = typeof Buffer !== 'undefined' | ||
? Buffer // in node, use buffers | ||
: Uint8Array // in browsers, use typed arrays | ||
function BitField(data, opts){ | ||
if(!(this instanceof BitField)) { | ||
return new BitField(data, opts); | ||
} | ||
function BitField (data, opts) { | ||
if (!(this instanceof BitField)) { | ||
return new BitField(data, opts) | ||
} | ||
if(arguments.length === 0){ | ||
data = 0; | ||
} | ||
if (arguments.length === 0) { | ||
data = 0 | ||
} | ||
this.grow = opts && (isFinite(opts.grow) && getByteSize(opts.grow) || opts.grow) || 0; | ||
this.grow = (opts && ((isFinite(opts.grow) && getByteSize(opts.grow)) || opts.grow)) || 0 | ||
if(typeof data === "number" || data === undefined){ | ||
data = new Container(getByteSize(data)); | ||
if(data.fill && !data._isBuffer) data.fill(0); // clear node buffers of garbage | ||
} | ||
this.buffer = data; | ||
if (typeof data === 'number' || data === undefined) { | ||
data = new Container(getByteSize(data)) // eslint-disable-line node/no-deprecated-api | ||
if (data.fill && !data._isBuffer) data.fill(0) // clear node buffers of garbage | ||
} | ||
this.buffer = data | ||
} | ||
function getByteSize(num){ | ||
var out = num >> 3; | ||
if(num % 8 !== 0) out++; | ||
return out; | ||
function getByteSize (num) { | ||
var out = num >> 3 | ||
if (num % 8 !== 0) out++ | ||
return out | ||
} | ||
BitField.prototype.get = function(i){ | ||
var j = i >> 3; | ||
return (j < this.buffer.length) && | ||
!!(this.buffer[j] & (128 >> (i % 8))); | ||
}; | ||
BitField.prototype.get = function (i) { | ||
var j = i >> 3 | ||
return (j < this.buffer.length) && | ||
!!(this.buffer[j] & (128 >> (i % 8))) | ||
} | ||
BitField.prototype.set = function(i, b){ | ||
var j = i >> 3; | ||
if (b || arguments.length === 1){ | ||
if (this.buffer.length < j + 1) this._grow(Math.max(j + 1, Math.min(2 * this.buffer.length, this.grow))); | ||
// Set | ||
this.buffer[j] |= 128 >> (i % 8); | ||
} else if (j < this.buffer.length) { | ||
/// Clear | ||
this.buffer[j] &= ~(128 >> (i % 8)); | ||
} | ||
}; | ||
BitField.prototype.set = function (i, b) { | ||
var j = i >> 3 | ||
if (b || arguments.length === 1) { | ||
if (this.buffer.length < j + 1) { | ||
this._grow(Math.max(j + 1, Math.min(2 * this.buffer.length, this.grow))) | ||
} | ||
// Set | ||
this.buffer[j] |= 128 >> (i % 8) | ||
} else if (j < this.buffer.length) { | ||
// Clear | ||
this.buffer[j] &= ~(128 >> (i % 8)) | ||
} | ||
} | ||
BitField.prototype._grow = function(length) { | ||
if (this.buffer.length < length && length <= this.grow) { | ||
var newBuffer = new Container(length); | ||
if (newBuffer.fill) newBuffer.fill(0); | ||
if (this.buffer.copy) this.buffer.copy(newBuffer, 0); | ||
else { | ||
for(var i = 0; i < this.buffer.length; i++) { | ||
newBuffer[i] = this.buffer[i]; | ||
} | ||
} | ||
this.buffer = newBuffer; | ||
} | ||
}; | ||
BitField.prototype._grow = function (length) { | ||
if (this.buffer.length < length && length <= this.grow) { | ||
var newBuffer = new Container(length) // eslint-disable-line node/no-deprecated-api | ||
if (newBuffer.fill) newBuffer.fill(0) | ||
if (this.buffer.copy) { | ||
this.buffer.copy(newBuffer, 0) | ||
} else { | ||
for (var i = 0; i < this.buffer.length; i++) { | ||
newBuffer[i] = this.buffer[i] | ||
} | ||
} | ||
this.buffer = newBuffer | ||
} | ||
} | ||
if(typeof module !== "undefined") module.exports = BitField; | ||
if (typeof module !== 'undefined') module.exports = BitField |
{ | ||
"name": "bitfield", | ||
"version": "1.1.2", | ||
"description": "a very simple bitfield implementation using buffers", | ||
"main": "index.js", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/fb55/bitfield" | ||
"version": "2.0.0", | ||
"author": "Felix Boehm <me@feedic.com>", | ||
"bugs": { | ||
"url": "https://github.com/fb55/bitfield/issues" | ||
}, | ||
"scripts": { | ||
"test": "tape tests.js" | ||
"devDependencies": { | ||
"standard": "^11.0.0", | ||
"tape": "~4.9.0" | ||
}, | ||
@@ -17,18 +17,11 @@ "keywords": [ | ||
], | ||
"devDependencies": { | ||
"tape": "2.12" | ||
"license": "MIT", | ||
"main": "index.js", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/fb55/bitfield" | ||
}, | ||
"testling": { | ||
"files": "tests.js", | ||
"browsers": [ | ||
"ie/9..latest", | ||
"chrome/25..latest", | ||
"firefox/20..latest", | ||
"safari/6..latest", | ||
"opera/15.0..latest" | ||
] | ||
}, | ||
"author": "Felix Boehm <me@feedic.com>", | ||
"license": "MIT", | ||
"readmeFilename": "readme.md" | ||
"scripts": { | ||
"test": "standard && tape tests.js" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
#bitfield | ||
# bitfield | ||
@@ -7,6 +7,4 @@ a very simple bitfield, compliant with the Bittorrent spec | ||
[![browser support](https://ci.testling.com/fb55/bitfield.png)](https://ci.testling.com/fb55/bitfield) | ||
#### Example | ||
####Example | ||
```js | ||
@@ -29,3 +27,3 @@ var Bitfield = require("bitfield"); | ||
####Methods | ||
#### Methods | ||
`Bitfield(data)`: `data` can be either a node.js buffer, WebGL Int8Array or numeric array, or a number representing the maximum number of supported bytes. | ||
@@ -41,3 +39,4 @@ | ||
####Properties | ||
#### Properties | ||
`Bitfield#buffer`: The contents of the bitfield. | ||
@@ -47,4 +46,4 @@ | ||
##License | ||
## License | ||
MIT |
190
tests.js
@@ -1,119 +0,119 @@ | ||
var test = require("tape"), | ||
BitField = require("./"); | ||
var test = require('tape') | ||
var BitField = require('./') | ||
var data = "011011100110111".split("").map(Number).map(Boolean), | ||
field = new BitField(data.length); | ||
var data = '011011100110111'.split('').map(Number).map(Boolean) | ||
var field = new BitField(data.length) | ||
test("bitfield should be empty when initialized", function(t){ | ||
for(var i = 0; i < data.length; i++){ | ||
t.strictEqual(field.get(i), false); | ||
} | ||
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(); | ||
}); | ||
t.end() | ||
}) | ||
//Write data | ||
// Write data | ||
test("should reproduce written data", function(t){ | ||
for(var i = 0; i < data.length; i++){ | ||
field.set(i, data[i]); | ||
} | ||
test('should reproduce written data', function (t) { | ||
var i | ||
for (i = 0; i < data.length; i++) { | ||
field.set(i, data[i]) | ||
} | ||
for(var i = 0; i < data.length; i++){ | ||
t.strictEqual(field.get(i), data[i]); | ||
} | ||
for (i = 0; i < data.length; i++) { | ||
t.strictEqual(field.get(i), data[i]) | ||
} | ||
t.end(); | ||
}); | ||
t.end() | ||
}) | ||
test('out-of-bounds should simply be false', function (t) { | ||
for (var i = data.length; i < 1e3; i++) { | ||
t.strictEqual(field.get(i), false) | ||
} | ||
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() | ||
}) | ||
t.end(); | ||
}); | ||
test('should not grow by default', function (t) { | ||
var index = 25 | ||
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()) | ||
for(var i = 0; i < 100; i++) { | ||
index += 8 + Math.floor(32 * Math.random()); | ||
var oldLength = field.buffer.length | ||
t.strictEqual(field.get(index), false) | ||
t.equal(field.buffer.length, oldLength, 'should not have grown for get()') | ||
field.set(index, true) | ||
var oldLength = field.buffer.length; | ||
t.strictEqual(field.get(index), false); | ||
t.equal(field.buffer.length, oldLength, "should not have grown for get()"); | ||
field.set(index, true); | ||
t.equal(field.buffer.length, oldLength, 'should not have grown for set()') | ||
t.strictEqual(field.get(index), false) | ||
} | ||
t.equal(field.buffer.length, oldLength, "should not have grown for set()"); | ||
t.strictEqual(field.get(index), false); | ||
} | ||
t.end() | ||
}) | ||
t.end(); | ||
}); | ||
test('should be able to grow to infinity', function (t) { | ||
var growField = new BitField(data.length, { grow: Infinity }) | ||
var index = 25 | ||
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()) | ||
for(var i = 0; i < 100; i++) { | ||
index += 8 + Math.floor(32 * Math.random()); | ||
var oldLength = growField.buffer.length | ||
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) | ||
t.ok(growField.buffer.length >= newLength, 'should have grown for set()') | ||
t.strictEqual(growField.get(index), true) | ||
} | ||
var oldLength = growField.buffer.length; | ||
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); | ||
t.ok(growField.buffer.length >= newLength, "should have grown for set()"); | ||
t.strictEqual(growField.get(index), true); | ||
} | ||
t.end() | ||
}) | ||
t.end(); | ||
}); | ||
test('should restrict growth to growth option', function (t) { | ||
var smallGrowField = new BitField(0, { grow: 50 }) | ||
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(i, true) | ||
if (i <= 55) { | ||
t.ok(smallGrowField.buffer.length >= (i >> 3) + 1, 'should have grown for set()') | ||
t.strictEqual(smallGrowField.get(i), true) | ||
} else { | ||
t.equal(smallGrowField.buffer.length, oldLength, 'should not have grown for set()') | ||
t.strictEqual(smallGrowField.get(i), false, i + ' bitfield ' + smallGrowField.buffer.length) | ||
} | ||
} | ||
for(var i = 0; i < 100; i++) { | ||
var oldLength = smallGrowField.buffer.length; | ||
smallGrowField.set(i, true); | ||
if (i <= 55) { | ||
t.ok(smallGrowField.buffer.length >= (i >> 3) + 1, "should have grown for set()"); | ||
t.strictEqual(smallGrowField.get(i), true); | ||
} else { | ||
t.equal(smallGrowField.buffer.length, oldLength, "should not have grown for set()"); | ||
t.strictEqual(smallGrowField.get(i), false, i + " bitfield " + smallGrowField.buffer.length); | ||
} | ||
} | ||
t.end() | ||
}) | ||
t.end(); | ||
}); | ||
test('if no data or size passed in, should assume size 0', function (t) { | ||
var field2 = new BitField() | ||
t.ok(field2.buffer) | ||
test("if no data or size passed in, should assume size 0", function(t){ | ||
var field2 = new BitField(); | ||
t.ok(field2.buffer); | ||
t.end() | ||
}) | ||
t.end(); | ||
}); | ||
test("correct size bitfield", function(t){ | ||
t.equal(new BitField(1).buffer.length, 1); | ||
t.equal(new BitField(2).buffer.length, 1); | ||
t.equal(new BitField(3).buffer.length, 1); | ||
t.equal(new BitField(4).buffer.length, 1); | ||
t.equal(new BitField(5).buffer.length, 1); | ||
t.equal(new BitField(6).buffer.length, 1); | ||
t.equal(new BitField(7).buffer.length, 1); | ||
t.equal(new BitField(8).buffer.length, 1); | ||
t.equal(new BitField(9).buffer.length, 2); | ||
t.equal(new BitField(10).buffer.length, 2); | ||
t.equal(new BitField(11).buffer.length, 2); | ||
t.equal(new BitField(12).buffer.length, 2); | ||
t.equal(new BitField(13).buffer.length, 2); | ||
t.equal(new BitField(14).buffer.length, 2); | ||
t.equal(new BitField(15).buffer.length, 2); | ||
t.equal(new BitField(16).buffer.length, 2); | ||
t.equal(new BitField(17).buffer.length, 3); | ||
t.end(); | ||
}); | ||
test('correct size bitfield', function (t) { | ||
t.equal(new BitField(1).buffer.length, 1) | ||
t.equal(new BitField(2).buffer.length, 1) | ||
t.equal(new BitField(3).buffer.length, 1) | ||
t.equal(new BitField(4).buffer.length, 1) | ||
t.equal(new BitField(5).buffer.length, 1) | ||
t.equal(new BitField(6).buffer.length, 1) | ||
t.equal(new BitField(7).buffer.length, 1) | ||
t.equal(new BitField(8).buffer.length, 1) | ||
t.equal(new BitField(9).buffer.length, 2) | ||
t.equal(new BitField(10).buffer.length, 2) | ||
t.equal(new BitField(11).buffer.length, 2) | ||
t.equal(new BitField(12).buffer.length, 2) | ||
t.equal(new BitField(13).buffer.length, 2) | ||
t.equal(new BitField(14).buffer.length, 2) | ||
t.equal(new BitField(15).buffer.length, 2) | ||
t.equal(new BitField(16).buffer.length, 2) | ||
t.equal(new BitField(17).buffer.length, 3) | ||
t.end() | ||
}) |
Sorry, the diff of this file is not supported yet
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
150
0
8266
2
46