bit-prefix-len
Advanced tools
Comparing version 0.1.0 to 0.2.0
10
index.js
@@ -5,6 +5,12 @@ module.exports = bitPrefixLen | ||
// e.g. bitPrefixLen(new Buffer(00001010100), 0) == 4 | ||
function bitPrefixLen(buf, bit) { | ||
function bitPrefixLen(bit, buf) { | ||
if (!(buf instanceof Buffer)) | ||
throw new Error('buf must be a buffer') | ||
if (bit !== 0 && bit !== 1) | ||
throw new Error('bit must be 1 or 0') | ||
for (var i = 0; i < buf.length; i++) { | ||
for (var j = 0; j < 8; j++) { | ||
if ((buf[i] >> (7 - j)) & 0x1 != 0) { | ||
if (((buf[i] >> (7 - j)) & 0x1) != bit) { | ||
return i * 8 + j; | ||
@@ -11,0 +17,0 @@ } |
{ | ||
"name": "bit-prefix-len", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "return length of a prefix in a buffer", | ||
@@ -26,4 +26,3 @@ "main": "index.js", | ||
}, | ||
"dependencies": { | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
@@ -30,0 +29,0 @@ "tape": "~2.13.2" |
24
test.js
var test = require('tape') | ||
bitPrefixLen = require('./index') | ||
test('basic', function(t) { | ||
t.equal(bitPrefixLen(new Buffer('0000abc00abc', 'hex')), 4 * 4) | ||
t.equal(bitPrefixLen(new Buffer('000000000000', 'hex')), 12 * 4) | ||
t.equal(bitPrefixLen(new Buffer('000000000001', 'hex')), 12 * 4 - 1) | ||
t.equal(bitPrefixLen(new Buffer([4, 12, 12])), 5) | ||
test('0', function(t) { | ||
t.equal(bitPrefixLen(0, new Buffer('0000abc00abc', 'hex')), 4 * 4) | ||
t.equal(bitPrefixLen(0, new Buffer('000000000000', 'hex')), 12 * 4) | ||
t.equal(bitPrefixLen(0, new Buffer('ffffffffffff', 'hex')), 0) | ||
t.equal(bitPrefixLen(0, new Buffer('800000000000', 'hex')), 0) | ||
t.equal(bitPrefixLen(0, new Buffer('000000000001', 'hex')), 12 * 4 - 1) | ||
t.equal(bitPrefixLen(0, new Buffer([4, 12, 12])), 5) | ||
t.end() | ||
}) | ||
test('1', function(t) { | ||
t.equal(bitPrefixLen(1, new Buffer('ffff00c00abc', 'hex')), 4 * 4) | ||
t.equal(bitPrefixLen(1, new Buffer('ffffffffffff', 'hex')), 12 * 4) | ||
t.equal(bitPrefixLen(1, new Buffer('000000000000', 'hex')), 0) | ||
t.equal(bitPrefixLen(1, new Buffer('800000000000', 'hex')), 1) | ||
t.equal(bitPrefixLen(1, new Buffer('000000000001', 'hex')), 0) | ||
t.equal(bitPrefixLen(1, new Buffer('fffffffffffe', 'hex')), 12 * 4 - 1) | ||
t.equal(bitPrefixLen(1, new Buffer('11111111111e', 'hex')), 0) | ||
t.equal(bitPrefixLen(1, new Buffer([255, 128, 12, 12])), 9) | ||
t.end() | ||
}) |
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 README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
2915
5
45
1
16