From 3faff2f6f50a8e72d81365b5bce640de610d75a2 Mon Sep 17 00:00:00 2001 From: Luan Date: Sat, 2 Nov 2024 10:30:41 -0300 Subject: [PATCH] refactor(UMP): Simplify `readVarInt` method Based on the following from the latest `base.js` from YouTube: ```ts if (a.Kx(b, 1)) { var c = a.getUint8(b); c = c < 128 ? 1 : c < 192 ? 2 : c < 224 ? 3 : c < 240 ? 4 : 5 } else c = 0; ``` --- src/core/UMP.ts | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/src/core/UMP.ts b/src/core/UMP.ts index 9bad348..89b9947 100644 --- a/src/core/UMP.ts +++ b/src/core/UMP.ts @@ -38,24 +38,13 @@ export class UMP { } } - public readVarInt(offset: number): [ number, number ] { + public readVarInt(offset: number): [number, number] { let byteLength: number; + // Determine the length of the val if (this.chunkedDataBuffer.canReadBytes(offset, 1)) { const firstByte = this.chunkedDataBuffer.getUint8(offset); - - // Determine the length of the val - if (firstByte < 128) { - byteLength = 1; - } else if (firstByte < 192) { - byteLength = 2; - } else if (firstByte < 224) { - byteLength = 3; - } else if (firstByte < 240) { - byteLength = 4; - } else { - byteLength = 5; - } + byteLength = firstByte < 128 ? 1 : firstByte < 192 ? 2 : firstByte < 224 ? 3 : firstByte < 240 ? 4 : 5; } else { byteLength = 0; }