npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

autokomplete

v0.0.5-b

Published

A fast, lightweight autocomplete module that linearly scales with large amounts of data

Downloads

6

Readme

autokomplete

A fast, robust and highly scalable autocomplete module that matches query quickly regardless of the input size.

Summary

autokomplete was created to capstone an article on autocomplete algorithms using the findings and discussions in that article. It uses a suffix array built in linear time using DC3 and two binary searches to find the match extents, slicing, mapping, and returning everything in between.

Matching time scales linearly with the number of matches and logarithmicly with the size of the input, making it especially fast for large inputs and longer queries.

Use

Simply add autokomplete to your project with npm install autokomplete then:

  1. import autocompleter from 'autokomplete'
  2. Ensure your input is an array where each entry has a string property. The module will match queries against this property.
    const entries = [
     {string: 'kha-ld', id: 4, name: 'Khaled'},  // You can add any other properties
     {string: 'test', id: 0},  // Entry schemas don't have to be identical
     {string: 'autocomplete'},  // Just make sure each has a "string" property
     {string: 'Pharaoh 🐪𓂀'}  // You can include any special characters or character case
    ]
  3. Create an autocompleter instance with those entries:
    const example = autocompleter(entries)
  4. Match queries, returning an array of matching entries:
    const matchOne = example.match('complete')
    // matchOne == [{string: 'autocomplete'}] - Matches substrings, not just prefixes
    
    const matchTwo = example.match('ph')
    // matchTwo == [{string: 'Pharaoh 🐪𓂀'}] - Matching is case insensitive
    
    const matchThree = example.match('test')
    // matchThree == [{string: 'test', id: 0}] - Matching returns the complete entry
    
    const matchFour = example.match('🐪')
    // matchFour == [{string: 'Pharaoh 🐪𓂀'}] - Yup

Modifying a Model

autocompleters are immutable, so adding new entries and removing existing ones will return new models.

Insertion

Use currentModel.insert(entries: entry|entry[]) to add new entries:

const entries = [{string: 'Pyramid of Menkaure,', type: 'pyramid'}]
let egyptianWonders = autocompleter(entries)

const newEntry = {string: 'Pyramid of Khafre', type: 'pyramid'}
const egyptianWondersTemp = egyptianWonders.insert(newEntry) // Insert a single entry, and return a new model

const newerEntries = [
    {string: 'Great Pyramid of Khufu', type:'pyramid'},
    {string: 'Great Sphinx of Giza', type: 'statue'}
]
egyptianWonders = egyptianWondersTemp.insert(newerEntries) // Insert an array of entries

Removal

Removing entries can be done in multilpe ways with:

currentModel.remove({
    filters: callback[], // An array of filtering functions. Keeping entries that return true/truthy
    strings: string[], // A string filter, removes entries with strings that match ones in this array
    entries: entry[] // Entry filter, removes entries that exactly match entries in this array.* 
})

* entries currently uses string comparisons and sorts upper level properties. Ensure depper properties are in the correct order or implement a filter function and use filters.

const entries = [
    {string: 'test', id: 1},
    {string: 'module', id: 2},
    {string: 'autocomplete', id: 3},
    {string: 'kha-ld', id: 4}
]
const model = autocompleter(entries)

const idFilter = (entry) => entry.id > 1 // Only keep passing entries
const stringFilter = ['module'] // Remove entries with matching 'string' values
const entryFilter = [ {string: 'autocomplete', id: 3}] // Remove entries that exactly match ones in this list

const newModel = model.remove({
    filters: [idFilter], 
    strings: stringFilter,
    entries: entryFilter
}) // You don't need all 3 criterea
   // If you don't include any, you get an new but identical instance.

const matches = newModel.match('') // Match all entries
// matches == [{string: 'kha-ld', id: 4}]

Contributing

This module has the potential to grow and improve even more.

Features like exporting model data JSON, mapping matches over multiple threads, choosing a different matching porperty instead of "string", or adding more tests will all make the model more powerful and robust.

Feel free to contribute as you like.