Skip to content

Commit

Permalink
Add unpack code
Browse files Browse the repository at this point in the history
  • Loading branch information
francisrstokes committed Jan 9, 2022
1 parent 4ff9125 commit dde5985
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,29 @@ export class BitPacker {
}
}
}

static createUnpackIterator = function* <T>(buffer: Uint8Array, unpackFn: UnpackFn<T>) {
let index = 0;
let bitIndex = 7;

let pattern = '';

while (index < buffer.byteLength) {
pattern += (buffer[index] & (1 << bitIndex)) >>> bitIndex;
bitIndex--;

const transformedPatternValue = unpackFn(pattern);
if (transformedPatternValue !== null) {
yield transformedPatternValue;
pattern = '';
}

if (bitIndex === -1) {
index++;
bitIndex = 7;
}
}
}
}

const packed = BitPacker.pack([
Expand Down

0 comments on commit dde5985

Please sign in to comment.