Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: refactor codebase #407

Merged
merged 9 commits into from
Jan 16, 2024
Prev Previous commit
Next Next commit
fix: simplify uint16 encode/decode
wemeetagain committed Dec 6, 2023
commit 150f78fa06fdfce4b411679c05f50dd82cea313b
8 changes: 6 additions & 2 deletions src/encoder.ts
Original file line number Diff line number Diff line change
@@ -6,7 +6,8 @@ import type { LengthDecoderFunction } from 'it-length-prefixed'

export const uint16BEEncode = (value: number): Uint8Array => {
const target = uint8ArrayAllocUnsafe(2)
new DataView(target.buffer, target.byteOffset, target.byteLength).setUint16(0, value, false)
target[0] = value >> 8
target[1] = value
return target
}
uint16BEEncode.bytes = 2
@@ -15,7 +16,10 @@ export const uint16BEDecode: LengthDecoderFunction = (data: Uint8Array | Uint8Ar
if (data.length < 2) throw RangeError('Could not decode int16BE')

if (data instanceof Uint8Array) {
return new DataView(data.buffer, data.byteOffset, data.byteLength).getUint16(0, false)
let value = 0
value += data[0] << 8
value += data[1]
return value
}

return data.getUint16(0)