Skip to content

Commit 41ffb3b

Browse files
refactor by comments
1 parent 022393f commit 41ffb3b

File tree

2 files changed

+39
-24
lines changed

2 files changed

+39
-24
lines changed

snippets/mongocompat/mongotypes.js

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,14 @@ if (!NumberLong.prototype) {
380380

381381
NumberLong.prototype.nativeToString = NumberLong.prototype.toString;
382382
NumberLong.prototype.toString = function () {
383-
return `NumberLong(${this.nativeToString()})`;
383+
const INT32_MIN = -2147483648;
384+
const INT32_MAX = 2147483647;
385+
386+
const numValue = this.toNumber ? this.toNumber() : Number(this);
387+
if (numValue >= INT32_MIN && numValue <= INT32_MAX && Number.isInteger(numValue)) {
388+
return `NumberLong(${numValue})`;
389+
}
390+
return `NumberLong("${this.exactValueString}")`;
384391
};
385392

386393
NumberLong.prototype.tojson = function() {
@@ -391,39 +398,36 @@ Object.defineProperty(NumberLong.prototype, 'floatApprox', {
391398
enumerable: false,
392399
configurable: true,
393400
get: function() {
394-
return Number(this.nativeToString());
401+
return this.toNumber ? this.toNumber() : Number(this);
395402
}
396403
});
397404

398405
Object.defineProperty(NumberLong.prototype, 'top', {
399406
enumerable: false,
400407
configurable: true,
401408
get: function() {
402-
const str = this.nativeToString();
403-
const bigIntValue = BigInt(str);
404-
const unsigned64 = bigIntValue < 0n
405-
? bigIntValue + (1n << 64n)
406-
: bigIntValue;
407-
return Number((unsigned64 >> 32n) & 0xFFFFFFFFn);
409+
return this.high;
408410
}
409411
});
410412

411413
Object.defineProperty(NumberLong.prototype, 'bottom', {
412414
enumerable: false,
413415
configurable: true,
414416
get: function() {
415-
const str = this.nativeToString();
416-
const bigIntValue = BigInt(str);
417-
const unsigned64 = bigIntValue < 0n
418-
? bigIntValue + (1n << 64n)
419-
: bigIntValue;
420-
return Number(unsigned64 & 0xFFFFFFFFn);
417+
return this.low;
421418
}
422419
});
423420

424-
NumberLong.prototype.exactValueString = function() {
425-
return this.nativeToString();
426-
};
421+
Object.defineProperty(NumberLong.prototype, 'exactValueString', {
422+
enumerable: false,
423+
configurable: true,
424+
get: function() {
425+
const high = BigInt(this.high);
426+
const low = BigInt(this.low >>> 0);
427+
const value = (high << 32n) | low;
428+
return value.toString();
429+
}
430+
});
427431

428432
// NumberInt
429433
if (!NumberInt.prototype) {

snippets/mongocompat/test.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,22 @@ assert.strictEqual(ObjectId('0123456789abcdef01234567').tojson(), 'ObjectId("012
44

55
assert.strictEqual(BinData(4, 'abcdefgh').toString(), 'BinData(4,"abcdefgh")');
66

7-
assert.strictEqual(NumberLong(1234567890).toString(), 'NumberLong(1234567890)');
8-
const nl1 = NumberLong(9.223372036854776e+18, 2147483647, 4294967295);
9-
assert.strictEqual(nl1.floatApprox, 9223372036854776000);
10-
assert.strictEqual(nl1.top, 2147483647);
11-
assert.strictEqual(nl1.bottom, 4294967295);
12-
assert.strictEqual(nl1.exactValueString(), "9223372036854775807");
7+
assert.strictEqual(NumberLong(2147483647).toString(), 'NumberLong(2147483647)');
8+
assert.strictEqual(NumberLong("2147483648").toString(), 'NumberLong("2147483648")');
9+
assert.strictEqual(NumberLong(-2147483648).toString(), 'NumberLong(-2147483648)');
10+
console.log(NumberLong(-2147483649).toString());
11+
assert.strictEqual(NumberLong(-2147483649).toString(), 'NumberLong("-2147483649")');
12+
assert.strictEqual(NumberLong(9223372036854775807).toString(), 'NumberLong("9223372036854775807")');
13+
assert.strictEqual(NumberLong(-9223372036854775808).toString(), 'NumberLong("-9223372036854775808")');
14+
const maxLong = NumberLong(9223372036854775807, 2147483647, -1);
15+
assert.strictEqual(maxLong.floatApprox, 9223372036854775807);
16+
assert.strictEqual(maxLong.top, 2147483647);
17+
assert.strictEqual(maxLong.bottom, -1);//mongosh uses signed representation, while old shell uses unsigned
18+
assert.strictEqual(maxLong.exactValueString, "9223372036854775807");
19+
const minLong = NumberLong(-9223372036854775808);
20+
assert.strictEqual(minLong.floatApprox, -9223372036854776000);
21+
assert.strictEqual(minLong.top, -2147483648);
22+
assert.strictEqual(minLong.bottom, 0);
23+
assert.strictEqual(minLong.exactValueString, "-9223372036854775808");
1324
const nl2 = NumberLong("200");
14-
assert.strictEqual(nl1.compare(nl2), 1);
25+
assert.strictEqual(maxLong.compare(nl2), 1);

0 commit comments

Comments
 (0)