forked from brianreavis/sifter.js
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
216 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
declare type TDiacraticList = { | ||
[key: string]: string; | ||
}; | ||
export declare const DIACRITICS: TDiacraticList; | ||
/** | ||
* Remove accents | ||
* via https://github.com/krisk/Fuse/issues/133#issuecomment-318692703 | ||
* | ||
*/ | ||
export declare const asciifold: (str: string) => string; | ||
/** | ||
* Generate a list of diacritics from the list of code points | ||
* | ||
*/ | ||
export declare const generateDiacritics: () => TDiacraticList; | ||
export declare const diacriticRegexPoints: (regex: string) => string; | ||
export {}; | ||
/** | ||
* Expand a regular expression pattern to include diacritics | ||
* eg /a/ becomes /aⓐaẚàáâầấẫẩãāăằắẵẳȧǡäǟảåǻǎȁȃạậặḁąⱥɐɑAⒶAÀÁÂẦẤẪẨÃĀĂẰẮẴẲȦǠÄǞẢÅǺǍȀȂẠẬẶḀĄȺⱯ/ | ||
* | ||
* rollup will bundle this function (and the DIACRITICS constant) unless commented out | ||
* | ||
var diacriticRegex = (function() { | ||
var list = []; | ||
for( let letter in DIACRITICS ){ | ||
if( letter.toLowerCase() != letter && letter.toLowerCase() in DIACRITICS ){ | ||
continue; | ||
} | ||
if( DIACRITICS.hasOwnProperty(letter) ){ | ||
var replace = letter + DIACRITICS[letter]; | ||
if( letter.toUpperCase() in DIACRITICS ){ | ||
replace += letter.toUpperCase() + DIACRITICS[letter.toUpperCase()]; | ||
} | ||
list.push({let:letter,pat:'['+replace+']'}); | ||
} | ||
} | ||
return function(regex:string):string{ | ||
list.forEach((item)=>{ | ||
regex = regex.replace( new RegExp(item.let,'g'),item.pat); | ||
}); | ||
return regex; | ||
} | ||
})(); | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/** | ||
* sifter.js | ||
* Copyright (c) 2013–2020 Brian Reavis & contributors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this | ||
* file except in compliance with the License. You may obtain a copy of the License at: | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF | ||
* ANY KIND, either express or implied. See the License for the specific language | ||
* governing permissions and limitations under the License. | ||
* | ||
* @author Brian Reavis <[email protected]> | ||
*/ | ||
import * as T from 'types.ts'; | ||
export default class Sifter { | ||
items: any; | ||
settings: T.Settings; | ||
/** | ||
* Textually searches arrays and hashes of objects | ||
* by property (or multiple properties). Designed | ||
* specifically for autocomplete. | ||
* | ||
*/ | ||
constructor(items: any, settings: T.Settings); | ||
/** | ||
* Splits a search string into an array of individual | ||
* regexps to be used to match results. | ||
* | ||
*/ | ||
tokenize(query: string, respect_word_boundaries?: boolean, weights?: T.Weights): T.Token[]; | ||
/** | ||
* Returns a function to be used to score individual results. | ||
* | ||
* Good matches will have a higher score than poor matches. | ||
* If an item is not a match, 0 will be returned by the function. | ||
* | ||
* @returns {function} | ||
*/ | ||
getScoreFunction(query: string, options: T.Options): (data: {}) => any; | ||
_getScoreFunction(search: T.PrepareObj): (data: {}) => any; | ||
/** | ||
* Returns a function that can be used to compare two | ||
* results, for sorting purposes. If no sorting should | ||
* be performed, `null` will be returned. | ||
* | ||
* @return function(a,b) | ||
*/ | ||
getSortFunction(query: string, options: T.Options): ((a: any, b: any) => number) | null; | ||
_getSortFunction(search: T.PrepareObj): ((a: any, b: any) => number) | null; | ||
/** | ||
* Parses a search query and returns an object | ||
* with tokens and fields ready to be populated | ||
* with results. | ||
* | ||
*/ | ||
prepareSearch(query: string, optsUser: T.Options): T.PrepareObj; | ||
/** | ||
* Searches through all items and returns a sorted array of matches. | ||
* | ||
*/ | ||
search(query: string, options: T.Options): T.PrepareObj; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
export declare type Field = { | ||
field: string; | ||
weight: number; | ||
}; | ||
export declare type Sort = { | ||
field: string; | ||
direction?: string; | ||
}; | ||
export declare type Options = { | ||
fields: Field[]; | ||
sort: Sort[]; | ||
score?: () => any; | ||
filter?: boolean; | ||
limit?: number; | ||
sort_empty?: Sort[]; | ||
nesting?: boolean; | ||
respect_word_boundaries?: boolean; | ||
conjunction?: string; | ||
}; | ||
export declare type Token = { | ||
string: string; | ||
regex: RegExp | null; | ||
field: string | null; | ||
}; | ||
export declare type Weights = { | ||
[key: string]: number; | ||
}; | ||
export declare type PrepareObj = { | ||
options: Options; | ||
query: string; | ||
tokens: Token[]; | ||
total: number; | ||
items: ResultItem[]; | ||
weights: Weights; | ||
getAttrFn: (data: any, field: string) => any; | ||
}; | ||
export declare type Settings = { | ||
diacritics: boolean; | ||
}; | ||
export declare type ResultItem = { | ||
score: number; | ||
id: number | string; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* A property getter resolving dot-notation | ||
* @param {Object} obj The root object to fetch property on | ||
* @param {String} name The optionally dotted property name to fetch | ||
* @return {Object} The resolved property value | ||
*/ | ||
export declare const getAttr: (obj: { | ||
[key: string]: any; | ||
}, name: string) => any; | ||
/** | ||
* A property getter resolving dot-notation | ||
* @param {Object} obj The root object to fetch property on | ||
* @param {String} name The optionally dotted property name to fetch | ||
* @return {Object} The resolved property value | ||
*/ | ||
export declare const getAttrNesting: (obj: { | ||
[key: string]: any; | ||
}, name: string) => { | ||
[key: string]: any; | ||
} | undefined; | ||
/** | ||
* Calculates how close of a match the | ||
* given value is against a search token. | ||
* | ||
*/ | ||
export declare const scoreValue: (value: string, token: any, weight: number) => number; | ||
export declare const escape_regex: (str: string) => string; | ||
/** | ||
* Cast object property to an array if it exists and has a value | ||
* | ||
*/ | ||
export declare const propToArray: (obj: { | ||
[key: string]: any; | ||
}, key: string) => void; | ||
/** | ||
* Iterates over arrays and hashes. | ||
* | ||
* ``` | ||
* iterate(this.items, function(item, id) { | ||
* // invoked for each item | ||
* }); | ||
* ``` | ||
* | ||
*/ | ||
export declare const iterate: (object: [] | { | ||
[key: string]: any; | ||
}, callback: (value: any, key: number | string) => any) => void; | ||
export declare const cmp: (a: number | string, b: number | string) => 1 | -1 | 0; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters