Skip to content

Commit bb376a0

Browse files
committed
add seek()
1 parent 7ae96d9 commit bb376a0

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ A Deno module helper class that wraps around [DataView](https://developer.mozill
55
## Usage
66

77
```typescript
8-
import { BinaryReader } from "https://deno.land/x/binary_reader@v0.1.3/mod.ts";
8+
import { BinaryReader } from "https://deno.land/x/binary_reader@v0.1.4/mod.ts";
99

1010
const filename = "some-file.bin";
1111
const file = Deno.openSync(filename);

binary_reader.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ export class BinaryReader {
2020
this.#position -= numBytes;
2121
}
2222

23+
/**
24+
* Moves the buffer/DataView byteOffset to the specified position.
25+
* @param position The position to move the buffer/DataView to.
26+
*/
27+
seek(position: number): void {
28+
if (position > this.buffer.length) {
29+
return;
30+
}
31+
32+
this.#position = position;
33+
}
34+
2335
/**
2436
* Returns the current position in the buffer.
2537
*/

binary_reader_test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,3 +363,21 @@ Deno.test(
363363
assertEquals(data, [0x48, 0x65, 0x6C, 0x6C]);
364364
},
365365
);
366+
367+
Deno.test(
368+
"seek",
369+
function (): void {
370+
const buffer = new ArrayBuffer(4);
371+
const array = new Uint8Array(buffer);
372+
373+
array[0] = 0x48;
374+
array[1] = 0x65;
375+
array[2] = 0x6C;
376+
array[3] = 0x6C;
377+
378+
const binaryReader = new BinaryReader(array);
379+
assertEquals(binaryReader.position, 0);
380+
binaryReader.seek(3);
381+
assertEquals(binaryReader.position, 3);
382+
},
383+
);

0 commit comments

Comments
 (0)