Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
.lock-wscript
.idea/
.vscode/
.zed/
*.iml
.npmrc
.nvmrc
Expand Down
35 changes: 35 additions & 0 deletions snippets/mongocompat/mongotypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,41 @@ if (typeof (gc) == "undefined") {
};
}

// MinKey
if (typeof (MinKey) != "undefined") {
const OriginalMinKey = MinKey;
MinKey = function () {
if (MinKey.prototype.__instance__ === undefined) {
MinKey.prototype.__instance__ = new OriginalMinKey();
}

return MinKey.prototype.__instance__;
};

MinKey.prototype = OriginalMinKey.prototype;

for (const key of Object.getOwnPropertyNames(OriginalMinKey)) {
// Skip prototype, length, name(function internals)
if (key !== 'prototype' && key !== 'length' && key !== 'name') {
MinKey[key] = OriginalMinKey[key];
}
}

MinKey.prototype.toJSON = function () {
return this.tojson();
};

MinKey.prototype.tojson = function () {
return "{ \"$minKey\" : 1 }";
};

MinKey.prototype.toString = function () {
return "[object Function]";
};
} else {
print("warning: no MinKey class");
}

// Free Functions
tojsononeline = function(x) {
return tojson(x, " ", true);
Expand Down
14 changes: 13 additions & 1 deletion snippets/mongocompat/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ const tsFromStr = Timestamp.fromString('ff', 16);
assert.strictEqual(tsFromStr.i, 255);
assert.strictEqual(tsFromStr.t, 0);
assert.strictEqual(Timestamp.MAX_VALUE._bsontype, 'Long');
assert.strictEqual(Timestamp.MAX_VALUE, Long.MAX_UNSIGNED_VALUE);
assert.strictEqual(Timestamp.MAX_VALUE, Long.MAX_UNSIGNED_VALUE);

const id = ObjectId('68ffa28b77bba38c9ddcf376');
const dbRef = DBRef('testColl', id, 'testDb');
Expand Down Expand Up @@ -161,3 +161,15 @@ assert(sortedArrayJson.indexOf('"a"') < sortedArrayJson.indexOf('"z"'), 'Array.t
assert(sortedArrayJson.indexOf('"b"') < sortedArrayJson.indexOf('"y"'), 'Array.tojson with sortedKeys=true should sort object keys in array elements');
assert(unsortedArrayJson.indexOf('"z"') < unsortedArrayJson.indexOf('"a"'), 'Array.tojson with sortedKeys=false should not sort keys');
assert(defaultArrayJson.indexOf('"z"') < defaultArrayJson.indexOf('"a"'), 'Array.tojson without sortedKeys should not sort keys');

// Test MinKey
const minKey = new MinKey();
assert(minKey instanceof MinKey, "minKey should be an instance of MinKey");
assert.strictEqual(minKey.tojson(), '{ "$minKey" : 1 }', "minKey should serialize correctly");
assert.strictEqual(minKey.toString(), "[object Function]", "minKey toString should work");
assert.strictEqual(minKey.toJSON(), '{ "$minKey" : 1 }', "minKey toJSON should work");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a big deal, but passing a static string as a message for assert.strictEqual() and friends is an antipattern since it will hide information about the actual comparison results – if you want to add this type of information, add a comment


// Test that multiple references return the same instance
const anotherMinKeyRef = new MinKey();
assert.strictEqual(minKey, anotherMinKeyRef, "minKey should be a singleton - v1");
assert.strictEqual(MinKey(), MinKey(), "minKey should be a singleton - v2");