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

bit-prefix-len

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bit-prefix-len - npm Package Compare versions

Comparing version 0.1.0 to 0.2.0

README.md

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()
})
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