Launch Week Day 5: Introducing Reachability for PHP.Learn More
Socket
Book a DemoSign in
Socket

bitfield

Package Overview
Dependencies
Maintainers
3
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bitfield - npm Package Compare versions

Comparing version
3.0.0
to
4.0.0
+54
lib/index.d.ts
interface BitFieldOptions {
/**
* If you `set` an index that is out-of-bounds, the bitfield
* will automatically grow so that the bitfield is big enough
* to contain the given index, up to the given size (in bit).
*
* If you want the Bitfield to grow indefinitely, pass `Infinity`.
*
* @default 0.
*/
grow?: number;
}
export default class BitField {
/**
* Grow the bitfield up to this number of entries.
* @default 0.
*/
private readonly grow;
/** The internal storage of the bitfield. */
buffer: Uint8Array;
/**
*
*
* @param data Either a number representing the maximum number of supported bytes, or a Uint8Array.
* @param opts Options for the bitfield.
*/
constructor(data?: number | Uint8Array, opts?: BitFieldOptions);
/**
* Get a particular bit.
*
* @param i Bit index to retrieve.
* @returns A boolean indicating whether the `i`th bit is set.
*/
get(i: number): boolean;
/**
* Set a particular bit.
*
* Will grow the underlying array if the bit is out of bounds and the `grow` option is set.
*
* @param i Bit index to set.
* @param value Value to set the bit to. Defaults to `true`.
*/
set(i: number, value?: boolean): void;
/**
* Loop through the bits in the bitfield.
*
* @param fn Function to be called with the bit value and index.
* @param start Index of the first bit to look at.
* @param end Index of the first bit that should no longer be considered.
*/
forEach(fn: (bit: boolean, index: number) => void, start?: number, end?: number): void;
}
export {};
//# sourceMappingURL=index.d.ts.map
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAMA,UAAU,eAAe;IACrB;;;;;;;;OAQG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,CAAC,OAAO,OAAO,QAAQ;IACzB;;;OAGG;IACH,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAS;IAC9B,4CAA4C;IACrC,MAAM,EAAE,UAAU,CAAC;IAE1B;;;;;OAKG;gBACS,IAAI,GAAE,MAAM,GAAG,UAAc,EAAE,IAAI,CAAC,EAAE,eAAe;IAOjE;;;;;OAKG;IACH,GAAG,CAAC,CAAC,EAAE,MAAM,GAAG,OAAO;IAKvB;;;;;;;OAOG;IACH,GAAG,CAAC,CAAC,EAAE,MAAM,EAAE,KAAK,UAAO,GAAG,IAAI;IAsBlC;;;;;;OAMG;IACH,OAAO,CACH,EAAE,EAAE,CAAC,GAAG,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,KAAK,IAAI,EACzC,KAAK,SAAI,EACT,GAAG,SAAyB,GAC7B,IAAI;CAWV"}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
function getByteSize(num) {
var out = num >> 3;
if (num % 8 !== 0)
out++;
return out;
}
var BitField = /** @class */ (function () {
/**
*
*
* @param data Either a number representing the maximum number of supported bytes, or a Uint8Array.
* @param opts Options for the bitfield.
*/
function BitField(data, opts) {
if (data === void 0) { data = 0; }
var grow = opts === null || opts === void 0 ? void 0 : opts.grow;
this.grow = (grow && isFinite(grow) && getByteSize(grow)) || grow || 0;
this.buffer =
typeof data === "number" ? new Uint8Array(getByteSize(data)) : data;
}
/**
* Get a particular bit.
*
* @param i Bit index to retrieve.
* @returns A boolean indicating whether the `i`th bit is set.
*/
BitField.prototype.get = function (i) {
var j = i >> 3;
return j < this.buffer.length && !!(this.buffer[j] & (128 >> i % 8));
};
/**
* Set a particular bit.
*
* Will grow the underlying array if the bit is out of bounds and the `grow` option is set.
*
* @param i Bit index to set.
* @param value Value to set the bit to. Defaults to `true`.
*/
BitField.prototype.set = function (i, value) {
if (value === void 0) { value = true; }
var j = i >> 3;
if (value) {
if (this.buffer.length < j + 1) {
var length_1 = Math.max(j + 1, Math.min(2 * this.buffer.length, this.grow));
if (length_1 <= this.grow) {
var newBuffer = new Uint8Array(length_1);
newBuffer.set(this.buffer);
this.buffer = newBuffer;
}
}
// Set
this.buffer[j] |= 128 >> i % 8;
}
else if (j < this.buffer.length) {
// Clear
this.buffer[j] &= ~(128 >> i % 8);
}
};
/**
* Loop through the bits in the bitfield.
*
* @param fn Function to be called with the bit value and index.
* @param start Index of the first bit to look at.
* @param end Index of the first bit that should no longer be considered.
*/
BitField.prototype.forEach = function (fn, start, end) {
if (start === void 0) { start = 0; }
if (end === void 0) { end = this.buffer.length * 8; }
for (var i = start, j = i >> 3, y = 128 >> i % 8, byte = this.buffer[j]; i < end; i++) {
fn(!!(byte & y), i);
y = y === 1 ? ((byte = this.buffer[++j]), 128) : y >> 1;
}
};
return BitField;
}());
exports.default = BitField;
+1
-1
The MIT License (MIT)
Copyright (c) 2013 Felix Böhm
Copyright (c) 2013-2020 Felix Böhm

