Skip to content

Commit 376d199

Browse files
authored
feat: update simplified schema to sort the types by the most frequent first COMPASS-7139 (#209)
1 parent ac1616d commit 376d199

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

src/schema-analyzer.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,12 @@ export type SimplifiedSchema = {
293293

294294
function simplifiedSchema(fields: SchemaAnalysisFieldsMap): SimplifiedSchema {
295295
function finalizeSchemaFieldTypes(types: SchemaAnalysisFieldTypes): SimplifiedSchemaType[] {
296-
return Object.values(types).map((type) => {
296+
return Object.values(types).sort(
297+
(a: SchemaAnalysisType, b: SchemaAnalysisType) => {
298+
// Sort the types by what occurs most frequent first.
299+
return b.count - a.count;
300+
}
301+
).map((type: SchemaAnalysisType) => {
297302
return {
298303
bsonType: type.bsonType, // Note: `Object` is replaced with `Document`.
299304
...(isArrayType(type) ? {

test/simplified-schema.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import { allBSONTypesDoc } from './all-bson-types-fixture';
77
const docsFixture = [
88
{
99
foo: 1,
10-
bar: 'test'
10+
bar: 25
1111
},
1212
{
1313
foo: 2,
14-
bar: 25,
14+
bar: 'test',
1515
baz: true
1616
},
1717
{

0 commit comments

Comments
 (0)