Skip to content

Commit 0a9606f

Browse files
bit math only change
1 parent 2615864 commit 0a9606f

File tree

1 file changed

+7
-14
lines changed

1 file changed

+7
-14
lines changed

src/long.ts

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -243,20 +243,13 @@ export class Long extends BSONValue {
243243
* @returns The corresponding Long value
244244
*/
245245
static fromBigInt(value: bigint, unsigned?: boolean): Long {
246-
const byteStr = value.toString(16);
247-
const isNeg = byteStr[0] === '-';
248-
const startIndex = isNeg ? 1 : 0;
249-
const lowByteString =
250-
byteStr.length - startIndex > 8
251-
? byteStr.substring(byteStr.length - 8, byteStr.length)
252-
: isNeg
253-
? byteStr.slice(1)
254-
: byteStr;
255-
const highByteString =
256-
byteStr.length - startIndex > 8 ? byteStr.substring(startIndex, byteStr.length - 8) : 0;
257-
return isNeg
258-
? new Long(Number('0x' + lowByteString), Number('0x' + highByteString), unsigned).neg()
259-
: new Long(Number('0x' + lowByteString), Number('0x' + highByteString), unsigned);
246+
/* eslint-disable no-bigint-usage/no-bigint-literals */
247+
const bit32Mask = 0xffffffffn;
248+
249+
const lowBits = value & bit32Mask;
250+
const highBits = (value >> 32n) & bit32Mask;
251+
/* eslint-enable no-bigint-usage/no-bigint-literals */
252+
return new Long(Number(lowBits), Number(highBits), unsigned);
260253
}
261254

262255
/**

0 commit comments

Comments
 (0)