Skip to content

Commit ca5ead1

Browse files
authored
STREAMS-1982: Add singleton compat for MinKey (#23)
1 parent 0b18f7a commit ca5ead1

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-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: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
load(__dirname + '/index.js');
22

3+
const bson = require('bson');
4+
35
assert.strictEqual(ObjectId('0123456789abcdef01234567').tojson(), 'ObjectId("0123456789abcdef01234567")');
46

57
assert.strictEqual(BinData(4, 'abcdefgh').toString(), 'BinData(4,"abcdefgh")');
@@ -87,7 +89,7 @@ const tsFromStr = Timestamp.fromString('ff', 16);
8789
assert.strictEqual(tsFromStr.i, 255);
8890
assert.strictEqual(tsFromStr.t, 0);
8991
assert.strictEqual(Timestamp.MAX_VALUE._bsontype, 'Long');
90-
assert.strictEqual(Timestamp.MAX_VALUE, Long.MAX_UNSIGNED_VALUE);
92+
assert.strictEqual(Timestamp.MAX_VALUE, Long.MAX_UNSIGNED_VALUE);
9193

9294
const id = ObjectId('68ffa28b77bba38c9ddcf376');
9395
const dbRef = DBRef('testColl', id, 'testDb');
@@ -161,3 +163,19 @@ assert(sortedArrayJson.indexOf('"a"') < sortedArrayJson.indexOf('"z"'), 'Array.t
161163
assert(sortedArrayJson.indexOf('"b"') < sortedArrayJson.indexOf('"y"'), 'Array.tojson with sortedKeys=true should sort object keys in array elements');
162164
assert(unsortedArrayJson.indexOf('"z"') < unsortedArrayJson.indexOf('"a"'), 'Array.tojson with sortedKeys=false should not sort keys');
163165
assert(defaultArrayJson.indexOf('"z"') < defaultArrayJson.indexOf('"a"'), 'Array.tojson without sortedKeys should not sort keys');
166+
167+
// Test MinKey
168+
const minKey = new MinKey();
169+
assert(minKey instanceof MinKey, "minKey should be an instance of MinKey");
170+
assert.strictEqual(minKey.tojson(), '{ "$minKey" : 1 }');
171+
assert.strictEqual(minKey.toString(), "[object Function]");
172+
assert.strictEqual(minKey.toJSON(), '{ "$minKey" : 1 }');
173+
174+
// Test that multiple references return the same instance
175+
const anotherMinKeyRef = new MinKey();
176+
assert.strictEqual(minKey, anotherMinKeyRef);
177+
assert.strictEqual(MinKey(), MinKey());
178+
179+
const serializedBsonMinKey = bson.serialize({ key1: MinKey, key2: MinKey() });
180+
const deserializedBsonMinKey = bson.deserialize(serializedBsonMinKey);
181+
assert.deepStrictEqual(deserializedBsonMinKey.key1, deserializedBsonMinKey.key2);

0 commit comments

Comments
 (0)