Installation
1. Install the latest version with yarn:
yarn add extract-search-index
or with npm:
npm install extract-search-index
2. Import it in your code:
Quick Take
import { strict as assert } from "assert";
import { extract } from "extract-search-index";
assert.equal(
extract(`The quick brown fox jumps over the lazy dog.`),
"quick brown fox jumps over lazy dog"
);
// works with HTML, strips tags
assert.equal(extract("<tralala><div>some text</div>"), "some text");
Purpose
Search programs such as fuse.js
work on a set of data, for example, it’s this list
argument below:
const options = {
keys: ["title", "author.firstName"],
};
const fuse = new Fuse(list, options);
// Change the pattern
const pattern = "";
return fuse.search(pattern);
What if you want to implement a search function which would search within large strings, let’s say blog articles?
It would be inefficient to bundle up raw article string, send it to user’s browser, initiate fuse.js
there and perform a search.
It’s better to pre-format the sources — search function does not care about words present more than once; it does not care about letter case or article words.
This program prepares optimised strings to be consumed by the likes of fuse.js
.
PS. Also, you might want to pre-index the list, to speed up the search, but that’s a separate thing.
API — extract()
The main function extract()
is imported like this:
import { extract } from "extract-search-index";
It’s a function which takes one input argument:
function extract (str: string): string;
Input argument | Type | Obligatory | Description |
---|---|---|---|
str Type: String Obligatory: yes | |||
str | String | yes | String which we will process |
This function will return a string.
API — version
You can import version
:
import { version } from "extract-search-index";
console.log(version);
// => "2.0.3"