@@ -86,6 +86,30 @@ describe('MiniSearch', () => {
86
86
expect ( tokenize ) . toHaveBeenCalledWith ( 'true' , 'isBlinky' )
87
87
} )
88
88
89
+ it ( 'turns the field to string before tokenization using a custom stringifyField function, if given' , ( ) => {
90
+ const tokenize = jest . fn ( x => x . split ( / \W + / ) )
91
+ const stringifyField = jest . fn ( ( value , fieldName ) => {
92
+ if ( fieldName === 'tags' ) {
93
+ return value . join ( '|' )
94
+ } else if ( typeof value === 'boolean' ) {
95
+ return value ? 'T' : 'F'
96
+ }
97
+ return value . toString ( )
98
+ } )
99
+ const ms = new MiniSearch ( { fields : [ 'id' , 'tags' , 'isBlinky' ] , tokenize, stringifyField } )
100
+ expect ( ( ) => {
101
+ ms . add ( { id : 123 , tags : [ 'foo' , 'bar' ] , isBlinky : false } )
102
+ ms . add ( { id : 321 , isBlinky : true } )
103
+ } ) . not . toThrowError ( )
104
+
105
+ expect ( tokenize ) . toHaveBeenCalledWith ( '123' , 'id' )
106
+ expect ( tokenize ) . toHaveBeenCalledWith ( 'foo|bar' , 'tags' )
107
+ expect ( tokenize ) . toHaveBeenCalledWith ( 'F' , 'isBlinky' )
108
+
109
+ expect ( tokenize ) . toHaveBeenCalledWith ( '321' , 'id' )
110
+ expect ( tokenize ) . toHaveBeenCalledWith ( 'T' , 'isBlinky' )
111
+ } )
112
+
89
113
it ( 'passes document and field name to the field extractor' , ( ) => {
90
114
const extractField = jest . fn ( ( document , fieldName ) => {
91
115
if ( fieldName === 'pubDate' ) {
@@ -290,39 +314,47 @@ describe('MiniSearch', () => {
290
314
expect ( ms . search ( 'bar' ) ) . toHaveLength ( 0 )
291
315
} )
292
316
293
- describe ( 'when using custom per-field extraction/tokenizer/processing' , ( ) => {
317
+ describe ( 'when using custom per-field extraction/stringification/ tokenizer/processing' , ( ) => {
294
318
const documents = [
295
- { id : 1 , title : 'Divina Commedia' , tags : 'dante, virgilio' , author : { name : 'Dante Alighieri' } } ,
296
- { id : 2 , title : 'I Promessi Sposi' , tags : 'renzo, lucia' , author : { name : 'Alessandro Manzoni' } } ,
297
- { id : 3 , title : 'Vita Nova' , author : { name : 'Dante Alighieri' } }
319
+ { id : 1 , title : 'Divina Commedia' , tags : [ 'dante' , ' virgilio'] , author : { name : 'Dante Alighieri' } , available : true } ,
320
+ { id : 2 , title : 'I Promessi Sposi' , tags : [ 'renzo' , ' lucia'] , author : { name : 'Alessandro Manzoni' } , available : false } ,
321
+ { id : 3 , title : 'Vita Nova' , tags : [ 'dante' ] , author : { name : 'Dante Alighieri' } , available : true }
298
322
]
323
+ const options = {
324
+ fields : [ 'title' , 'tags' , 'authorName' , 'available' ] ,
325
+ extractField : ( doc , fieldName ) => {
326
+ if ( fieldName === 'authorName' ) {
327
+ return doc . author . name
328
+ } else {
329
+ return doc [ fieldName ]
330
+ }
331
+ } ,
332
+ stringifyField : ( fieldValue , fieldName ) => {
333
+ if ( fieldName === 'available' ) {
334
+ return fieldValue ? 'yes' : 'no'
335
+ } else {
336
+ return fieldValue . toString ( )
337
+ }
338
+ } ,
339
+ tokenize : ( field , fieldName ) => {
340
+ if ( fieldName === 'tags' ) {
341
+ return field . split ( ',' )
342
+ } else {
343
+ return field . split ( / \s + / )
344
+ }
345
+ } ,
346
+ processTerm : ( term , fieldName ) => {
347
+ if ( fieldName === 'tags' ) {
348
+ return term . toUpperCase ( )
349
+ } else {
350
+ return term . toLowerCase ( )
351
+ }
352
+ }
353
+ }
299
354
300
355
let ms , _warn
301
356
beforeEach ( ( ) => {
302
- ms = new MiniSearch ( {
303
- fields : [ 'title' , 'tags' , 'authorName' ] ,
304
- extractField : ( doc , fieldName ) => {
305
- if ( fieldName === 'authorName' ) {
306
- return doc . author . name
307
- } else {
308
- return doc [ fieldName ]
309
- }
310
- } ,
311
- tokenize : ( field , fieldName ) => {
312
- if ( fieldName === 'tags' ) {
313
- return field . split ( ',' )
314
- } else {
315
- return field . split ( / \s + / )
316
- }
317
- } ,
318
- processTerm : ( term , fieldName ) => {
319
- if ( fieldName === 'tags' ) {
320
- return term . toUpperCase ( )
321
- } else {
322
- return term . toLowerCase ( )
323
- }
324
- }
325
- } )
357
+ ms = new MiniSearch ( options )
326
358
ms . addAll ( documents )
327
359
_warn = console . warn
328
360
console . warn = jest . fn ( )
@@ -332,12 +364,20 @@ describe('MiniSearch', () => {
332
364
console . warn = _warn
333
365
} )
334
366
335
- it ( 'removes the document from the index' , ( ) => {
367
+ it ( 'removes the document and its terms from the index' , ( ) => {
336
368
expect ( ms . documentCount ) . toEqual ( 3 )
369
+ expect ( ms . search ( 'commedia' ) . map ( ( { id } ) => id ) ) . toEqual ( [ 1 ] )
370
+ expect ( ms . search ( 'DANTE' ) . map ( ( { id } ) => id ) ) . toEqual ( [ 1 , 3 ] )
371
+ expect ( ms . search ( 'vita' ) . map ( ( { id } ) => id ) ) . toEqual ( [ 3 ] )
372
+ expect ( ms . search ( 'yes' ) . map ( ( { id } ) => id ) ) . toEqual ( [ 1 , 3 ] )
373
+
337
374
ms . remove ( documents [ 0 ] )
375
+
338
376
expect ( ms . documentCount ) . toEqual ( 2 )
339
- expect ( ms . search ( 'commedia' ) . length ) . toEqual ( 0 )
377
+ expect ( ms . search ( 'commedia' ) . map ( ( { id } ) => id ) ) . toEqual ( [ ] )
378
+ expect ( ms . search ( 'DANTE' ) . map ( ( { id } ) => id ) ) . toEqual ( [ 3 ] )
340
379
expect ( ms . search ( 'vita' ) . map ( ( { id } ) => id ) ) . toEqual ( [ 3 ] )
380
+ expect ( ms . search ( 'yes' ) . map ( ( { id } ) => id ) ) . toEqual ( [ 3 ] )
341
381
expect ( console . warn ) . not . toHaveBeenCalled ( )
342
382
} )
343
383
} )
0 commit comments