bitfield
Version:
a simple bitfield, compliant with the BitTorrent spec
63 lines • 2.33 kB
TypeScript
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;
}
/** Bit-level set/get utility backed by a growable `Uint8Array`. */
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;
/** The number of bits in the bitfield. */
get length(): number;
/**
* Constructs a BitField.
* @param data Either a number representing the maximum number of supported bits, or a Uint8Array.
* @param options Configuration for bitfield growth behavior.
*/
constructor(data?: number | Uint8Array, options?: BitFieldOptions);
/**
* Get a particular bit.
* @param bitIndex Bit index to retrieve.
* @returns A boolean indicating whether the `i`th bit is set.
*/
get(bitIndex: 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 bitIndex Bit index to set.
* @param value Value to set the bit to. Defaults to `true`.
*/
set(bitIndex: number, value?: boolean): void;
/**
* Sets a value or an array of values.
* @param array An array of booleans to set.
* @param offset The bit offset at which the values are to be written.
*/
setAll(array: ArrayLike<boolean>, offset?: number): void;
/**
* Loop through the bits in the bitfield.
* @param callbackfn 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(callbackfn: (bit: boolean, index: number) => void, start?: number, end?: number): void;
/**
* Check if all bits in the Bitfield are unset.
* @returns A boolean indicating whether all bits are unset.
*/
isEmpty(): boolean;
}
export {};
//# sourceMappingURL=index.d.ts.map