Simple basic bits array for JavaScript.
⚠️ Starting from version 2 all strings are encoded and decoded as UTF-16 (2 bytes per character).
Install via npm:
npm install --save @zekfad/bitbytearray
Install via yarn:
yarn add @zekfad/bitbytearray
Simple array of bits:
const BitByteArray = require('./BitByteArray.js');
const myBitsArray = new BitByteArray(32);
console.log([ ...myBitsArray, ]);
/*
[
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
]
*/
myBitsArray.assign([
0, 1, 0, 0, 1, 0, 0, 0,
]);
myBitsArray.assign(
[
0, 1, 1, 0, 1, 0, 0, 1,
],
16,
);
console.log(myBitsArray.toString()); // Hi
Manipulate strings:
const BitByteArray = require('./BitByteArray.js');
const utf8string = 'UTF-8 Строка';
const myBitsArray = BitByteArray.from(utf8string);
console.log([ ...myBitsArray, ]);
/*
[
0, 1, 0, 1, 0, 1, 0, 1,
0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 0, 1, 0, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 0, 0, 0, 1, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 0, 1, 1, 0, 1,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 1, 1, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 0,
... 92 more items
]
*/
console.log(utf8string === myBitsArray.toString()); // true
console.log(myBitsArray.toString()); // UTF-8 Строка
// Change dash to space
myBitsArray.assign(
[
0, 0, 1, 0, 0, 0, 0, 0,
],
3 * 8 * 2 // Skip 3 UTF-16 characters (8 * 2 bits)
)
console.log(myBitsArray.toString()); // UTF 8 Строка
Array-like access by index:
const BitByteArray = require('./BitByteArray.js');
const myBitsArray = BitByteArray.from(2);
console.log([...myBitsArray, ]);
/*
[
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 0
]
*/
myBitsArray[29] = 1;
console.log([ ...myBitsArray, ]);
/*
[
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 1, 0
]
*/
console.log(
parseInt(
[ ...myBitsArray, ]
.join(''),
2
)
); // 6
This module doesn't implement all methods of normal Array
, you should use spread operator ([ ...myBitsArray, ]
), if you need to work with bits in array-like way, and then use BitByteArray.from(BitByteArray.projectBitsArray(myNormalArray))
.
Get class representing an array of bits with index assign checks.
length
(number
) - Bits array length.
Returns BitByteArray
Create an array of bits from existing data.
source
(number
|number[]
|boolean
|boolean[]
|string
) - Source data.
Returns BitByteArray
- Created BitByteArray.
Create an array of ones and zeros from existing data.
source
(Array
) - Source data.
Returns Array
- Normalized array, ready to use with .from()
.
Create an array of bits.
length
(number
) - Bits array length.
Get/set array of bits' length.
Change array of bits' length.
length
(number
) - Bits array length.
Returns number
- Bits array length.
Change bit on a given offset.
offset
(number
) - Bit offset.bit
(number
|boolean
) - Bit.
Returns number
- Bit.
Get bit on a given offset.
offset
(number
) - Bit offset.
Returns number
- Bit.
Get local storage of bytes as an array of numbers.
Returns number[]
- Bytes array.
Get local storage of bytes as a string.
encoding
(string
) - Encoding.
Returns string
- String representation of bits array.
Assign array of bits to an instance.
bits
(number[]
|boolean[]
) - Bits array.offset
(number
) - Assign offset.
Returns boolean
- Returns true if no errors found.
Fill local storage of bits with a provided bit.
bit
(number
|boolean
) - Bit.
Returns BitByteArray
- Returns instance if no errors found.
Generate bits sequence.
Yields number
- Next number in the sequence.
Access a bit by index in array-like way (in read-write mode).
Returns number
- Bit.
Check if given offset is acceptable.
- offset (
number
) - Bit offset.
Returns boolean
- Returns true if given argument is acceptable.
Change array of bits' count of bytes.
length
(number
) - Count of bytes.
Returns number
- Count of bytes.