1.2.3 • Published 14 days ago

fzearch v1.2.3

Weekly downloads
-
License
MIT
Repository
github
Last release
14 days ago

Fzeach

Fzearch is a fuzzy search library for Javascript/Typescript. It is base on Smith-Waterman algorithm and provides a simple and easy-to-use API for fuzzy search.

How to use

import { Fzeach } from 'fzeach';

const fzeach = new Fzeach();

const list = [
	'apple',
	'banana',
	'orange',
	'grape',
	'kiwi',
	'mango',
	'melon',
	'peach',
	'pear',
	'plum',
	'strawberry',
	'watermelon',
];

const keyword = 'apple';

// create a class instance
const result = fzeach.search(list, keyword, 'levenshtein');
// or use the static method
const result = Fzeach.search(list, keyword, 'damerau-levenshtein');

API

Fzearch.search(query: string, db: string[], options?: FzearchOptions): any[]

  • query: The keyword to search.
  • db: The list of strings to search.
  • options: The options for the search.
    • maxResults: The maximum number of results to return. Default is 5.
    • getPenalty: The function to calculate the penalty for the given characters.
    • getScore: The function to calculate the score for the given characters.
    • showScore: Whether to show the score in the result. Default is false.
    • caseSensitive: Whether to consider the case sensitivity. Default is false.
    • levelPenalty: The penalty for the level of the keyword in the object. Default is 1 (which is mean no penalty).
    • dropoutRate: The threshold for the dropout. Default is 0.8 (higher means less dropout).

Algorithms

I want to base on smith-waterman algorithm and modify some of the mark calculation to achieve some of the following goals:

  • Keyword appears earlier in the string, it should have a higher score. For example, if we search react in the list ['react hooks', 'common react mistakes'], 'react hooks' should and a higher order.

  • Case sensitivity should be considered. The weighting of case matching should be higher than the weighting of case mismatching.

Example

let fzearch: Fzearch;

const db = [
	'Algorithm of Searching Agentsz',
	'Debounce and Throttle',
	'React Hooks',
	'Persisting State in React',
	'Deep Copy and Shadow Copy in Javascript',
	'Common React Mistakes',
];

fzearch = new Fzearch(db);

console.log(fzearch.search('co', 5));
// [
//   'Common React Mistakes',
//   'Deep Copy and Shadow Copy in Javascript',
//   'Persisting State in React',
//   'React Hooks',
//   'Algorithm of Searching Agentsz'
// ]

Reference

1.2.0

14 days ago

1.1.1

14 days ago

1.1.0

14 days ago

1.2.3

14 days ago

1.1.4

14 days ago

1.2.2

14 days ago

1.1.2

14 days ago

1.0.2

27 days ago

1.0.4

27 days ago

1.0.3

27 days ago

1.0.1

1 month ago

1.0.0

1 month ago