Skip to content

Commit 6e1b1af

Browse files
STREAMS-1991: Add sortedKey param to tojson function
1 parent a991338 commit 6e1b1af

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
}
@@ -713,7 +713,7 @@ tojsononeline = function(x) {
713713
return tojson(x, " ", true);
714714
};
715715

716-
tojson = function(x, indent, nolint, depth) {
716+
tojson = function(x, indent, nolint, depth, sortKeys) {
717717
if (x === null)
718718
return "null";
719719

@@ -734,7 +734,7 @@ tojson = function(x, indent, nolint, depth) {
734734
case "boolean":
735735
return "" + x;
736736
case "object": {
737-
var s = tojsonObject(x, indent, nolint, depth);
737+
var s = tojsonObject(x, indent, nolint, depth, sortKeys);
738738
if ((nolint == null || nolint == true) && s.length < 80 &&
739739
(indent == null || indent.length == 0)) {
740740
s = s.replace(/[\t\r\n]+/gm, " ");
@@ -751,10 +751,13 @@ tojson = function(x, indent, nolint, depth) {
751751
};
752752
tojson.MAX_DEPTH = 100;
753753

754-
tojsonObject = function(x, indent, nolint, depth) {
754+
tojsonObject = function(x, indent, nolint, depth, sortKeys) {
755755
if (typeof depth !== 'number') {
756756
depth = 0;
757757
}
758+
if (typeof sortKeys !== 'boolean') {
759+
sortKeys = false;
760+
}
758761
var lineEnding = nolint ? " " : "\n";
759762
var tabSpace = nolint ? "" : "\t";
760763
if (typeof x !== "object") {
@@ -765,12 +768,12 @@ tojsonObject = function(x, indent, nolint, depth) {
765768
indent = "";
766769

767770
if (typeof (x.tojson) == "function" && x.tojson != tojson) {
768-
return x.tojson(indent, nolint, depth);
771+
return x.tojson(indent, nolint, depth, sortKeys);
769772
}
770773

771774
if (x.constructor && typeof (x.constructor.tojson) == "function" &&
772775
x.constructor.tojson != tojson) {
773-
return x.constructor.tojson(x, indent, nolint, depth);
776+
return x.constructor.tojson(x, indent, nolint, depth, sortKeys);
774777
}
775778

776779
if (x instanceof Error) {
@@ -796,8 +799,14 @@ tojsonObject = function(x, indent, nolint, depth) {
796799
var keys = x;
797800
if (typeof (x._simpleKeys) == "function")
798801
keys = x._simpleKeys();
799-
var fieldStrings = [];
802+
var keyNames = [];
800803
for (var k in keys) {
804+
keyNames.push(k);
805+
}
806+
if (sortKeys) keyNames.sort();
807+
808+
var fieldStrings = [];
809+
for (var k of keyNames) {
801810
var val = x[k];
802811

803812
// skip internal DB types to avoid issues with interceptors
@@ -806,7 +815,7 @@ tojsonObject = function(x, indent, nolint, depth) {
806815
if (typeof DBCollection != 'undefined' && val == DBCollection.prototype)
807816
continue;
808817

809-
fieldStrings.push(indent + "\"" + k + "\" : " + tojson(val, indent, nolint, depth + 1));
818+
fieldStrings.push(indent + "\"" + k + "\" : " + tojson(val, indent, nolint, depth + 1, sortKeys));
810819
}
811820

812821
if (fieldStrings.length > 0) {

snippets/mongocompat/test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,25 @@ try {
113113
}
114114
assert.strictEqual(typeof tojsonObject({ key: "value" }), 'string');
115115
assert.strictEqual(typeof tojsonObject([1, 2, 3]), 'string');
116+
117+
// Test sortedkey parameter
118+
const unsortedObj = { z: 1, a: 2, m: 3 };
119+
const sortedJson = tojson(unsortedObj, "", true, 0, true);
120+
const unsortedJson = tojson(unsortedObj, "", true, 0, false);
121+
const defaultJson = tojson(unsortedObj);
122+
assert(sortedJson.indexOf('"a"') < sortedJson.indexOf('"m"'), 'sortedJson should be sorted alphabetically');
123+
assert(sortedJson.indexOf('"m"') < sortedJson.indexOf('"z"'), 'sortedJson should be sorted alphabetically');
124+
assert(unsortedJson.indexOf('"z"') < unsortedJson.indexOf('"a"'), 'unsortedJson should not be sorted alphabetically');
125+
assert(defaultJson.indexOf('"z"') < defaultJson.indexOf('"a"'), 'tojson without sortedkey should not sort keys');
126+
const nestedObj = { b: { y: 1, x: 2 }, a: { z: 1, a: 2 } };
127+
const sortedNestedJson = tojson(nestedObj, "", true, 0, true);
128+
assert(sortedNestedJson.indexOf('"a"') < sortedNestedJson.indexOf('"b"'), 'sortedkey=true should sort top-level keys');
129+
assert(sortedNestedJson.indexOf('"a" :') < sortedNestedJson.indexOf('"z" :'), 'sortedkey=true should sort nested keys');
130+
const objWithBson = {
131+
c: NumberLong(123),
132+
b: ObjectId('0123456789abcdef01234567'),
133+
a: NumberDecimal("1.1")
134+
};
135+
const sortedBsonJson = tojson(objWithBson, "", true, 0, true);
136+
assert(sortedBsonJson.indexOf('"a"') < sortedBsonJson.indexOf('"b"'), 'sortedkey=true should sort keys with BSON types');
137+
assert(sortedBsonJson.indexOf('"b"') < sortedBsonJson.indexOf('"c"'), 'sortedkey=true should sort keys with BSON types');

0 commit comments

Comments
 (0)