Skip to content

Commit 34dc671

Browse files
pr requested changes
1 parent 82594b5 commit 34dc671

File tree

2 files changed

+62
-43
lines changed

2 files changed

+62
-43
lines changed

src/long.ts

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -133,26 +133,51 @@ export class Long extends BSONValue {
133133

134134
/**
135135
* Constructs a 64 bit two's-complement integer, given its low and high 32 bit values as *signed* integers.
136-
* See the from* functions below for more convenient ways of constructing Longs.
137-
*
138-
* Acceptable signatures are:
139-
* - Long(low, high, unsigned?)
140-
* - Long(bigint, unsigned?)
141-
* - Long(string, unsigned?)
142136
*
143137
* @param low - The low (signed) 32 bits of the long
144138
* @param high - The high (signed) 32 bits of the long
145139
* @param unsigned - Whether unsigned or not, defaults to signed
146140
*/
147-
constructor(low: number | bigint | string = 0, high?: number | boolean, unsigned?: boolean) {
141+
constructor(low: number, high?: number, unsigned?: boolean);
142+
/**
143+
* Constructs a 64 bit two's-complement integer, given a bigint representation.
144+
*
145+
* @param value - BigInt representation of the long value
146+
* @param unsigned - Whether unsigned or not, defaults to signed
147+
*/
148+
constructor(value: bigint, unsigned?: boolean);
149+
/**
150+
* Constructs a 64 bit two's-complement integer, given a string representation.
151+
*
152+
* @param value - 32 bit number representation of the Long
153+
* @param unsigned - Whether unsigned or not, defaults to signed
154+
*/
155+
constructor(value: number, unsigned?: boolean);
156+
/**
157+
* Constructs a 64 bit two's-complement integer, given a string representation.
158+
*
159+
* @param value - String representation of the long value
160+
* @param unsigned - Whether unsigned or not, defaults to signed
161+
*/
162+
constructor(value: string, unsigned?: boolean);
163+
constructor(
164+
lowOrValue: number | bigint | string = 0,
165+
highOrUnsigned?: number | boolean,
166+
unsigned?: boolean
167+
) {
148168
super();
149-
if (typeof low === 'bigint') {
150-
Object.assign(this, Long.fromBigInt(low, !!high));
151-
} else if (typeof low === 'string') {
152-
Object.assign(this, Long.fromString(low, !!high));
169+
unsigned = typeof highOrUnsigned === 'boolean' ? highOrUnsigned : !!unsigned;
170+
const high = typeof highOrUnsigned === 'number' ? highOrUnsigned : 0;
171+
if (typeof lowOrValue === 'bigint') {
172+
const longFromBigInt = Long.fromBigInt(lowOrValue, unsigned);
173+
this.low = longFromBigInt.low;
174+
this.high = longFromBigInt.high;
175+
this.unsigned = longFromBigInt.unsigned;
176+
} else if (typeof lowOrValue === 'string') {
177+
Object.assign(this, Long.fromString(lowOrValue, unsigned));
153178
} else {
154-
this.low = low | 0;
155-
this.high = (high as number) | 0;
179+
this.low = lowOrValue | 0;
180+
this.high = high | 0;
156181
this.unsigned = !!unsigned;
157182
}
158183
}
@@ -176,17 +201,6 @@ export class Long extends BSONValue {
176201
/** Minimum signed value. */
177202
static MIN_VALUE = Long.fromBits(0, 0x80000000 | 0, false);
178203

179-
/**
180-
* @internal
181-
* bit mask used for fromBigInt method, lazy loaded
182-
*/
183-
static FROM_BIGINT_BIT_MASK: bigint;
184-
185-
/**
186-
* @internal
187-
* bit shift used for fromBigInt method, lazy loaded */
188-
static FROM_BIGINT_BIT_SHIFT: bigint;
189-
190204
/**
191205
* Returns a Long representing the 64 bit integer that comes by concatenating the given low and high bits.
192206
* Each is assumed to use 32 bits.
@@ -255,12 +269,12 @@ export class Long extends BSONValue {
255269
*/
256270
static fromBigInt(value: bigint, unsigned?: boolean): Long {
257271
// eslint-disable-next-line no-restricted-globals
258-
Long.FROM_BIGINT_BIT_MASK ??= BigInt(0xffffffff);
272+
const FROM_BIGINT_BIT_MASK = BigInt(0xffffffff);
259273
// eslint-disable-next-line no-restricted-globals
260-
Long.FROM_BIGINT_BIT_SHIFT ??= BigInt(32);
274+
const FROM_BIGINT_BIT_SHIFT = BigInt(32);
261275
return new Long(
262-
Number(value & Long.FROM_BIGINT_BIT_MASK),
263-
Number((value >> Long.FROM_BIGINT_BIT_SHIFT) & Long.FROM_BIGINT_BIT_MASK),
276+
Number(value & FROM_BIGINT_BIT_MASK),
277+
Number((value >> FROM_BIGINT_BIT_SHIFT) & FROM_BIGINT_BIT_MASK),
264278
unsigned
265279
);
266280
}

test/node/long.test.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -171,25 +171,30 @@ describe('Long', function () {
171171
name: string,
172172
input: bigint,
173173
unsigned: boolean | undefined,
174-
expectedStr?: string
174+
expectedLong?: Long
175175
][] = [
176-
['0', BigInt('0'), false, '0'],
177-
['-0 (bigint coerces this to 0)', BigInt('-0'), false, '0'],
176+
['0', BigInt('0'), false, Long.ZERO],
177+
['-0 (bigint coerces this to 0)', BigInt('-0'), false, Long.ZERO],
178178
[
179179
'max unsigned input',
180180
BigInt(Long.MAX_UNSIGNED_VALUE.toString(10)),
181181
true,
182-
Long.MAX_UNSIGNED_VALUE.toString(10)
182+
Long.MAX_UNSIGNED_VALUE
183+
],
184+
['max signed input', BigInt(Long.MAX_VALUE.toString(10)), false, Long.MAX_VALUE],
185+
['min signed input', BigInt(Long.MIN_VALUE.toString(10)), false, Long.MIN_VALUE],
186+
[
187+
'negative greater than 32 bits',
188+
BigInt(-9228915101),
189+
false,
190+
Long.fromBits(0xd9e9ee63, 0xfffffffd)
183191
],
184-
['max signed input', BigInt(Long.MAX_VALUE.toString(10)), false, Long.MAX_VALUE.toString(10)],
185-
['min signed input', BigInt(Long.MIN_VALUE.toString(10)), false, Long.MIN_VALUE.toString(10)],
186-
['negative greater than 32 bits', BigInt(-9228915101), false, '-9228915101'],
187-
['less than 32 bits', BigInt(245666), false, '245666'],
188-
['unsigned less than 32 bits', BigInt(245666), true, '245666'],
189-
['negative less than 32 bits', BigInt(-245666), false, '-245666'],
190-
['max int32', BigInt(BSON_INT32_MAX), false, BSON_INT32_MAX.toString(10)],
191-
['max int32 unsigned', BigInt(BSON_INT32_MAX), true, BSON_INT32_MAX.toString(10)],
192-
['min int32', BigInt(BSON_INT32_MIN), false, BSON_INT32_MIN.toString(10)]
192+
['less than 32 bits', BigInt(245666), false, new Long(245666)],
193+
['unsigned less than 32 bits', BigInt(245666), true, new Long(245666, true)],
194+
['negative less than 32 bits', BigInt(-245666), false, new Long(-245666, -1)],
195+
['max int32', BigInt(BSON_INT32_MAX), false, new Long(BSON_INT32_MAX)],
196+
['max int32 unsigned', BigInt(BSON_INT32_MAX), true, new Long(BSON_INT32_MAX, 0, true)],
197+
['min int32', BigInt(BSON_INT32_MIN), false, new Long(BSON_INT32_MIN, -1)]
193198
];
194199

195200
beforeEach(function () {
@@ -198,10 +203,10 @@ describe('Long', function () {
198203
}
199204
});
200205

201-
for (const [testName, num, unsigned, expectedStr] of inputs) {
206+
for (const [testName, num, unsigned, expectedLong] of inputs) {
202207
context(`when the input is ${testName}`, () => {
203208
it(`should return a Long representation of the input`, () => {
204-
expect(Long.fromBigInt(num, unsigned).toString(10)).to.equal(expectedStr);
209+
expect(Long.fromBigInt(num, unsigned)).to.deep.equal(expectedLong);
205210
});
206211
});
207212
}

0 commit comments

Comments
 (0)