Skip to content

Commit 0bf7696

Browse files
authored
feat: sort paths from getSchemaPaths (#194)
1 parent 1736684 commit 0bf7696

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

src/schema-analyzer.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,11 @@ function extractStringValueFromBSON(value: any): string {
183183
return String(value);
184184
}
185185

186-
function fieldComparator(a: SchemaField, b: SchemaField) {
186+
function fieldComparator(a: {
187+
name: string
188+
}, b: {
189+
name: string
190+
}) {
187191
// Make sure _id is always at top, even in presence of uppercase fields.
188192
const aName = a.name;
189193
const bName = b.name;
@@ -233,8 +237,9 @@ function schemaToPaths(
233237
parent: string[] = []
234238
): string[][] {
235239
const paths: string[][] = [];
240+
const sortedFields = Object.values(fields).sort(fieldComparator);
236241

237-
for (const field of Object.values(fields)) {
242+
for (const field of sortedFields) {
238243
const path = [...parent, field.name];
239244
paths.push(path);
240245

test/get-schema-paths.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ describe('getSchemaPaths', function() {
5959
schemaPaths = await getSchemaPaths(docs);
6060
});
6161

62-
it('returns all of the field paths', function() {
62+
it('returns all of the field paths (sorted)', function() {
6363
assert.deepEqual(schemaPaths, [
64+
['clementine'],
6465
['pineapple'],
6566
['pineapple', 'orange'],
66-
['pineapple', 'orange', 'apple'],
67-
['clementine']
67+
['pineapple', 'orange', 'apple']
6868
]);
6969
});
7070
});
@@ -86,13 +86,13 @@ describe('getSchemaPaths', function() {
8686
schemaPaths = await getSchemaPaths(docs);
8787
});
8888

89-
it('returns all of the field paths', function() {
89+
it('returns all of the field paths (sorted)', function() {
9090
assert.deepEqual(schemaPaths, [
9191
['orangutan'],
92-
['orangutan', 'tuatara'],
9392
['orangutan', 'lizard'],
93+
['orangutan', 'lizard', 'birds'],
9494
['orangutan', 'lizard', 'snakes'],
95-
['orangutan', 'lizard', 'birds']
95+
['orangutan', 'tuatara']
9696
]);
9797
});
9898
});

0 commit comments

Comments
 (0)