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

@marianmeres/searchable

v1.0.10

Published

Searchable index

Downloads

1

Readme

@marianmeres/searchable

A simple "any word from set that exactly begins with" in-memory trie based search index. No built-in rankings or sorting. Query words order agnostic.

Useful for fast in-memory filtering of ahead-of-time known set of objects (autosuggestions, typeahead or similar).

Real world example

See https://searchable.meres.sk/example/ (source) for more real-world like example.

Installation

npm install @marianmeres/searchable

Basic usage

// create instance
const index = new Searchable();

// add any value to index with provided searchable label
const license = { to: 'kill' };
index.add('james bond', license);
index.add('007', license);
// index.add(...) ...

// search for it
let results = index.search('bond james bond');
assert(results.length === 1);
assert(results[0] === license);

results = index.search('007 bond');
assert(results.length === 1);
assert(results[0] === license);

The index doesn't care of the values stored in it. It can be anything. But, for cases where:

  • you need to index large result set,
  • or you have equal but not same instance objects

it might be a good idea to save ids only, which you will need to map to your values manualy. See below.

const map = { 1: 'peter pan', 2: 'mickey mouse', 3: 'shrek' };

const index = new Searchable();

// add only ids to the index
Object.entries(map).forEach(([id, label]) => index.add(label, id));

// map results back to values
assert('shrek' === index.search('shr').map((id) => map[id])[0]);

Options

(Terminology: a search query or an indexable label are split into words before processing).


// default options
const index = new Searchable({
    // if false (default), both input and search query will be lower-cased
    // if true, both input and search query will be kept case-untouched
    caseSensitive: false,

    // if false (default), both input and search query will be un-accented
    // if true, both input and search query will be kept accent-untouched
    accentSensitive: false,

    // function to check whether the word should be considered as a stopword (and so
    // effectivelly omitted from index and/or query)
    isStopword: (word): boolean => false,

    // any custom normalization applied before adding to index or used for query
    // usefull for e.g.: stemming, spellcheck, now-word chars removal, custom conversion...
    // can return array of words (e.g. aliases)
    normalizeWord: (word): string | string[] => word,

    // any custom logic applied to query before being used for search
    // should return `{ query, operators }` shape, where:
    // - `query` will be used for index.search(query)
    // - `operators` (might be null) will be passed to final `processResults` filtering
    parseQuery: (query): ParseQueryResult => ({ query, operators: null }),

    // applied as a last step on found results. Use for:
    // sorting, custom operators filtering, ...
    processResults: (results, parseQueryResults: ParseQueryResult): any[] => results,

    // will skip search altogether if none of the query words is longer than this limit
    querySomeWordMinLength: 1,
});

Advanced usage example

Word normalization example

const index = new Searchable({
    // will be applied to both label in the index and the query
    normalizeWord: (w) => {
        const sports = { basketball: 'sport', football: [ 'sport', 'soccer' ] };
		return sports[w.toLowerCase()] || w;
    }
});

index.add('basketball', 'basketball');
index.add('football', 'football');

assert(index.search('sport').length === 2); // ['basketball', 'football']

assert(index.search('soccer').length === 1);
assert(index.search('soccer')[0] === 'football');

Accent sensitivity example

const accented = 'Příliš žluťoučký kůň úpěl ďábelské ódy';

// accent insensitive (default)
let index = new Searchable();
index.add(accented, true);
assert(index.search('kůň')[0]);
assert(index.search('kun')[0]);

// accent sensitive
index = new Searchable({ accentSensitive: true });
index.add(accented, true);
assert(index.search('kůň')[0]);
assert(!index.search('kun')[0]);

Index can be serialized. Note however, that all stored values must suppport JSON.stringify for this to work.

const index = new Searchable();
index.add('james bond', 7);
const dump = index.dump();
assert(typeof dump === 'string');

// dump can now be saved to file, db, etc...

// now restore
const index2 = new Searchable();
index2.restore(dump);
assert(7 === index2.search('bond')[0]);