18
18
import { scoreValue , getAttr , getAttrNesting , escape_regex , propToArray , iterate , cmp } from './utils.ts' ;
19
19
// @ts -ignore TS2691 "An import path cannot end with a '.ts' extension"
20
20
import { diacriticRegexPoints , asciifold } from './diacritics.ts' ;
21
-
22
-
23
- type TField = {
24
- field : string ,
25
- weight : number ,
26
- }
27
-
28
- type TSort = {
29
- field : string ,
30
- direction ?: string ,
31
- }
32
-
33
- type TOptions = {
34
- fields : TField [ ] ,
35
- sort : TSort [ ] ,
36
- score ?: ( ) => any ,
37
- filter ?: boolean ,
38
- limit ?: number ,
39
- sort_empty ?: TSort [ ] ,
40
- nesting ?: boolean ,
41
- respect_word_boundaries ?: boolean ,
42
- conjunction ?: string ,
43
- }
44
-
45
- type TToken = {
46
- string :string ,
47
- regex :RegExp | null ,
48
- field :string | null ,
49
- }
50
-
51
- type TWeights = { [ key :string ] :number }
52
-
53
- type TPrepareObj = {
54
- options : TOptions ,
55
- query : string ,
56
- tokens : TToken [ ] ,
57
- total : number ,
58
- items : TResultItem [ ] ,
59
- weights : TWeights ,
60
- getAttrFn : ( data :any , field :string ) => any ,
61
-
62
- }
63
-
64
- type TSettings = {
65
- diacritics :boolean
66
- }
67
-
68
- type TResultItem = {
69
- score : number ,
70
- id : number | string ,
71
- }
72
-
73
-
21
+ // @ts -ignore TS2691 "An import path cannot end with a '.ts' extension"
22
+ import * as T from 'types.ts' ;
74
23
75
24
export default class Sifter {
76
25
77
26
public items ; // []|{};
78
- public settings : TSettings ;
27
+ public settings : T . Settings ;
79
28
80
29
/**
81
30
* Textually searches arrays and hashes of objects
82
31
* by property (or multiple properties). Designed
83
32
* specifically for autocomplete.
84
33
*
85
34
*/
86
- constructor ( items :any , settings :TSettings ) {
35
+ constructor ( items :any , settings :T . Settings ) {
87
36
this . items = items ;
88
37
this . settings = settings || { diacritics : true } ;
89
38
} ;
@@ -93,10 +42,10 @@ export default class Sifter{
93
42
* regexps to be used to match results.
94
43
*
95
44
*/
96
- tokenize ( query :string , respect_word_boundaries ?:boolean , weights ?:TWeights ) :TToken [ ] {
45
+ tokenize ( query :string , respect_word_boundaries ?:boolean , weights ?:T . Weights ) :T . Token [ ] {
97
46
if ( ! query || ! query . length ) return [ ] ;
98
47
99
- const tokens :TToken [ ] = [ ] ;
48
+ const tokens :T . Token [ ] = [ ] ;
100
49
const words = query . split ( / \s + / ) ;
101
50
var field_regex :RegExp ;
102
51
@@ -142,12 +91,12 @@ export default class Sifter{
142
91
*
143
92
* @returns {function }
144
93
*/
145
- getScoreFunction ( query :string , options :TOptions ) {
94
+ getScoreFunction ( query :string , options :T . Options ) {
146
95
var search = this . prepareSearch ( query , options ) ;
147
96
return this . _getScoreFunction ( search ) ;
148
97
}
149
98
150
- _getScoreFunction ( search :TPrepareObj ) {
99
+ _getScoreFunction ( search :T . PrepareObj ) {
151
100
const tokens = search . tokens ,
152
101
token_count = tokens . length ;
153
102
@@ -169,21 +118,18 @@ export default class Sifter{
169
118
* Calculates the score of an object
170
119
* against the search query.
171
120
*
172
- * @param {TToken } token
173
- * @param {object } data
174
- * @return {number }
175
121
*/
176
122
const scoreObject = ( function ( ) {
177
123
178
124
179
125
if ( field_count === 1 ) {
180
- return function ( token :TToken , data :{ } ) {
126
+ return function ( token :T . Token , data :{ } ) {
181
127
const field = fields [ 0 ] . field ;
182
128
return scoreValue ( getAttrFn ( data , field ) , token , weights [ field ] ) ;
183
129
} ;
184
130
}
185
131
186
- return function ( token :TToken , data :{ } ) {
132
+ return function ( token :T . Token , data :{ } ) {
187
133
var sum = 0 ;
188
134
189
135
// is the token specific to a field?
@@ -228,7 +174,7 @@ export default class Sifter{
228
174
} else {
229
175
return function ( data :{ } ) {
230
176
var sum = 0 ;
231
- iterate ( tokens , ( token :TToken ) => {
177
+ iterate ( tokens , ( token :T . Token ) => {
232
178
sum += scoreObject ( token , data ) ;
233
179
} ) ;
234
180
return sum / token_count ;
@@ -243,18 +189,18 @@ export default class Sifter{
243
189
*
244
190
* @return function(a,b)
245
191
*/
246
- getSortFunction ( query :string , options :TOptions ) {
192
+ getSortFunction ( query :string , options :T . Options ) {
247
193
var search = this . prepareSearch ( query , options ) ;
248
194
return this . _getSortFunction ( search ) ;
249
195
}
250
196
251
- _getSortFunction ( search :TPrepareObj ) {
197
+ _getSortFunction ( search :T . PrepareObj ) {
252
198
var i , n , implicit_score ;
253
199
254
200
const self = this ,
255
201
options = search . options ,
256
202
sort = ( ! search . query && options . sort_empty ) ? options . sort_empty : options . sort ,
257
- sort_flds :TSort [ ] = [ ] ,
203
+ sort_flds :T . Sort [ ] = [ ] ,
258
204
multipliers :number [ ] = [ ] ;
259
205
260
206
@@ -263,7 +209,7 @@ export default class Sifter{
263
209
* from a search result item.
264
210
*
265
211
*/
266
- const get_field = function ( name :string , result :TResultItem ) :string | number {
212
+ const get_field = function ( name :string , result :T . ResultItem ) :string | number {
267
213
if ( name === '$score' ) return result . score ;
268
214
return search . getAttrFn ( self . items [ result . id ] , name ) ;
269
215
} ;
@@ -310,14 +256,14 @@ export default class Sifter{
310
256
} else if ( sort_flds_count === 1 ) {
311
257
const sort_fld = sort_flds [ 0 ] . field ;
312
258
const multiplier = multipliers [ 0 ] ;
313
- return function ( a :TResultItem , b :TResultItem ) {
259
+ return function ( a :T . ResultItem , b :T . ResultItem ) {
314
260
return multiplier * cmp (
315
261
get_field ( sort_fld , a ) ,
316
262
get_field ( sort_fld , b )
317
263
) ;
318
264
} ;
319
265
} else {
320
- return function ( a :TResultItem , b :TResultItem ) {
266
+ return function ( a :T . ResultItem , b :T . ResultItem ) {
321
267
var i , result , field ;
322
268
for ( i = 0 ; i < sort_flds_count ; i ++ ) {
323
269
field = sort_flds [ i ] . field ;
@@ -338,8 +284,8 @@ export default class Sifter{
338
284
* with results.
339
285
*
340
286
*/
341
- prepareSearch ( query :string , optsUser :TOptions ) : TPrepareObj {
342
- const weights :TWeights = { } ;
287
+ prepareSearch ( query :string , optsUser :T . Options ) : T . PrepareObj {
288
+ const weights :T . Weights = { } ;
343
289
var options = Object . assign ( { } , optsUser ) ;
344
290
345
291
propToArray ( options , 'sort' ) ;
@@ -348,8 +294,8 @@ export default class Sifter{
348
294
// convert fields to new format
349
295
if ( options . fields ) {
350
296
propToArray ( options , 'fields' ) ;
351
- const fields :TField [ ] = [ ] ;
352
- options . fields . forEach ( ( field :string | TField ) => {
297
+ const fields :T . Field [ ] = [ ] ;
298
+ options . fields . forEach ( ( field :string | T . Field ) => {
353
299
if ( typeof field == 'string' ) {
354
300
field = { field :field , weight :1 } ;
355
301
}
@@ -376,8 +322,8 @@ export default class Sifter{
376
322
* Searches through all items and returns a sorted array of matches.
377
323
*
378
324
*/
379
- search ( query :string , options :TOptions ) : TPrepareObj {
380
- var self = this , score , search :TPrepareObj ;
325
+ search ( query :string , options :T . Options ) : T . PrepareObj {
326
+ var self = this , score , search :T . PrepareObj ;
381
327
382
328
search = this . prepareSearch ( query , options ) ;
383
329
options = search . options ;
@@ -388,14 +334,14 @@ export default class Sifter{
388
334
389
335
// perform search and sort
390
336
if ( query . length ) {
391
- iterate ( self . items , ( item :TResultItem , id :string | number ) => {
337
+ iterate ( self . items , ( item :T . ResultItem , id :string | number ) => {
392
338
score = fn_score ( item ) ;
393
339
if ( options . filter === false || score > 0 ) {
394
340
search . items . push ( { 'score' : score , 'id' : id } ) ;
395
341
}
396
342
} ) ;
397
343
} else {
398
- iterate ( self . items , ( item :TResultItem , id :string | number ) => {
344
+ iterate ( self . items , ( item :T . ResultItem , id :string | number ) => {
399
345
search . items . push ( { 'score' : 1 , 'id' : id } ) ;
400
346
} ) ;
401
347
}
0 commit comments