Skip to content

Commit babdf21

Browse files
STREAMS-1991: Add sortedKey param to tojson function
1 parent a91825f commit babdf21

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

snippets/mongocompat/mongotypes.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ Array.tojson = function(a, indent, nolint, depth) {
256256
indent += "\t";
257257

258258
for (var i = 0; i < a.length; i++) {
259-
s += indent + tojson(a[i], indent, nolint, depth + 1);
259+
s += indent + tojson(a[i], indent, nolint, depth + 1, false);
260260
if (i < a.length - 1) {
261261
s += "," + elementSeparator;
262262
}
@@ -719,7 +719,7 @@ tojsononeline = function(x) {
719719
return tojson(x, " ", true);
720720
};
721721

722-
tojson = function(x, indent, nolint, depth) {
722+
tojson = function(x, indent, nolint, depth, sortKeys) {
723723
if (x === null)
724724
return "null";
725725

@@ -740,7 +740,7 @@ tojson = function(x, indent, nolint, depth) {
740740
case "boolean":
741741
return "" + x;
742742
case "object": {
743-
var s = tojsonObject(x, indent, nolint, depth);
743+
var s = tojsonObject(x, indent, nolint, depth, sortKeys);
744744
if ((nolint == null || nolint == true) && s.length < 80 &&
745745
(indent == null || indent.length == 0)) {
746746
s = s.replace(/[\t\r\n]+/gm, " ");
@@ -757,10 +757,13 @@ tojson = function(x, indent, nolint, depth) {
757757
};
758758
tojson.MAX_DEPTH = 100;
759759

760-
tojsonObject = function(x, indent, nolint, depth) {
760+
tojsonObject = function(x, indent, nolint, depth, sortKeys) {
761761
if (typeof depth !== 'number') {
762762
depth = 0;
763763
}
764+
if (typeof sortKeys !== 'boolean') {
765+
sortKeys = false;
766+
}
764767
var lineEnding = nolint ? " " : "\n";
765768
var tabSpace = nolint ? "" : "\t";
766769
if (typeof x !== "object") {
@@ -771,12 +774,12 @@ tojsonObject = function(x, indent, nolint, depth) {
771774
indent = "";
772775

773776
if (typeof (x.tojson) == "function" && x.tojson != tojson) {
774-
return x.tojson(indent, nolint, depth);
777+
return x.tojson(indent, nolint, depth, sortKeys);
775778
}
776779

777780
if (x.constructor && typeof (x.constructor.tojson) == "function" &&
778781
x.constructor.tojson != tojson) {
779-
return x.constructor.tojson(x, indent, nolint, depth);
782+
return x.constructor.tojson(x, indent, nolint, depth, sortKeys);
780783
}
781784

782785
if (x instanceof Error) {
@@ -802,8 +805,14 @@ tojsonObject = function(x, indent, nolint, depth) {
802805
var keys = x;
803806
if (typeof (x._simpleKeys) == "function")
804807
keys = x._simpleKeys();
805-
var fieldStrings = [];
808+
var keyNames = [];
806809
for (var k in keys) {
810+
keyNames.push(k);
811+
}
812+
if (sortKeys) keyNames.sort();
813+
814+
var fieldStrings = [];
815+
for (var k of keyNames) {
807816
var val = x[k];
808817

809818
// skip internal DB types to avoid issues with interceptors
@@ -812,7 +821,7 @@ tojsonObject = function(x, indent, nolint, depth) {
812821
if (typeof DBCollection != 'undefined' && val == DBCollection.prototype)
813822
continue;
814823

815-
fieldStrings.push(indent + "\"" + k + "\" : " + tojson(val, indent, nolint, depth + 1));
824+
fieldStrings.push(indent + "\"" + k + "\" : " + tojson(val, indent, nolint, depth + 1, sortKeys));
816825
}
817826

818827
if (fieldStrings.length > 0) {

snippets/mongocompat/test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,25 @@ try {
131131
}
132132
assert.strictEqual(typeof tojsonObject({ key: "value" }), 'string');
133133
assert.strictEqual(typeof tojsonObject([1, 2, 3]), 'string');
134+
135+
// Test sortedkey parameter
136+
const unsortedObj = { z: 1, a: 2, m: 3 };
137+
const sortedJson = tojson(unsortedObj, "", true, 0, true);
138+
const unsortedJson = tojson(unsortedObj, "", true, 0, false);
139+
const defaultJson = tojson(unsortedObj);
140+
assert(sortedJson.indexOf('"a"') < sortedJson.indexOf('"m"'), 'sortedJson should be sorted alphabetically');
141+
assert(sortedJson.indexOf('"m"') < sortedJson.indexOf('"z"'), 'sortedJson should be sorted alphabetically');
142+
assert(unsortedJson.indexOf('"z"') < unsortedJson.indexOf('"a"'), 'unsortedJson should not be sorted alphabetically');
143+
assert(defaultJson.indexOf('"z"') < defaultJson.indexOf('"a"'), 'tojson without sortedkey should not sort keys');
144+
const nestedObj = { b: { y: 1, x: 2 }, a: { z: 1, a: 2 } };
145+
const sortedNestedJson = tojson(nestedObj, "", true, 0, true);
146+
assert(sortedNestedJson.indexOf('"a"') < sortedNestedJson.indexOf('"b"'), 'sortedkey=true should sort top-level keys');
147+
assert(sortedNestedJson.indexOf('"a" :') < sortedNestedJson.indexOf('"z" :'), 'sortedkey=true should sort nested keys');
148+
const objWithBson = {
149+
c: NumberLong(123),
150+
b: ObjectId('0123456789abcdef01234567'),
151+
a: NumberDecimal("1.1")
152+
};
153+
const sortedBsonJson = tojson(objWithBson, "", true, 0, true);
154+
assert(sortedBsonJson.indexOf('"a"') < sortedBsonJson.indexOf('"b"'), 'sortedkey=true should sort keys with BSON types');
155+
assert(sortedBsonJson.indexOf('"b"') < sortedBsonJson.indexOf('"c"'), 'sortedkey=true should sort keys with BSON types');

0 commit comments

Comments
 (0)