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

redis-trie

v1.4.0

Published

Trie based search for autocompletes with Redis.

Downloads

9

Readme

redis-trie

Trie based autocomplete with Redis.

Install

npm i redis-trie --save

Usage

var rtrie = new Rtrie(options)

options:

  • trieKey: {String} the key prefixes for indexes, default 'trie:index:'
  • metadataKey: {String|false} the key prefixes for metadata, default 'trie:metadata', if false, not save metadata to redis.
  • client: {Object} redis client
  • host: {String} redis host(only client not exist)
  • port: {String} redis port(only client not exist)
  • password: {String} redis password(only client not exist)
  • others option see ioredis

rtrie.add(key, value, id, priority) => {Promise}

add the key with a given value and id and priority.

  • key: {String} key for index
  • value: {Object} data you may want to store directly on the index.
  • id: {String} id for metadata
  • priority: {Number|Funcrion=>Number} the relevance of this item in comprassion of others.

rtrie.del(key, id) => {Promise}

del the key.

  • key: {String} key for index
  • id: {String} id for metadata

rtrie.search(key[, offset][, limit]) => {Promise}

search for a key.

  • key: {String} the search key
  • offset: {Number} offset, default 0
  • limit: {Number} limit, default 20

Example

'use strict';

var co = require('co');
var Rtrie = require('.');
var rtrie = new Rtrie();

var user1 = {
  _id: '55d2b3bffad4f453dbbb590b', // timestamp: 1439871935000
  username: 'user1',
  avatar: 'avatar1'
};

var user2 = {
  _id: '55d2b3fb7b61adf480a08192', // timestamp: 1439871995000
  username: 'user2',
  avatar: 'avatar2'
};

var dateFromObjectId = function (objectId) {
  return new Date(parseInt(objectId.substring(0, 8), 16) * 1000).getTime();
};

co(function* () {
  var res;
  yield rtrie.add(user1.username, user1, user1._id, dateFromObjectId(user1._id));
  res = yield rtrie.search('user');
  console.log(res);
  // [ { _id: '55d2b3bffad4f453dbbb590b',
  //     username: 'user1',
  //     avatar: 'avatar1' } ]
  res = yield rtrie.search('user1');
  console.log(res);
  // [ { _id: '55d2b3bffad4f453dbbb590b',
  //     username: 'user1',
  //     avatar: 'avatar1' } ]
  res = yield rtrie.search('user2');
  console.log(res);
  // []

  rtrie.add(user2.username, user2, user2._id, dateFromObjectId(user2._id));
  res = yield rtrie.search('user');
  console.log(res);
  // [ { _id: '55d2b3fb7b61adf480a08192',
  //     username: 'user2',
  //     avatar: 'avatar2' },
  //   { _id: '55d2b3bffad4f453dbbb590b',
  //     username: 'user1',
  //     avatar: 'avatar1' } ]
  res = yield rtrie.search('user1');
  console.log(res);
  // [ { _id: '55d2b3bffad4f453dbbb590b',
  //     username: 'user1',
  //     avatar: 'avatar1' } ]
  res = yield rtrie.search('user2');
  console.log(res);
  // [ { _id: '55d2b3fb7b61adf480a08192',
  //     username: 'user2',
  //     avatar: 'avatar2' } ]

  yield rtrie.del(user1.username, user1._id);

  res = yield rtrie.search('user');
  console.log(res);
  // [ { _id: '55d2b3fb7b61adf480a08192',
  //     username: 'user2',
  //     avatar: 'avatar2' } ]
  res = yield rtrie.search('user1');
  console.log(res);
  // []
  res = yield rtrie.search('user2');
  console.log(res);
  // [ { _id: '55d2b3fb7b61adf480a08192',
  //     username: 'user2',
  //     avatar: 'avatar2' } ]
});

or

'use strict';

var co = require('co');
var Rtrie = require('.');
var rtrie = new Rtrie();

var user = {
  _id: '55d2b3bffad4f453dbbb590b',
  username: 'user',
  avatar: 'avatar'
};

var user1 = {
  _id: '55d2b3fb7b61adf480a08192',
  username: 'user1',
  avatar: 'avatar1'
};

var user11 = {
  _id: '55d2b3fb7b61adf480a08193',
  username: 'user11',
  avatar: 'avatar11'
};

var priority = function (key, value, id, part) {
  return 100 - ( key.length - part.length );
};

co(function* () {
  var res;
  yield rtrie.add(user.username, user, user._id, priority);
  yield rtrie.add(user1.username, user1, user1._id, priority);
  yield rtrie.add(user11.username, user11, user11._id, priority);
  res = yield rtrie.search('user');
  console.log(res);
  // [ { _id: '55d2b3bffad4f453dbbb590b',
  //     username: 'user',
  //     avatar: 'avatar' },
  //   { _id: '55d2b3fb7b61adf480a08192',
  //     username: 'user1',
  //     avatar: 'avatar1' },
  //   { _id: '55d2b3fb7b61adf480a08193',
  //     username: 'user11',
  //     avatar: 'avatar11' } ]
});

when metadataKey is false:

'use strict';

var co = require('co');
var Rtrie = require('.');
var rtrie = new Rtrie({
  metadataKey: false
});

var user = {
  _id: '55d2b3bffad4f453dbbb590b',
  username: 'user',
  avatar: 'avatar'
};

var user1 = {
  _id: '55d2b3fb7b61adf480a08192',
  username: 'user1',
  avatar: 'avatar1'
};

var user11 = {
  _id: '55d2b3fb7b61adf480a08193',
  username: 'user11',
  avatar: 'avatar11'
};

var priority = function (key, value, id, part) {
  return 100 - ( key.length - part.length );
};

co(function* () {
  var res;
  yield rtrie.add(user.username, null, user._id, priority);
  yield rtrie.add(user1.username, null, user1._id, priority);
  yield rtrie.add(user11.username, null, user11._id, priority);
  res = yield rtrie.search('user');
  console.log(res);
  // [ '55d2b3bffad4f453dbbb590b',
  //   '55d2b3fb7b61adf480a08192',
  //   '55d2b3fb7b61adf480a08193' ]
});

Test

npm test