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

tokensearch.js

v0.8.0

Published

Substring token search

Downloads

5,516

Readme

tokensearch.js

Dependency Status Build Status

tokensearch.js is a simple substring search functions for collections. You can search for multiple search tokens in a json file, the result array contains the original object plus a search score (0: perfect, 1: forget it). See the example or unit tests for more details.

Inspired by https://github.com/krisk/Fuse, users.json file is ripped from this project.

Options

Tokensearch.defaultOptions = {
  //split strings with those delimiters, default delimiters: space and dash
  delimiter: /[\s-]+/,

  // At what point does the match algorithm give up. A threshold of '0.0' requires a perfect match
  // (of both letters and location), a threshold of '1.0' would match anything.
  threshold: 0.7,

  // How many search tokens are considered
  maxFilterTokenEntries: 5,

  // search key
  collectionKeys: [],

  //the result just contains unique results (based on collection keys)
  unique: false,

  // search all 'needles' in the 'haystack', return a score for each function call
  searchAlgorithm: function(haystack, needles) {
    var score = 0;
    var arrayLength = needles.length;
    for (var i = 0; i < arrayLength; i++) {
      var needle = needles[i];
      var stringPos = haystack.indexOf(needle);
      if (stringPos > -1) {
        if (haystack === needle) {
          score += 6;
        } else if (stringPos === 0) {
          score += 2;
        } else {
          score += 1;
        }
      }
    }
    return score;
  },

  //postprocess all elements (=contains all elements with a score)
  postprocessAlgorithm: function(collection, maxScore, threshold) {
    var normalizedScore = 1 / maxScore;
    var result = [];
    collection.forEach(function(e) {
      e.score = 1-e.score*normalizedScore;
      if (e.score <= threshold) {
        result.push(e);
      }
    });
    return result;
  },

  // sort the result array (=output of the postprocess step)
  sortAlgorithm: function(array) {
    return array.sort(function(a, b) {
    return a.score - b.score;
    });
  }};

You can pass one or multiple parameter when creating the object, for example

new Tokensearch(myCollection, { collectionKeys: ['key1', 'key2'], threshold: 0.5 });

Examples

Simple

Search for text tokens in one JSON field, use space as delimiter.

Setup:

var collection = [{
  "name": "JOHN PETER DOW",
  "id": "123"
}, {
  "name": "FOO BAR JOHN",
  "id": "127",
}, {
  "name": "BODE JON MULLER",
  "id": "147",
}];
var tokenSearch = new Tokensearch(collection, { collectionKeys: ['name'] });

Search:

var result = tokenSearch.search('JOHN BAR');

Result:

[
  {"item":{"name":"FOO BAR JOHN","id":"127}","score":0},
  {"item":{"name":"JOHN PETER DOW","id":"123"},"score":0.5}
]

Advanced 1

Search for text tokens in two JSON fields, use space and : as delimiter.

Setup:

var collection = [{
  "name": "JOHN PETER DOW",
  "address": "a:funny:street:44",
  "id": "123"
}, {
  "name": "FOO BAR JON",
  "address": "bullvd:33",
  "id": "127",
}, {
  "name": "BODE JOHN MULLER",
  "address": "upside:street",
  "id": "147",
}];
var tokenSearch = new Tokensearch(collection, { collectionKeys: ['name', 'address'], delimiter: /[\s:]+/, threshold: 0.5});

Search:

var result = tokenSearch.search('JOHN:street');

Result:

[
  {"item":{"name":"JOHN PETER DOW","address":"a:funny:street:44","id":"123"},"score":0},
  {"item":{"name":"BODE JOHN MULLER","address":"upside:street","id":"147"},"score":0}
]

Advanced 2

Search for text tokens in two JSON fields, use space and : as delimiter, use a custom search algorithm.

Setup:

    var collection = [{
      "name": "JOHN DOE",
      "address": "a:funny:street:44",
      "id": "123"
    }, {
      "name": "FOO BAR JON",
      "address": "bullvd:33",
      "id": "127",
    }, {
      "name": "BODE MULLER",
      "address": "john:upside:street",
      "id": "147",
    }];

    var tokenSearch = function(haystack, needles) {
      var score = 0;
      var arrayLength = needles.length;
      for (var i = 0; i < arrayLength; i++) {
        var needle = needles[i];
        if (haystack === needle) {
          score ++;
        }
      }
      return score;
    };
    var tokenSearch = new Tokensearch(collection, { collectionKeys: ['name', 'address'], delimiter: /[\s:]+/, threshold: 0.5, searchAlgorithm: tokenSearch});

Search:

var result = tokenSearch.search('JOHN');

Result:

[
  {"item":{"name":"JOHN DOE","address":"a:funny:street:44","id":"123"},"score":0},
  {"item":{"name":"BODE MULLER","address":"john:upside:street","id":"147"},"score":0}
]

Build

  • to run tests: npm test
  • to run jshint: npm run-script jshint
  • to create a new release: npm run-script release
  • to check code coverage: npm run-script coverage