Skip to content

Commit

Permalink
fix: use prototypeless objects for field-to-type mappings
Browse files Browse the repository at this point in the history
Fixes: #211
  • Loading branch information
addaleax committed Dec 15, 2023
1 parent ac1616d commit a6b79ca
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/schema-analyzer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

import Reservoir from 'reservoir';
import type {
import {
Document,
ObjectId,
MinKey,
Expand Down Expand Up @@ -432,7 +432,7 @@ export class SchemaAnalyzer {
options: SchemaParseOptions;
documentsAnalyzed = 0;
schemaAnalysisRoot: SchemaAnalysisRoot = {
fields: {},
fields: Object.create(null),
count: 0
};

Expand Down Expand Up @@ -513,7 +513,7 @@ export class SchemaAnalyzer {
(value as BSONValue[]).forEach((v: BSONValue) => addToType(path, v, type.types));
} else if (isDocumentType(type)) {
// Recurse into nested documents by calling `addToField` for all sub-fields.
type.fields = type.fields ?? {};
type.fields = type.fields ?? Object.create(null);
Object.entries(value as Document).forEach(
([fieldName, v]) => addToField(fieldName, [...path, fieldName], v, type.fields)
);
Expand Down
7 changes: 5 additions & 2 deletions test/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ describe('using only basic fields', function() {
last_address_longitude: null,
created_at: new Date(),
length: 29,
'name[]': 'Annabeth Frankie'
'name[]': 'Annabeth Frankie',
toString: 42
}
];

Expand All @@ -38,7 +39,8 @@ describe('using only basic fields', function() {
'name',
'stats_friends',
'twitter_username',
'name[]'
'name[]',
'toString'
];
assert.deepEqual(users.fields.map(v => v.name).sort(), fieldNames.sort());
});
Expand All @@ -59,5 +61,6 @@ describe('using only basic fields', function() {
assert.equal(users.fields.find(v => v.name === 'name')?.type, 'String');
assert.equal(users.fields.find(v => v.name === 'stats_friends')?.type, 'Number');
assert.equal(users.fields.find(v => v.name === 'twitter_username')?.type, 'String');
assert.equal(users.fields.find(v => v.name === 'toString')?.type, 'Number');
});
});
5 changes: 3 additions & 2 deletions test/mixed-type-nested.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ describe('mixed types nested', function() {
{
_id: 5,
address: {
valid: true
valid: true,
toString: { value: false }
}
}
];
] as const;

let schema: Schema;
let valid: SchemaField | undefined;
Expand Down

0 comments on commit a6b79ca

Please sign in to comment.