Skip to content

Instantly share code, notes, and snippets.

@okayurisotto
Created April 4, 2024 08:36
Show Gist options
  • Save okayurisotto/890b734c767631c587e3d4e628aad915 to your computer and use it in GitHub Desktop.
Save okayurisotto/890b734c767631c587e3d4e628aad915 to your computer and use it in GitHub Desktop.
export function* separate(
data: Uint8Array,
bitLength: number
): Generator<number> {
let bufferBitLength = 0;
let buffer = 0;
for (const chunk of data) {
bufferBitLength += 8;
buffer = (buffer << 8) | chunk;
while (bufferBitLength > bitLength) {
yield buffer >> (bufferBitLength - bitLength);
bufferBitLength -= bitLength;
buffer = buffer & (2 ** bufferBitLength - 1);
}
}
yield buffer << (bitLength - bufferBitLength);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment