Skip to content

Commit

Permalink
chore(common): return destination from writes
Browse files Browse the repository at this point in the history
  • Loading branch information
janniks committed Nov 30, 2023
1 parent 25c821b commit 9dc46b3
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions packages/common/src/buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ export function readUInt16BE(source: Uint8Array, offset: number): number {
}

/** @ignore */
export function writeUInt16BE(source: Uint8Array, value: number, offset: number): void {
source[offset + 0] = value >>> 8;
source[offset + 1] = value >>> 0;
export function writeUInt16BE(destination: Uint8Array, value: number, offset = 0): Uint8Array {
destination[offset + 0] = value >>> 8;
destination[offset + 1] = value >>> 0;
return destination;
}

// The following methods are based on `microsoft/vscode` implementation
Expand All @@ -49,8 +50,9 @@ export function readUInt8(source: Uint8Array, offset: number): number {
}

/** @ignore */
export function writeUInt8(destination: Uint8Array, value: number, offset: number): void {
export function writeUInt8(destination: Uint8Array, value: number, offset = 0): Uint8Array {
destination[offset] = value;
return destination;
}

/** @ignore */
Expand All @@ -59,10 +61,11 @@ export function readUInt16LE(source: Uint8Array, offset: number): number {
}

/** @ignore */
export function writeUInt16LE(destination: Uint8Array, value: number, offset: number): void {
export function writeUInt16LE(destination: Uint8Array, value: number, offset = 0): Uint8Array {
destination[offset + 0] = value & 0b1111_1111;
value >>>= 8;
destination[offset + 1] = value & 0b1111_1111;
return destination;
}

/** @ignore */
Expand All @@ -76,14 +79,15 @@ export function readUInt32BE(source: Uint8Array, offset: number): number {
}

/** @ignore */
export function writeUInt32BE(destination: Uint8Array, value: number, offset: number): void {
export function writeUInt32BE(destination: Uint8Array, value: number, offset = 0): Uint8Array {
destination[offset + 3] = value;
value >>>= 8;
destination[offset + 2] = value;
value >>>= 8;
destination[offset + 1] = value;
value >>>= 8;
destination[offset] = value;
return destination;
}

/** @ignore */
Expand All @@ -97,12 +101,13 @@ export function readUInt32LE(source: Uint8Array, offset: number): number {
}

/** @ignore */
export function writeUInt32LE(destination: Uint8Array, value: number, offset: number): void {
export function writeUInt32LE(destination: Uint8Array, value: number, offset = 0): Uint8Array {
destination[offset + 0] = value & 0b1111_1111;
value >>>= 8;
destination[offset + 1] = value & 0b1111_1111;
value >>>= 8;
destination[offset + 2] = value & 0b1111_1111;
value >>>= 8;
destination[offset + 3] = value & 0b1111_1111;
return destination;
}

0 comments on commit 9dc46b3

Please sign in to comment.