synchronous-autocomplete
Advanced tools
Comparing version 3.0.0-alpha.1 to 3.0.0
16
index.js
@@ -0,1 +1,2 @@ | ||
import {ok} from 'node:assert' | ||
import hifo from 'hifo' | ||
@@ -6,3 +7,16 @@ import leven from 'leven' | ||
const createAutocomplete = (tokens, scores, weights, nrOfTokens, originalIds, tokenize) => { | ||
const createAutocomplete = (index, tokenize) => { | ||
const { | ||
tokens, | ||
scores, | ||
weights, | ||
nrOfTokens, | ||
originalIds, | ||
} = index | ||
ok(tokens && typeof tokens === 'object', 'tokens must be an object') | ||
ok(scores && typeof scores === 'object', 'scores must be an object') | ||
ok(Array.isArray(weights), 'weights must be an array') | ||
ok(Array.isArray(nrOfTokens), 'nrOfTokens must be an array') | ||
ok(Array.isArray(originalIds), 'originalIds must be an array') | ||
const byFragment = (fragment, completion, fuzzy) => { | ||
@@ -9,0 +23,0 @@ const results = [] |
{ | ||
"name": "synchronous-autocomplete", | ||
"description": "Fast, simple autocompletion.", | ||
"version": "3.0.0-alpha.1", | ||
"version": "3.0.0", | ||
"type": "module", | ||
"main": "index.js", | ||
"todo": "declare entrypoints", | ||
"files": [ | ||
@@ -9,0 +8,0 @@ "index.js", |
@@ -85,3 +85,3 @@ # synchronous-autocomplete | ||
As the last step, we must define a function that normalizes search input into a list of *fragments*. Consider using this simple function: | ||
Next, we must define a function that normalizes search input into a list of *fragments*. Consider using this simple function: | ||
@@ -96,2 +96,10 @@ ```js | ||
Of course, you don't have to calculate the tokens & scores! Instead, use `buildIndex` to generate the data: | ||
```js | ||
import {buildIndex} from 'synchronous-autocomplete/build.js' | ||
const index = buildIndex(tokenize, items) | ||
``` | ||
Now, we can query our index: | ||
@@ -102,9 +110,9 @@ | ||
const autocomplete = createAutocomplete(tokens, scores, weights, nrOfTokens, originalIds, tokenize) | ||
const autocomplete = createAutocomplete(index, tokenize) | ||
autocomplete('bana') | ||
// [ { | ||
// id: 'banana', | ||
// relevance: 0.66667, | ||
// score: 0.83995 | ||
// relevance: 0.6666665555555555, | ||
// score: 0.8399472266053544, | ||
// weight: 2, | ||
// } ] | ||
@@ -114,9 +122,11 @@ | ||
// [ { | ||
// id: 'pomegranate', | ||
// relevance: 1.83333, | ||
// score: 3.13496 | ||
// id: 'pome', | ||
// relevance: 1.8333335, | ||
// score: 3.134956187236602, | ||
// weight: 5, | ||
// }, { | ||
// id: 'apple', | ||
// relevance: 1.22222, | ||
// score: 1.76275 | ||
// id: 'apple', | ||
// relevance: 1.2222223333333333, | ||
// score: 1.762749635070118, | ||
// weight: 3, | ||
// } ] | ||
@@ -126,5 +136,6 @@ | ||
// [ { | ||
// id: 'apple', | ||
// relevance: 0.22222, | ||
// score: 0.3205 | ||
// id: 'apple', | ||
// relevance: 0.22222216666666667, | ||
// score: 0.3204998243877813, | ||
// weight: 3, | ||
// } ] | ||
@@ -137,3 +148,11 @@ ``` | ||
```js | ||
const autocomplete = createAutocomplete(tokens, scores, weights, nrOfTokens, originalIds, tokenize) | ||
const index = buildIndex(tokenize, items) | ||
const {tokens, scores, weights, nrOfTokens, originalIds} = index | ||
``` | ||
- `tokenize` must be a function that, given a search query, returns an array of *fragments*. | ||
- `items` must be an array of objects, each with `id`, `name` & `weight`. | ||
```js | ||
const autocomplete = createAutocomplete(index, tokenize) | ||
autocomplete(query, limit = 6, fuzzy = false, completion = true) | ||
@@ -147,3 +166,3 @@ ``` | ||
- `originalIds` must be an array with the (real) *item* ID per internal *item* ID. | ||
- `tokenize` must be a function that, given a search query, returns an array of *fragments*. | ||
- `tokenize` is the same as with `buildIndex()`. | ||
@@ -160,3 +179,2 @@ | ||
// encode & write the index | ||
const index = {tokens, scores, weights, nrOfTokens, originalIds} | ||
const encoded = encodeIndex(index) | ||
@@ -163,0 +181,0 @@ writeFileSync('index.pbf', encoded) |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
22688
499
0
183
0