@@ -5,0 +5,0 @@ Permission is hereby granted, free of charge, to any person obtaining a copy

{
"name": "bitfield",
"description": "a very simple bitfield implementation using buffers",
"version": "3.0.0",
"author": "Felix Boehm <me@feedic.com>",
"bugs": {
"url": "https://github.com/fb55/bitfield/issues"
},
"devDependencies": {
"standard": "^12.0.1",
"tape": "~4.11.0"
},
"engines": {
"node": ">=8"
},
"keywords": [
"bitfield",
"buffer"
],
"license": "MIT",
"main": "index.js",
"repository": {
"type": "git",
"url": "https://github.com/fb55/bitfield"
},
"scripts": {
"test": "standard && tape tests.js"
}
"name": "bitfield",
"description": "a simple bitfield, compliant with the BitTorrent spec",
"version": "4.0.0",
"author": "Felix Boehm <me@feedic.com>",
"funding": {
"url": "https://github.com/sponsors/fb55"
},
"sideEffects": false,
"main": "lib/index.js",
"types": "lib/index.d.ts",
"directories": {
"lib": "lib/"
},
"files": [
"lib/**/*"
],
"bugs": {
"url": "https://github.com/fb55/bitfield/issues"
},
"devDependencies": {
"@types/jest": "^26.0.0",
"@types/node": "^14.11.8",
"@typescript-eslint/eslint-plugin": "^4.4.1",
"@typescript-eslint/parser": "^4.4.1",
"coveralls": "*",
"eslint": "^7.11.0",
"eslint-config-prettier": "^6.0.0",
"eslint-plugin-node": "^11.1.0",
"jest": "^26.5.3",
"prettier": "^2.0.5",
"ts-jest": "^26.1.0",
"typescript": "^4.0.2"
},
"engines": {
"node": ">=8"
},
"keywords": [
"bitfield",
"buffer",
"bittorrent"
],
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/fb55/bitfield"
},
"scripts": {
"test": "jest --coverage && npm run lint",
"coverage": "cat coverage/lcov.info | coveralls",
"lint": "npm run lint:es && npm run lint:prettier",
"lint:es": "eslint .",
"lint:prettier": "npm run prettier -- --check",
"format": "npm run format:es && npm run format:prettier",
"format:es": "npm run lint:es -- --fix",
"format:prettier": "npm run prettier -- --write",
"prettier": "prettier '**/*.{js,ts,md,json,yml}'",
"build": "tsc",
"prepare": "npm run build"
},
"jest": {
"preset": "ts-jest",
"testEnvironment": "node"
},
"prettier": {
"tabWidth": 4
}
}
+96
-19
# bitfield
a very simple bitfield, compliant with the Bittorrent spec
A simple bitfield, compliant with the BitTorrent spec.

@@ -10,37 +10,114 @@ npm install bitfield

