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

bitfield

Package Overview
Dependencies
Maintainers
3
Versions
16
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

lib/index.d.ts

93

package.json
{
"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
}
}
# 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

Sorry, the diff of this file is not supported yet

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