Skip to content

Commit aafc981

Browse files
committed
Address code review feedback, round 1.
1 parent 5ba70ef commit aafc981

File tree

3 files changed

+37
-37
lines changed

3 files changed

+37
-37
lines changed

package.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,5 @@
128128
"hooks": {
129129
"pre-push": "node tools/gitHooks/prepush.js"
130130
}
131-
},
132-
"dependencies": {
133-
"@types/json-stable-stringify": "^1.0.32",
134-
"json-stable-stringify": "^1.0.1"
135131
}
136132
}

packages/firestore/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
"@firebase/app-types": "0.x"
5050
},
5151
"devDependencies": {
52+
"@types/json-stable-stringify": "^1.0.32",
53+
"json-stable-stringify": "^1.0.1",
5254
"protobufjs": "6.8.9",
5355
"rollup": "2.0.6",
5456
"rollup-plugin-copy-assets": "1.1.0",

packages/firestore/test/unit/specs/describe_spec.ts

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { ExclusiveTestFunction, PendingTestFunction } from 'mocha';
1919

2020
import { IndexedDbPersistence } from '../../../src/local/indexeddb_persistence';
2121
import { assert } from '../../../src/util/assert';
22+
import { primitiveComparator } from '../../../src/util/misc';
2223
import { addEqualityMatcher } from '../../util/equality_matcher';
2324

2425
import { SpecBuilder } from './spec_builder';
@@ -247,7 +248,10 @@ export function describeSpec(
247248
// Note: We use json-stable-stringify instead of JSON.stringify() to ensure
248249
// that the generated JSON does not produce diffs merely due to the order
249250
// of the keys in an object changing.
250-
const output = stringify(specsInThisTest, { space: 2, cmp: stringifyCmp });
251+
const output = stringify(specsInThisTest, {
252+
space: 2,
253+
cmp: stringifyComparator
254+
});
251255
writeJSONFile(output);
252256
}
253257
}
@@ -258,6 +262,7 @@ export function describeSpec(
258262
* ordered in the generated JSON in the same relative order of this array.
259263
*/
260264
const stringifyCustomOrdering = [
265+
'comment',
261266
'describeName',
262267
'itName',
263268
'tags',
@@ -266,42 +271,39 @@ const stringifyCustomOrdering = [
266271
];
267272

268273
/**
269-
* A comparator function for stringify() that sorts keys in the JSON output.
274+
* Assigns a "group number" to the given key in the generated JSON.
270275
*
271-
* In order to produce semi-readable JSON that has an intuitive top-level key
272-
* arrangement, some keys are sorted non-alphabetically; namely, the ordering
273-
* defined in `stringifyCustomOrdering` is used, falling back to alphabetical
274-
* ordering if one or both of the key are not overridden.
276+
* Keys with lower group numbers should be ordered before keys with greater
277+
* group numbers. Keys with equal group numbers should be ordered
278+
* alphabetically.
275279
*/
276-
function stringifyCmp(a: stringify.Element, b: stringify.Element): number {
277-
// If the keys are equal, then avoid the costly computations below.
278-
if (a.key === b.key) {
279-
return 0;
280-
}
281-
282-
// If the keys have a custom ordering, then use it.
283-
const aIndex = stringifyCustomOrdering.indexOf(a.key);
284-
const bIndex = stringifyCustomOrdering.indexOf(b.key);
285-
if (aIndex >= 0 && bIndex >= 0) {
286-
return aIndex - bIndex;
287-
}
288-
289-
// Order "expected" blocks after other blocks since the expectations logically
290-
// occur after the steps are executed.
291-
if (a.key.startsWith('expected')) {
292-
if (!b.key.startsWith('expected')) {
293-
return 1;
294-
}
295-
} else if (b.key.startsWith('expected')) {
296-
return -1;
280+
function stringifyGroup(s: string): number {
281+
const index = stringifyCustomOrdering.indexOf(s);
282+
if (index >= 0) {
283+
return index;
284+
} else if (!s.startsWith('expected')) {
285+
return stringifyCustomOrdering.length;
286+
} else {
287+
return stringifyCustomOrdering.length + 1;
297288
}
289+
}
298290

299-
// If all else fails, order alphabetically.
300-
if (a.key < b.key) {
301-
return -1;
302-
} else if (a.key > b.key) {
303-
return 1;
291+
/**
292+
* A comparator function for stringify() that sorts keys in the JSON output.
293+
*
294+
* In order to produce JSON that has somewhat-intuitively-ordered keys, this
295+
* comparator intentionally deviates from pure alphabetic ordering, placing
296+
* some logically-first keys before others.
297+
*/
298+
function stringifyComparator(
299+
a: stringify.Element,
300+
b: stringify.Element
301+
): number {
302+
const aGroup = stringifyGroup(a.key);
303+
const bGroup = stringifyGroup(b.key);
304+
if (aGroup === bGroup) {
305+
return primitiveComparator(a.key, b.key);
304306
} else {
305-
return 0;
307+
return primitiveComparator(aGroup, bGroup);
306308
}
307309
}

0 commit comments

Comments
 (0)