Skip to content

Commit 0942c69

Browse files
committed
STREAMS-1982: Add singleton compat for MinKey
1 parent a91825f commit 0942c69

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');
@@ -131,3 +131,15 @@ try {
131131
}
132132
assert.strictEqual(typeof tojsonObject({ key: "value" }), 'string');
133133
assert.strictEqual(typeof tojsonObject([1, 2, 3]), 'string');
134+
135+
// Test MinKey
136+
const minKey = new MinKey();
137+
assert(minKey instanceof MinKey, "minKey should be an instance of MinKey");
138+
assert.strictEqual(minKey.tojson(), '{ "$minKey" : 1 }', "minKey should serialize correctly");
139+
assert.strictEqual(minKey.toString(), "[object Function]", "minKey toString should work");
140+
assert.strictEqual(minKey.toJSON(), '{ "$minKey" : 1 }', "minKey toJSON should work");
141+
142+
// Test that multiple references return the same instance
143+
const anotherMinKeyRef = new MinKey();
144+
assert.strictEqual(minKey, anotherMinKeyRef, "minKey should be a singleton - v1");
145+
assert.strictEqual(MinKey(), MinKey(), "minKey should be a singleton - v2");

0 commit comments

Comments
 (0)