Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
synchronous-autocomplete
Advanced tools
Fast, simple autocompletion. Supports autocompletion and Levenshtein-based fuzzy search. Uses precomputed indexes to be fast.
npm install synchronous-autocomplete
Let's build a simple search for our fruit stand. We assign a weight
property to each of them because some are bought more often and we want to push their ranking in the search results.
const items = [ {
id: 'apple',
name: 'Juicy sour Apple.',
weight: 3
}, {
id: 'banana',
name: 'Sweet juicy Banana!',
weight: 2
}, {
id: 'pome',
name: 'Sour Pomegranate',
weight: 5
} ]
Let's understand the terminology used by this tool:
Hey There!
, you may process its name into the tokens hey
& there
.ther
(from the search query Hey Ther
) partially matches the token there
.In order to be as fast and disk-space-efficient as possible, synchronous-autocomplete
requires five indexes to be prebuilt from the list of items. Check the example code for more details on how to build them. For our example, they would look like this:
const tokens = { // internal item IDs, by token
juicy: [0, 1],
sour: [0, 3],
apple: [0],
sweet: [1],
banana: [1],
pomegranate: [3]
}
const weights = [ // item weights, by internal item ID
3, // apple
2, // banana
5 // pome
]
const nrOfTokens = [ // nr of tokens, by internal item ID
3, // apple
3, // banana
2 // pome
]
const scores = { // "uniqueness" of each token, by token
juicy: 2 / 3, // 2 out of 3 items have the token "juicy"
sour: 2 / 3,
apple: 1 / 3,
sweet: 1 / 3,
banana: 1 / 3,
pomegranate: 1 / 3
}
// In order to create smaller search indexes, we use numerical item IDs
// internally and maintain a mapping to their "real"/original IDs.
const originalIds = [
'apple',
'banana',
'pome'
]
As the last step, we must define a function that normalizes search input into a list of fragments. Consider using this simple function:
const normalize = require('normalize-for-search')
const tokenize = (str) => {
return normalize(str).replace(/[^\w\s]/g, '').split(/\s+/g)
}
Now, we can query our index:
const create = require('synchronous-autocomplete')
const autocomplete = create(tokens, scores, weights, nrOfTokens, originalIds, tokenize)
autocomplete('bana')
// [ {
// id: 'banana',
// relevance: 0.66667,
// score: 0.83995
// } ]
autocomplete('sour')
// [ {
// id: 'pomegranate',
// relevance: 1.83333,
// score: 3.13496
// }, {
// id: 'apple',
// relevance: 1.22222,
// score: 1.76275
// } ]
autocomplete('aplle', 3, true) // note the typo
// [ {
// id: 'apple',
// relevance: 0.22222,
// score: 0.3205
// } ]
const autocomplete = create(tokens, scores, weights, nrOfTokens, originalIds, tokenize)
autocomplete(query, limit = 6, fuzzy = false, completion = true)
tokens
must be an object with an array of internal item IDs per token.scores
must be an object with a token score per token.weights
must be an array with an item weight per internal item ID.nrOfTokens
must be an array with the number of tokens per internal item ID.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.If you have a question or have difficulties using synchronous-autocomplete
, please double-check your code and setup first. If you think you have found a bug or want to propose a feature, refer to the issues page.
FAQs
Fast, simple autocompletion.
The npm package synchronous-autocomplete receives a total of 60,430 weekly downloads. As such, synchronous-autocomplete popularity was classified as popular.
We found that synchronous-autocomplete demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.