Skip to content

Commit 0c68636

Browse files
committed
remove wrap option from .readString
1 parent 7f2bc2b commit 0c68636

2 files changed

Lines changed: 19 additions & 12 deletions

File tree

‎.changeset/real-sloths-fold.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"binary-util": major
3+
---
4+
5+
Removed `wrap` option from `.readString()`.

‎src/decoder.ts‎

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ type BaseOptions = {
55
}
66

77
type BaseStringOptions = {
8-
wrap?: (str: Buffer) => Buffer
98
encoding?: BufferEncoding
109
}
1110

@@ -386,20 +385,19 @@ export class Decoder {
386385
/**
387386
* Reads a string from the buffer. Defaults to `utf8`.
388387
*
389-
* @param options - Requires either `zeroed` or `length`.
390-
* @param options.zeroed - Read until a null terminator (`0x00`) is reached.
391-
* @param options.length - Bytes to read.
392-
* @param options.encoding - Defaults to `utf8`.
393-
* @param options.wrap - ?
388+
* @param opts - Requires either `zeroed` or `length`.
389+
* @param opts.zeroed - Read until a null terminator (`0x00`) is reached.
390+
* @param opts.length - Bytes to read.
391+
* @param opts.encoding - Defaults to `utf8`.
394392
*/
395-
readString(options: StringOptions): string {
396-
let strBuffer: Buffer
393+
readString(opts: StringOptions): string {
394+
let strBuffer: Buffer | null = null
397395

398-
if ("length" in options) {
399-
strBuffer = this.readBuffer(options)
396+
if ("length" in opts && opts.length != null) {
397+
strBuffer = this.readBuffer(opts)
400398
}
401399

402-
if ("zeroed" in options) {
400+
if ("zeroed" in opts && opts.zeroed) {
403401
const view = new DataView(
404402
this.#buffer.buffer,
405403
this.#buffer.byteOffset + this.#currentOffset,
@@ -414,6 +412,10 @@ export class Decoder {
414412
this.#currentOffset += size + 1
415413
}
416414

417-
return strBuffer!.toString(options.encoding ?? "utf8")
415+
if (strBuffer == null) {
416+
throw new Error("You need to specify either `zeroed` or `length`.")
417+
}
418+
419+
return strBuffer.toString(opts.encoding ?? "utf8")
418420
}
419421
}

0 commit comments

Comments
 (0)