```js
var Bitfield = require("bitfield");
import Bitfield from "bitfield";
var field = new Bitfield(256); //create a bitfield with 256 bits
const field = new Bitfield(256); // Create a bitfield with 256 bits.
field.set(128); //set the 128th bit
field.set(128, true); //same as above
field.set(128); // Set the 128th bit.
field.set(128, true); // Same as above.
field.get(128); //true
field.get(200); //false (all values are initialised to `false`)
field.get(1e3); //false (out-of-bounds is also false)
field.get(128); // `true`
field.get(200); // `false` (all values are initialised to `false`)
field.get(1e3); // `false` (out-of-bounds is also false)
field.set(128, false); //set the 128th bit to 0 again
field.set(128, false); // Set the 128th bit to 0 again.
field.buffer; //the buffer used by bitfield
field.buffer; // The buffer used by the bitfield.
```
#### 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.
## Class: BitField
`Bitfield#get(index)`: Returns a boolean indicating whether the bit is set.
### Constructors
`Bitfield#set(index[, value])`: `value` defaults to true. Sets the bit to `1` for a value of `true` or `0` for `false`.
- [constructor](#constructor)
##### Auto-grow mode
`Bitfield(data, { grow: size })`: If you `set` an index that is out-of-bounds, the Bitfield will automatically grow so that the bitfield is big enough to contain the given index, up to the given `size` (in bit). If you want the Bitfield to grow indefinitely, pass `Infinity` as the size.
### Properties
- [buffer](#buffer)
#### Properties
### Methods
`Bitfield#buffer`: The contents of the bitfield.
- [forEach](#foreach)
- [get](#get)
- [set](#set)
`Bitfield#grow`: The passed growth option (defaults to `0`).
## Constructors
### constructor
\+ **new BitField**(`data?`: number \| Uint8Array, `opts?`: BitFieldOptions): `BitField`
#### Parameters:
| Name | Type | Default value | Description |
| ------- | -------------------- | ------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `data` | number \| Uint8Array | 0 | Either a number representing the maximum number of supported bytes, or a Uint8Array. |
| `opts?` | { grow: number } | { grow: 0 } | <p>**grow:**<p>If you `set` an index that is out-of-bounds, the bitfield will automatically grow so that the bitfield is big enough to contain the given index, up to the given size (in bit). <p>If you want the Bitfield to grow indefinitely, pass `Infinity`. |
**Returns:** `BitField`
## Properties
### buffer
• **buffer**: Uint8Array
The internal storage of the bitfield.
## Methods
### forEach
▸ **forEach**(`fn`: (bit: boolean, index: number) => void, `start?`: number, `end?`: number): void
Loop through the bits in the bitfield.
#### Parameters:
| Name | Type | Default value | Description |
| ------- | ------------------------------------- | ----------------------- | ----------------------------------------------------------- |
| `fn` | (bit: boolean, index: number) => void | - | Function to be called with the bit value and index. |
| `start` | number | 0 | Index of the first bit to look at. |
| `end` | number | this.buffer.length \* 8 | Index of the first bit that should no longer be considered. |
**Returns:** void
---
### get
▸ **get**(`i`: number): boolean
Get a particular bit.
#### Parameters:
| Name | Type | Description |
| ---- | ------ | ---------------------- |
| `i` | number | Bit index to retrieve. |
**Returns:** boolean
A boolean indicating whether the `i`th bit is set.
---
### set
▸ **set**(`i`: number, `value?`: boolean): void
Set a particular bit.
Will grow the underlying array if the bit is out of bounds and the `grow` option is set.
#### Parameters:
| Name | Type | Default value | Description |
| ------- | ------- | ------------- | -------------------------------------------- |
| `i` | number | - | Bit index to set. |
| `value` | boolean | true | Value to set the bit to. Defaults to `true`. |
**Returns:** void
## License
MIT
function getByteSize (num) {
let out = num >> 3
if (num % 8 !== 0) out++
return out
}
class BitField {
constructor (data = 0, opts) {
const grow = opts != null && opts.grow
this.grow = (grow && isFinite(grow) && getByteSize(grow)) || grow || 0
this.buffer = typeof data === 'number' ? new Uint8Array(getByteSize(data)) : data
}
get (i) {
const j = i >> 3
return (j < this.buffer.length) &&
!!(this.buffer[j] & (128 >> (i % 8)))
}
set (i, b = true) {
const j = i >> 3
if (b) {
if (this.buffer.length < j + 1) {
const length = Math.max(j + 1, Math.min(2 * this.buffer.length, this.grow))
if (length <= this.grow) {
const newBuffer = new Uint8Array(length)
newBuffer.set(this.buffer)
this.buffer = newBuffer
}
}
// Set
this.buffer[j] |= 128 >> (i % 8)
} else if (j < this.buffer.length) {
// Clear
this.buffer[j] &= ~(128 >> (i % 8))
}
}
}
if (typeof module !== 'undefined') module.exports = BitField