@@ -209,7 +209,6 @@ function getBSONType(value: any): SchemaBSONType {
209
209
const bsonType = value ?. _bsontype
210
210
? value . _bsontype
211
211
: Object . prototype . toString . call ( value ) . replace ( / ^ \[ o b j e c t ( \w + ) \] $ / , '$1' ) ;
212
-
213
212
if ( bsonType === 'Object' ) {
214
213
// In the resulting schema we rename `Object` to `Document`.
215
214
return 'Document' ;
@@ -324,10 +323,30 @@ function simplifiedSchema(fields: SchemaAnalysisFieldsMap): SimplifiedSchema {
324
323
return finalizeDocumentFieldSchema ( fields ) ;
325
324
}
326
325
327
- function cropStringAt10kCharacters ( value : string ) {
328
- return value . charCodeAt ( 10000 - 1 ) === value . codePointAt ( 10000 - 1 )
329
- ? value . slice ( 0 , 10000 )
330
- : value . slice ( 0 , 10000 - 1 ) ;
326
+ function cropString ( value : string , limit : number ) {
327
+ if ( limit < 1 ) return '' ;
328
+ return value . charCodeAt ( limit - 1 ) === value . codePointAt ( 10000 - 1 )
329
+ ? value . slice ( 0 , limit )
330
+ : value . slice ( 0 , limit - 1 ) ;
331
+ }
332
+
333
+ function getCappedValue ( bsonType : SchemaBSONType , value : BSONValue ) {
334
+ if ( bsonType === 'String' ) {
335
+ return cropString ( value as string , 10000 ) ;
336
+ }
337
+ if ( bsonType === 'Binary' ) {
338
+ value = value as Binary ;
339
+ return value . buffer . length > 10000
340
+ ? new Binary ( value . buffer . slice ( 0 , 10000 ) , value . sub_type )
341
+ : value ;
342
+ }
343
+ if ( bsonType === 'Code' ) {
344
+ value = value as Code ;
345
+ return ( value . code . length >= 10000 )
346
+ ? new Code ( cropString ( value . code , 10000 ) , value . scope )
347
+ : value ;
348
+ }
349
+ return value ;
331
350
}
332
351
333
352
function computeHasDuplicatesForType ( type : SchemaAnalysisType , unique ?: number ) {
@@ -525,12 +544,12 @@ export class SchemaAnalyzer {
525
544
} else if ( this . options . storeValues && ! isNullType ( type ) ) {
526
545
// When the `storeValues` option is enabled, store some example values.
527
546
if ( ! type . values ) {
528
- type . values = bsonType === 'String'
547
+ type . values = [ 'String' , 'Binary' , 'Code' ] . includes ( bsonType )
529
548
? Reservoir ( 100 ) : Reservoir ( 10000 ) ;
530
549
}
531
550
532
551
type . values . pushSome (
533
- type . name === 'String' ? cropStringAt10kCharacters ( value as string ) : value
552
+ getCappedValue ( type . bsonType , value )
534
553
) ;
535
554
}
536
555
} ;
0 commit comments