Skip to content

Commit 2880cd5

Browse files
Refactor per comments
1 parent f8cc0a4 commit 2880cd5

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

snippets/mongocompat/mongotypes.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22
if (typeof (Timestamp) != "undefined") {
33
const OriginalTimestamp = Timestamp;
44

5-
// Reference: https://github.com/10gen/mongo/blob/master/src/mongo/scripting/mozjs/timestamp.cpp:67-78
5+
// Reference: https://github.com/mongodb/mongo/blob/c4d21d3346572e28df2f174df4d87e7618df4a77/src/mongo/scripting/mozjs/timestamp.cpp#L67-L78
66
function validateTimestampComponent(component, name) {
77
const MAX_UINT32 = 4294967295;
88

99
if (typeof component !== 'number') {
10-
throw new Error(name + " must be a number");
10+
throw new TypeError(`${name} must be a number`);
1111
}
1212

1313
const val = Math.floor(component);
1414
if (val < 0 || val > MAX_UINT32) {
15-
throw new Error(
16-
name + " must be non-negative and not greater than " + MAX_UINT32 + ", got " + val
15+
throw new TypeError(
16+
`${name} must be non-negative and not greater than ${MAX_UINT32}, got ${val}`
1717
);
1818
}
1919

@@ -28,14 +28,14 @@ if (typeof (Timestamp) != "undefined") {
2828
if (arguments.length === 1) {
2929
const proto = Object.getPrototypeOf(t);
3030
if ((proto === null || proto === Object.prototype) && ('t' in t || 'i' in t)) {
31-
var validatedT = validateTimestampComponent(t.t || 0, "Timestamp time (seconds)");
32-
var validatedI = validateTimestampComponent(t.i || 0, "Timestamp increment");
31+
const validatedT = validateTimestampComponent(t.t || 0, "Timestamp time (seconds)");
32+
const validatedI = validateTimestampComponent(t.i || 0, "Timestamp increment");
3333
return new OriginalTimestamp({ t: validatedT, i: validatedI });
3434
}
3535
return new OriginalTimestamp(t);
3636
}
3737

38-
// Reference: https://github.com/10gen/mongo/blob/master/src/mongo/scripting/mozjs/timestamp.cpp:91-98
38+
// Reference: https://github.com/mongodb/mongo/blob/c4d21d3346572e28df2f174df4d87e7618df4a77/src/mongo/scripting/mozjs/timestamp.cpp#L91-L98
3939
if (arguments.length === 2) {
4040
const validatedT = validateTimestampComponent(t, "Timestamp time (seconds)");
4141
const validatedI = validateTimestampComponent(i, "Timestamp increment");
@@ -47,8 +47,7 @@ if (typeof (Timestamp) != "undefined") {
4747

4848
Timestamp.prototype = OriginalTimestamp.prototype;
4949

50-
var staticProps = Object.getOwnPropertyNames(OriginalTimestamp);
51-
for (var key of staticProps) {
50+
for (const key of Object.getOwnPropertyNames(OriginalTimestamp)) {
5251
// Skip prototype, length, name(function internals)
5352
if (key !== 'prototype' && key !== 'length' && key !== 'name') {
5453
Timestamp[key] = OriginalTimestamp[key];

snippets/mongocompat/test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,14 @@ try {
4646
assert(e.message.includes('not greater than 4294967295'));
4747
}
4848
const ts4 = Timestamp(123, 456);
49+
assert(ts4 instanceof Timestamp);
4950
assert.strictEqual(ts4.toString(), 'Timestamp(123, 456)');
5051
assert.strictEqual(ts4.tojson(), 'Timestamp(123, 456)');
5152
assert.strictEqual(ts4.getTime(), 123);
5253
assert.strictEqual(ts4.getInc(), 456);
5354
assert.strictEqual(ts4._bsontype, 'Timestamp');
5455
const tsFromBits = Timestamp.fromBits(100, 200);
56+
assert(tsFromBits instanceof Timestamp);
5557
assert.strictEqual(tsFromBits.i, 100);
5658
assert.strictEqual(tsFromBits.t, 200);
5759
assert.strictEqual(tsFromBits.toString(), 'Timestamp(200, 100)');

0 commit comments

Comments
 (0)