Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
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
20 changes: 19 additions & 1 deletion snippets/mongocompat/test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
load(__dirname + '/index.js');

const bson = require('bson');

assert.strictEqual(ObjectId('0123456789abcdef01234567').tojson(), 'ObjectId("0123456789abcdef01234567")');

assert.strictEqual(BinData(4, 'abcdefgh').toString(), 'BinData(4,"abcdefgh")');
Expand Down Expand Up @@ -87,7 +89,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 +163,19 @@ 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 }');
assert.strictEqual(minKey.toString(), "[object Function]");
assert.strictEqual(minKey.toJSON(), '{ "$minKey" : 1 }');

// Test that multiple references return the same instance
const anotherMinKeyRef = new MinKey();
assert.strictEqual(minKey, anotherMinKeyRef);
assert.strictEqual(MinKey(), MinKey());

const serializedBsonMinKey = bson.serialize({ key1: MinKey, key2: MinKey() });
const deserializedBsonMinKey = bson.deserialize(serializedBsonMinKey);
assert.deepStrictEqual(deserializedBsonMinKey.key1, deserializedBsonMinKey.key2);