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

raddog

v1.0.5

Published

Simple, modifiable item-title search index based on radix tries.

Downloads

15

Readme

raddog

NPM License NPM version Bower version

RadDog is a simple, modifiable item-title search index based on radix tries. Its primary use case is for building auto-completion indexes that can be loaded and queried directly from the browser. For example, the query sweat may yield the items corresponding to the following titles that have been indexed: Ugly Christmas Sweater, Blue Sweatpants, and Sweater.

Install

Install with either bower or npm.

bower install raddog
npm install raddog

Example Code

// Demonstrates how to insert, delete, and search
var dog = new RadDog('uid', 'title');
var item1 = {uid: '1', title: 'Ugly Christmas Sweater', ...other data};
var item2 = {uid: '2', title: 'Blue Sweatpants', ...other data};
var item3 = {uid: '3', title: 'Sweater', ...other data};
var item4 = {uid: '4', title: 'Dog Treats', ...other data};
dog.insert(item1);
dog.insert(item2);
dog.insert(item3);
dog.insert(item4);
var cursor = dog.search('sweater');
var matches = cursor.remaining();
// matches = [item1, item2, item3]
dog.delete(item2);
var cursor = dog.search('sweater', function(item) { return (item.title.indexOf('Ugly') !== -1); });
var matches = cursor.remaining();
// matches = [item1]

Update Methods

Constructor

RadDog(uid or data, title)

  • uid or data - when inserting an item, which field contains the uid (unique id) of the item to use in the trie for indexing items. Or data being a serialized version of a RadDog index.
  • title - when inserting an item, which field contains the title to index.

Insert

insert(item)

  • item - takes the title of the item, tokenizes it into lowercase tokens and adds an entry in the trie for the item's uid for each token. Item is then saved in a uid:item dictionary for later querying.

Delete

delete(item) - finds all entries of the title tokens in the trie and removes the item's uid and also removes the item from the uid:item dictionary.

Serializing

Simply call JSON.stringify() on your RadDog object. This will give you index data to save and later load into the constructor or prepare() in a readonly setting.

Query Methods

Get

get(uid)

Returns an item that has the uid or undefined if items is not currently indexed.

  • uid - uid of the item you want to get.

Search

search(query[, filter])

Search takes a string, tokenizes around the spaces and lowercases each token. The cursor then returns results for items that have exact matches for all of the tokens (except that last token). The last token can be a partial match, because it is assumed the user is in the middle of typing the last token.

Returns a cursor object for iterating over search results.

  • query - Arbitrary user input, can have many tokens.
  • filter - Optional callback method to accept and item and apply another filter to it. Return true to include in the search results, and false to exclude.

Cursor Object

next()

Walk to the next result in the trie and return the item. Returns null when there are no more matching items.

remaining()

A convenience method to building an array of items by calling next() until the end is reached.

end

A variable that will be set to true once the end is reached.

QueryDog

As a convenience, RadDog also comes with querydog.js a smaller readonly version of RadDog that only contains the query methods described above. A similar constructor is included in querydog.js which only accepts existing RadDog data as the only argument.

Example Code

var dog = new RadDog('uid', 'title');
//......create an index
var serialized = JSON.stringify(dog);
var queryOnlyDog = new QueryDog(JSON.parse(serialized));
var cursor = queryOnlyDog.search('Sweater');
//......use cursor

License

MIT