Skip to content

Commit c1a582c

Browse files
committed
STREAMS-1982: Add singleton compat for MinKey
1 parent 0b18f7a commit c1a582c

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

snippets/mongocompat/mongotypes.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,41 @@ if (typeof (gc) == "undefined") {
714714
};
715715
}
716716

717+
// MinKey
718+
if (typeof (MinKey) != "undefined") {
719+
const OriginalMinKey = MinKey;
720+
MinKey = function () {
721+
if (MinKey.prototype.__instance__ === undefined) {
722+
MinKey.prototype.__instance__ = new OriginalMinKey();
723+
}
724+
725+
return MinKey.prototype.__instance__;
726+
};
727+
728+
MinKey.prototype = OriginalMinKey.prototype;
729+
730+
for (const key of Object.getOwnPropertyNames(OriginalMinKey)) {
731+
// Skip prototype, length, name(function internals)
732+
if (key !== 'prototype' && key !== 'length' && key !== 'name') {
733+
MinKey[key] = OriginalMinKey[key];
734+
}
735+
}
736+
737+
MinKey.prototype.toJSON = function () {
738+
return this.tojson();
739+
};
740+
741+
MinKey.prototype.tojson = function () {
742+
return "{ \"$minKey\" : 1 }";
743+
};
744+
745+
MinKey.prototype.toString = function () {
746+
return "[object Function]";
747+
};
748+
} else {
749+
print("warning: no MinKey class");
750+
}
751+
717752
// Free Functions
718753
tojsononeline = function(x) {
719754
return tojson(x, " ", true);

snippets/mongocompat/test.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ const tsFromStr = Timestamp.fromString('ff', 16);
8787
assert.strictEqual(tsFromStr.i, 255);
8888
assert.strictEqual(tsFromStr.t, 0);
8989
assert.strictEqual(Timestamp.MAX_VALUE._bsontype, 'Long');
90-
assert.strictEqual(Timestamp.MAX_VALUE, Long.MAX_UNSIGNED_VALUE);
90+
assert.strictEqual(Timestamp.MAX_VALUE, Long.MAX_UNSIGNED_VALUE);
9191

9292
const id = ObjectId('68ffa28b77bba38c9ddcf376');
9393
const dbRef = DBRef('testColl', id, 'testDb');
@@ -161,3 +161,15 @@ assert(sortedArrayJson.indexOf('"a"') < sortedArrayJson.indexOf('"z"'), 'Array.t
161161
assert(sortedArrayJson.indexOf('"b"') < sortedArrayJson.indexOf('"y"'), 'Array.tojson with sortedKeys=true should sort object keys in array elements');
162162
assert(unsortedArrayJson.indexOf('"z"') < unsortedArrayJson.indexOf('"a"'), 'Array.tojson with sortedKeys=false should not sort keys');
163163
assert(defaultArrayJson.indexOf('"z"') < defaultArrayJson.indexOf('"a"'), 'Array.tojson without sortedKeys should not sort keys');
164+
165+
// Test MinKey
166+
const minKey = new MinKey();
167+
assert(minKey instanceof MinKey, "minKey should be an instance of MinKey");
168+
assert.strictEqual(minKey.tojson(), '{ "$minKey" : 1 }', "minKey should serialize correctly");
169+
assert.strictEqual(minKey.toString(), "[object Function]", "minKey toString should work");
170+
assert.strictEqual(minKey.toJSON(), '{ "$minKey" : 1 }', "minKey toJSON should work");
171+
172+
// Test that multiple references return the same instance
173+
const anotherMinKeyRef = new MinKey();
174+
assert.strictEqual(minKey, anotherMinKeyRef, "minKey should be a singleton - v1");
175+
assert.strictEqual(MinKey(), MinKey(), "minKey should be a singleton - v2");

0 commit comments

Comments
 (0)