Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@digitalbazaar/bitstring

digitalbazaar42.1kBSD-3-Clause3.1.0TypeScript support: definitely-typed

Bitstring module for universal JavaScript

bitstring

readme

Bitstring

A Bitstring module for universal JavaScript.

Install

To install get the latest version from NPM:

npm install @digitalbazaar/bitstring

To install locally for development:

git clone https://github.com/digitalbazaar/bitstring.git
cd bitstring
npm install

Usage

import {Bitstring} from '@digitalbazaar/bitstring';

// You can make a new instance by passing in a length
const bitstring = new Bitstring({length: 10});

// OR by passing in a buffer
const buffer = Uint8Array.from([255]);
const bitstring = new Bitstring({buffer});

API

set(position, on)

Sets the value of a bit for the given position to the given boolean.

bitstring.set(1, true);

get(position)

Gets the value of a bit for the given position.

bitstring.get(3);

encodeBits()

Compresses and encodes bits to a base64url encoded string.

await bitstring.encodeBits();

decodeBits({encoded})

Decodes and uncompresses a given base64url encoded string to a Uint8Array.

await Bitstring.decodeBits({encoded});

compressBits()

Compresses bits to a Uint8Array.

await bitstring.compressBits();

uncompressBits({compressed})

Uncompresses compressed bits.

await Bitstring.uncompressBits({compressed});

Contribute

Please follow the existing code style.

PRs accepted.

If editing the Readme, please conform to the standard-readme specification.

Commercial Support

Commercial support for this library is available upon request from Digital Bazaar: support@digitalbazaar.com

License

BSD-3-Clause © Digital Bazaar

changelog

bitstring ChangeLog

3.1.0 - 2023-01-08

Changed

  • Rename littleEndianBits to leftToRightIndexing to try and avoid confusion over indexing order. There are two (opposite!) interpretations of the meaning of "big" or "little" endianness for bits (as opposed to bytes where it is more clear). Therefore, that terminology is to be avoided. The old parameter name can still be used, but is deprecated.

3.0.0 - 2023-01-08

Added

  • Add option littleEndianBits to use right to left bit order within a byte of the bitstring. This option provides backwards compatibility with previous versions. This name has led to confusion and will be renamed in the next minor release w/backwards-compatibility support for using it.

Changed

  • BREAKING: Default to left to right bit indexing within a byte of the bitstring. Previous versions would use right to left bit order with big-endian byte order (least significant byte index is at the most significant address, i.e., first). This version will default to being consistently left to right with indexing (largest and leftmost address is the lowest index for both bits and bytes).

Removed

  • BREAKING: Remove support for node 14, only node 16+ supported.

2.0.0 - 2022-06-02

Changed

  • BREAKING: Remove default export.
    • To update: Import named Bitstring class directly.
  • BREAKING: Convert to module (ESM).
  • BREAKING: Require Node.js >=14.
  • Update dependencies.
  • Lint module.

1.2.1 - 2022-01-21

Added

  • Add README.md.

1.2.0 - 2020-09-25

Added

  • Add compressBits and uncompressBits helper functions. These functions perform gzip and ungzip operations without doing base64url encoding/decoding.

1.1.0 - 2020-07-10

Changed

  • Improve parameter validation and error handling on all APIs.

1.0.0 - 2020-07-09

Added

  • Add core files.

  • See git history for changes previous to this release.