npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

bit-consumer

v1.0.1

Published

An ES module providing a "bit consumer" class allowing you to feed it endless of bits and receive unsigned integers back from them whenever the wanted amount of bits has been consumed. The bits are consumed in the order of the most significant bits first.

Downloads

8

Readme

bit-consumer

Description

An ES module providing a "bit consumer" class allowing you to feed it endless of bits and receive unsigned integers back from them whenever the wanted amount of bits has been consumed. The bits are consumed in the order of the most significant bits first.

Example

import {BitConsumer} from 'bit-consumer'

let bc = new BitConsumer(16)
bc.onIntegerReady = integer => console.log(integer.toString(16))
bc.consumeBitsFromInteger(0xDE, 8)
bc.consumeBitsFromInteger(0xAD, 8)
bc.consumeBitsFromInteger(0xBEEF, 16)
bc.consumeBitsFromInteger(0xFF, 8)
console.log(bc.integerFromRemains().toString(16))

bc = new BitConsumer(8, integer => console.log(integer.toString(2)))
bc.consumeBits(1,1,0,0, 0,0,0,1, 0,1,0)
console.log(bc.integerFromRemains().toString(2))

Console output from example:

dead
beef
ff00
11000001
1000000

Supported platforms

How to use

Install using NPM

npm i bit-consumer

Import the ES module into Node.js

import {BitConsumer} from 'bit-consumer'

Got problems using ES modules? Click here or read this.

Import the ES module into the browser or Deno

import {BitConsumer} from '/node_modules/bit-consumer/source/bitConsumer.js'

Cumbersome? Consider using import maps so you can import it just like in Node.js. Also see the Deno specific documentation for import maps if using Deno.

Funding

If you find this useful then please consider helping me out (I'm jobless and sick). For more information visit my GitHub sponsors page, my profile or my simple website.

Auto-generated API documentation (from JSDoc)

bit-consumer.BitConsumer

A "bit consumer" class allowing you to feed it endless of bits and receive unsigned integers back from them whenever the wanted amount of bits has been consumed. The bits are consumed in the order of the most significant bits first.

Kind: static class of bit-consumer

new exports.BitConsumer(bitsWanted, [onIntegerReady])

Params

  • bitsWanted number - Each time this many bits has been consumed the onIntegerReady callback is called with an integer created from those bits.
  • [onIntegerReady] function - The callback function to receive an integer each time the amount of wanted bits has been consumed. If more than 32 bits are wanted then it will receive a BigInt. It's optional because it can be set later.

bitConsumer.onIntegerReady

Set to a callback function to receive an integer each time the amount of wanted bits has been consumed. If more than 32 bits are wanted then it will receive a BigInt.

Kind: instance property of BitConsumer

bitConsumer.integerFromRemains() ⇒ Number | BigInt

If the BitConsumer has consumed more bits since the last call to the onIntegerReady callback and you want to get an integer from them before the count of bits has reached bitsWanted then call this function. It returns null if no more bits were consumed. This is usually used to flush any remaining bits at the end of consumption.

Kind: instance method of BitConsumer

bitConsumer.consumeBitsFromInteger(integer, bitsToConsume)

The bits must come from somewhere and consuming them from an integer (up to 32 bits) is the fastest way to do it. If you need more bits you can consume them from a BigInt. If the integer has more bits than the amount you want to consume then it will consume the least significant bits of that integer.

Kind: instance method of BitConsumer
Params

  • integer number | bigint - The Number or BigInt to consume bits from.
  • bitsToConsume number - The amount of bits to consume.

bitConsumer.consumeBit(bit)

If you want to consume just one single lonely bit then this is the command for that. For higher performance consider using consumeBitsFromInteger to consume several bits at once.

Kind: instance method of BitConsumer
Params

  • bit number

bitConsumer.consumeBits(...bits)

Allows you to consume several bits. For higher performance consider using consumeBitsFromInteger if possible.

Kind: instance method of BitConsumer
Params

  • ...bits number

End of readme

To get back up click here (only works on GitHub?) or find your own way.