fix: multi-repo local dev (file:../ deps, package exports, missing deps)
This commit is contained in:
+15
@@ -0,0 +1,15 @@
|
||||
ISC License
|
||||
|
||||
Copyright (c) 2022 Contributors
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||||
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
PERFORMANCE OF THIS SOFTWARE.
|
||||
+80
@@ -0,0 +1,80 @@
|
||||
# Bits to Bytes
|
||||
|
||||
Functions for doing bit manipulation of typed arrays.
|
||||
|
||||
```npm
|
||||
npm i bits-to-bytes
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```js
|
||||
const bits = require('bits-to-bytes')
|
||||
|
||||
const buffer = Uint8Array.from([0b11001000])
|
||||
|
||||
bits.get(buffer, 3)
|
||||
// true
|
||||
```
|
||||
|
||||
`buffer` may be any typed array with a `number` element type, including `Uint8Array`, `Uint16Array`, and `Uint32Array`.
|
||||
|
||||
## API
|
||||
|
||||
#### `const n = bits.byteLength(size)`
|
||||
|
||||
Get the number of bytes required to contain `size` bits.
|
||||
|
||||
#### `const b = bits.get(buffer, bit)`
|
||||
|
||||
Get the given bit, which will either be `true` (set) or `false` (unset).
|
||||
|
||||
#### `const changed = bits.set(buffer, bit[, value])`
|
||||
|
||||
Set the given bit to `value`, which defaults to `true`. Returns `true` if the bit changed, otherwise `false`.
|
||||
|
||||
#### `const changed = bits.setRange(buffer, start, end[, value])`
|
||||
|
||||
Set the given bit range to `value`, which defaults to `true`. Returns `true` if any of the bits changed, otherwise `false`.
|
||||
|
||||
If you don't need the additional information about whether any of the bits changed, consider `bits.fill()` as a more performant alternative.
|
||||
|
||||
#### `buffer = bits.fill(buffer, value[, start[, end]])`
|
||||
|
||||
Fill the given bit range with `value`. `start` defaults to `0` and `end` defaults to the bit length of the array. Returns the modified array.
|
||||
|
||||
#### `const b = bits.toggle(buffer, bit)`
|
||||
|
||||
Toggle the given bit, returning its new value.
|
||||
|
||||
#### `const changed = bits.remove(buffer, bit)`
|
||||
|
||||
Unset the given bit. Returns `true` if the bit was set, otherwise `false`.
|
||||
|
||||
#### `const changed = bits.removeRange(buffer, start, end)`
|
||||
|
||||
Unset the given bit range. Returns `true` if any of the bits were set, otherwise `false`.
|
||||
|
||||
#### `const index = bits.indexOf(buffer, value[, position])`
|
||||
|
||||
Return the index of the first occurrence of `value`, or `-1` if not found. If `position` is given, return the first index that is greater than or equal to `position`.
|
||||
|
||||
#### `const index = bits.lastIndexOf(buffer, value[, position])`
|
||||
|
||||
Return the index of the last occurrence of `value`, or `-1` if not found. If `position` is given, return the last index that is less than or equal to `position`.
|
||||
|
||||
#### `const buffer = bits.of(...values)`
|
||||
|
||||
Create a buffer containing the given bits.
|
||||
|
||||
#### `const buffer = bits.from(values)`
|
||||
|
||||
Create a buffer containing the given bits.
|
||||
|
||||
#### `const iterator = bits.iterator(buffer)`
|
||||
|
||||
Create an iterator over bits.
|
||||
|
||||
## License
|
||||
|
||||
ISC
|
||||
+161
@@ -0,0 +1,161 @@
|
||||
const b4a = require('b4a')
|
||||
|
||||
function byteLength (size) {
|
||||
return Math.ceil(size / 8)
|
||||
}
|
||||
|
||||
function get (buffer, bit) {
|
||||
const n = buffer.BYTES_PER_ELEMENT * 8
|
||||
|
||||
const offset = bit & (n - 1)
|
||||
const i = (bit - offset) / n
|
||||
|
||||
return (buffer[i] & (1 << offset)) !== 0
|
||||
}
|
||||
|
||||
function set (buffer, bit, value = true) {
|
||||
const n = buffer.BYTES_PER_ELEMENT * 8
|
||||
|
||||
const offset = bit & (n - 1)
|
||||
const i = (bit - offset) / n
|
||||
const mask = 1 << offset
|
||||
|
||||
if (value) {
|
||||
if ((buffer[i] & mask) !== 0) return false
|
||||
} else {
|
||||
if ((buffer[i] & mask) === 0) return false
|
||||
}
|
||||
|
||||
buffer[i] ^= mask
|
||||
return true
|
||||
}
|
||||
|
||||
function setRange (buffer, start, end, value = true) {
|
||||
const n = buffer.BYTES_PER_ELEMENT * 8
|
||||
|
||||
let remaining = end - start
|
||||
let offset = start & (n - 1)
|
||||
let i = (start - offset) / n
|
||||
|
||||
let changed = false
|
||||
|
||||
while (remaining > 0) {
|
||||
const mask = (2 ** Math.min(remaining, n - offset) - 1) << offset
|
||||
|
||||
if (value) {
|
||||
if ((buffer[i] & mask) !== mask) {
|
||||
buffer[i] |= mask
|
||||
changed = true
|
||||
}
|
||||
} else {
|
||||
if ((buffer[i] & mask) !== 0) {
|
||||
buffer[i] &= ~mask
|
||||
changed = true
|
||||
}
|
||||
}
|
||||
|
||||
remaining -= n - offset
|
||||
offset = 0
|
||||
i++
|
||||
}
|
||||
|
||||
return changed
|
||||
}
|
||||
|
||||
function fill (buffer, value, start = 0, end = buffer.byteLength * 8) {
|
||||
const n = buffer.BYTES_PER_ELEMENT * 8
|
||||
let i, j
|
||||
|
||||
{
|
||||
const offset = start & (n - 1)
|
||||
i = (start - offset) / n
|
||||
|
||||
if (offset !== 0) {
|
||||
const mask = (2 ** Math.min(n - offset, end - start) - 1) << offset
|
||||
|
||||
if (value) buffer[i] |= mask
|
||||
else buffer[i] &= ~mask
|
||||
|
||||
i++
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
const offset = end & (n - 1)
|
||||
j = (end - offset) / n
|
||||
|
||||
if (offset !== 0 && j >= i) {
|
||||
const mask = (2 ** offset) - 1
|
||||
|
||||
if (value) buffer[j] |= mask
|
||||
else buffer[j] &= ~mask
|
||||
}
|
||||
}
|
||||
|
||||
return buffer.fill(value ? (2 ** n) - 1 : 0, i, j)
|
||||
}
|
||||
|
||||
function toggle (buffer, bit) {
|
||||
const n = buffer.BYTES_PER_ELEMENT * 8
|
||||
|
||||
const offset = bit & (n - 1)
|
||||
const i = (bit - offset) / n
|
||||
const mask = 1 << offset
|
||||
|
||||
buffer[i] ^= mask
|
||||
return (buffer[i] & mask) !== 0
|
||||
}
|
||||
|
||||
function remove (buffer, bit) {
|
||||
return set(buffer, bit, false)
|
||||
}
|
||||
|
||||
function removeRange (buffer, start, end) {
|
||||
return setRange(buffer, start, end, false)
|
||||
}
|
||||
|
||||
function indexOf (buffer, value, position = 0) {
|
||||
for (let i = position, n = buffer.byteLength * 8; i < n; i++) {
|
||||
if (get(buffer, i) === value) return i
|
||||
}
|
||||
|
||||
return -1
|
||||
}
|
||||
|
||||
function lastIndexOf (buffer, value, position = buffer.byteLength * 8 - 1) {
|
||||
for (let i = position; i >= 0; i--) {
|
||||
if (get(buffer, i) === value) return i
|
||||
}
|
||||
|
||||
return -1
|
||||
}
|
||||
|
||||
function of (...bits) {
|
||||
return from(bits)
|
||||
}
|
||||
|
||||
function from (bits) {
|
||||
const buffer = b4a.alloc(byteLength(bits.length))
|
||||
for (let i = 0; i < bits.length; i++) set(buffer, i, bits[i])
|
||||
return buffer
|
||||
}
|
||||
|
||||
function * iterator (buffer) {
|
||||
for (let i = 0, n = buffer.byteLength * 8; i < n; i++) yield get(buffer, i)
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
byteLength,
|
||||
get,
|
||||
set,
|
||||
setRange,
|
||||
fill,
|
||||
toggle,
|
||||
remove,
|
||||
removeRange,
|
||||
indexOf,
|
||||
lastIndexOf,
|
||||
of,
|
||||
from,
|
||||
iterator
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
{
|
||||
"name": "bits-to-bytes",
|
||||
"version": "1.3.0",
|
||||
"description": "Functions for doing bit manipulation of typed arrays",
|
||||
"main": "index.js",
|
||||
"files": [
|
||||
"index.js"
|
||||
],
|
||||
"scripts": {
|
||||
"test": "standard && brittle test.mjs"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/holepunchto/bits-to-bytes.git"
|
||||
},
|
||||
"author": "Kasper Isager Dalsgarð <[email protected]>",
|
||||
"license": "ISC",
|
||||
"bugs": {
|
||||
"url": "https://github.com/holepunchto/bits-to-bytes/issues"
|
||||
},
|
||||
"homepage": "https://github.com/holepunchto/bits-to-bytes#readme",
|
||||
"dependencies": {
|
||||
"b4a": "^1.5.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"brittle": "^2.3.1",
|
||||
"standard": "^17.0.0"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user