Skip to content

Commit a7ba01a

Browse files
committed
move types to lib/types.ts
1 parent 1652ef9 commit a7ba01a

File tree

3 files changed

+77
-81
lines changed

3 files changed

+77
-81
lines changed

lib/sifter.ts

Lines changed: 25 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -18,72 +18,21 @@
1818
import { scoreValue, getAttr, getAttrNesting, escape_regex, propToArray, iterate, cmp } from './utils.ts';
1919
// @ts-ignore TS2691 "An import path cannot end with a '.ts' extension"
2020
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';
7423

7524
export default class Sifter{
7625

7726
public items; // []|{};
78-
public settings: TSettings;
27+
public settings: T.Settings;
7928

8029
/**
8130
* Textually searches arrays and hashes of objects
8231
* by property (or multiple properties). Designed
8332
* specifically for autocomplete.
8433
*
8534
*/
86-
constructor(items:any, settings:TSettings) {
35+
constructor(items:any, settings:T.Settings) {
8736
this.items = items;
8837
this.settings = settings || {diacritics: true};
8938
};
@@ -93,10 +42,10 @@ export default class Sifter{
9342
* regexps to be used to match results.
9443
*
9544
*/
96-
tokenize(query:string, respect_word_boundaries?:boolean, weights?:TWeights ):TToken[] {
45+
tokenize(query:string, respect_word_boundaries?:boolean, weights?:T.Weights ):T.Token[] {
9746
if (!query || !query.length) return [];
9847

99-
const tokens:TToken[] = [];
48+
const tokens:T.Token[] = [];
10049
const words = query.split(/\s+/);
10150
var field_regex:RegExp;
10251

@@ -142,12 +91,12 @@ export default class Sifter{
14291
*
14392
* @returns {function}
14493
*/
145-
getScoreFunction(query:string, options:TOptions ){
94+
getScoreFunction(query:string, options:T.Options ){
14695
var search = this.prepareSearch(query, options);
14796
return this._getScoreFunction(search);
14897
}
14998

150-
_getScoreFunction(search:TPrepareObj ){
99+
_getScoreFunction(search:T.PrepareObj ){
151100
const tokens = search.tokens,
152101
token_count = tokens.length;
153102

@@ -169,21 +118,18 @@ export default class Sifter{
169118
* Calculates the score of an object
170119
* against the search query.
171120
*
172-
* @param {TToken} token
173-
* @param {object} data
174-
* @return {number}
175121
*/
176122
const scoreObject = (function() {
177123

178124

179125
if (field_count === 1) {
180-
return function(token:TToken, data:{}) {
126+
return function(token:T.Token, data:{}) {
181127
const field = fields[0].field;
182128
return scoreValue(getAttrFn(data, field), token, weights[field]);
183129
};
184130
}
185131

186-
return function(token:TToken, data:{}) {
132+
return function(token:T.Token, data:{}) {
187133
var sum = 0;
188134

189135
// is the token specific to a field?
@@ -228,7 +174,7 @@ export default class Sifter{
228174
} else {
229175
return function(data:{}) {
230176
var sum = 0;
231-
iterate(tokens,(token:TToken)=>{
177+
iterate(tokens,(token:T.Token)=>{
232178
sum += scoreObject(token, data);
233179
});
234180
return sum / token_count;
@@ -243,18 +189,18 @@ export default class Sifter{
243189
*
244190
* @return function(a,b)
245191
*/
246-
getSortFunction(query:string, options:TOptions) {
192+
getSortFunction(query:string, options:T.Options) {
247193
var search = this.prepareSearch(query, options);
248194
return this._getSortFunction(search);
249195
}
250196

251-
_getSortFunction(search:TPrepareObj){
197+
_getSortFunction(search:T.PrepareObj){
252198
var i, n, implicit_score;
253199

254200
const self = this,
255201
options = search.options,
256202
sort = (!search.query && options.sort_empty) ? options.sort_empty : options.sort,
257-
sort_flds:TSort[] = [],
203+
sort_flds:T.Sort[] = [],
258204
multipliers:number[] = [];
259205

260206

@@ -263,7 +209,7 @@ export default class Sifter{
263209
* from a search result item.
264210
*
265211
*/
266-
const get_field = function(name:string, result:TResultItem):string|number {
212+
const get_field = function(name:string, result:T.ResultItem):string|number {
267213
if (name === '$score') return result.score;
268214
return search.getAttrFn(self.items[result.id], name);
269215
};
@@ -310,14 +256,14 @@ export default class Sifter{
310256
} else if (sort_flds_count === 1) {
311257
const sort_fld = sort_flds[0].field;
312258
const multiplier = multipliers[0];
313-
return function(a:TResultItem, b:TResultItem) {
259+
return function(a:T.ResultItem, b:T.ResultItem) {
314260
return multiplier * cmp(
315261
get_field(sort_fld, a),
316262
get_field(sort_fld, b)
317263
);
318264
};
319265
} else {
320-
return function(a:TResultItem, b:TResultItem) {
266+
return function(a:T.ResultItem, b:T.ResultItem) {
321267
var i, result, field;
322268
for (i = 0; i < sort_flds_count; i++) {
323269
field = sort_flds[i].field;
@@ -338,8 +284,8 @@ export default class Sifter{
338284
* with results.
339285
*
340286
*/
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 = {};
343289
var options = Object.assign({},optsUser);
344290

345291
propToArray(options,'sort');
@@ -348,8 +294,8 @@ export default class Sifter{
348294
// convert fields to new format
349295
if( options.fields ){
350296
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) => {
353299
if( typeof field == 'string' ){
354300
field = {field:field,weight:1};
355301
}
@@ -376,8 +322,8 @@ export default class Sifter{
376322
* Searches through all items and returns a sorted array of matches.
377323
*
378324
*/
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;
381327

382328
search = this.prepareSearch(query, options);
383329
options = search.options;
@@ -388,14 +334,14 @@ export default class Sifter{
388334

389335
// perform search and sort
390336
if (query.length) {
391-
iterate(self.items, (item:TResultItem, id:string|number) => {
337+
iterate(self.items, (item:T.ResultItem, id:string|number) => {
392338
score = fn_score(item);
393339
if (options.filter === false || score > 0) {
394340
search.items.push({'score': score, 'id': id});
395341
}
396342
});
397343
} else {
398-
iterate(self.items, (item:TResultItem, id:string|number) => {
344+
iterate(self.items, (item:T.ResultItem, id:string|number) => {
399345
search.items.push({'score': 1, 'id': id});
400346
});
401347
}

lib/types.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
export type Field = {
3+
field: string,
4+
weight: number,
5+
}
6+
7+
export type Sort = {
8+
field: string,
9+
direction?: string,
10+
}
11+
12+
export type Options = {
13+
fields: Field[],
14+
sort: Sort[],
15+
score?: ()=>any,
16+
filter?: boolean,
17+
limit?: number,
18+
sort_empty?: Sort[],
19+
nesting?: boolean,
20+
respect_word_boundaries?: boolean,
21+
conjunction?: string,
22+
}
23+
24+
export type Token = {
25+
string:string,
26+
regex:RegExp|null,
27+
field:string|null,
28+
}
29+
30+
export type Weights = {[key:string]:number}
31+
32+
export type PrepareObj = {
33+
options: Options,
34+
query: string,
35+
tokens: Token[],
36+
total: number,
37+
items: ResultItem[],
38+
weights: Weights,
39+
getAttrFn: (data:any,field:string)=>any,
40+
41+
}
42+
43+
export type Settings = {
44+
diacritics:boolean
45+
}
46+
47+
export type ResultItem = {
48+
score: number,
49+
id: number|string,
50+
}

lib/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { asciifold } from './diacritics.ts';
44

55
// @ts-ignore TS2691 "An import path cannot end with a '.ts' extension"
6-
import { TToken } from './sifter.ts';
6+
import * as T from './types.ts';
77

88

99
/**
@@ -35,7 +35,7 @@ export function getAttrNesting(obj:{[key:string]:any}, name:string ) {
3535
* given value is against a search token.
3636
*
3737
*/
38-
export function scoreValue(value:string, token:TToken, weight:number ):number {
38+
export function scoreValue(value:string, token:T.Token, weight:number ):number {
3939
var score, pos;
4040

4141
if (!value) return 0;

0 commit comments

Comments
 (0)