Searchability
Searchability provides a simple interface, a Searchable, to pair a string with some of the built-in search functions - endsWith
, includes
, or startsWith
.
interface Searchable {
searchTerm: string;
searchType: SearchType;
};
enum SearchType {
EndsWith,
Includes,
StartsWith
}
Users are free to build their own logic to interpret a Searchable, but a dedicated Search function is provided that picks the correct search based on the SearchType enum.
const Search = (searchable: Searchable, searchString: string, position?: number): boolean
Usage
The intended usage of a Searchable is to provide a compact option for any type-based searches, as the following example illustrates:
import { Searchable, Search, SearchKey } from "searchability";
type SearchKey = string | Searchable | RegExp;
const searchString: string = "Lorem ipsum";
const k: SearchKey = {searchTerm: "Lorem", searchKey: SearchKey.StartsWith}
function matchFound(k: SearchKey) => boolean {
switch (typeof k) {
case "string":
return k === searchString;
case "Searchable"
return Search(k, searchString);
case "RegExp"
return k.exec(searchString);
}
}
npm
Install withnpm i searchability --save
This package is provided in the ES2015 module format.
Contributing
Pull requests are always welcome. For bugs and feature requests, please create an issue.
License
Copyright (c) 2021 David Paige.
Released under the MIT license.