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

simplesearchjs

v1.1.0

Published

Gmail like string search.

Downloads

2

Readme

simplesearchjs

Easy to use gmail like search for your data.

What Simple Search Does

The goal of simple search is to provide powerful search features to your users (via a text field) and simple search implementation for developers (via a single javascript function).

For the user

If you're interacting with simplesearch, this document will help you understand how to use it (User Guide)[https://github.com/graham/simplesearchjs/blob/master/user_readme.md]

For the developer

All you'll need is some text input, you can pass that to the "build_fn" function from simplesearchjs, and you'll receive a callback that can take a single object (or list of objects). Depending on the search criteria it will return a boolean of if the search matched that object.

A simple example might look like this:


 const test_data = [{ name: 'Han', age: 35 }, { name: 'Leia', age: 21 }];

 const search_string = 'age:21';
 const filter = build_fn(search_string);
 const result = test_data.filter(filter);

 // result == [{name:'Leia', age:21}];

 expect(result.length).toBe(1);
 expect(result[0].name).toBe('Leia');

But users don't have to understand this structure to get started, what we call a "haystack" search is more what a user is used to (which is really just, do any of the words I type show up in the results).

Anything in the "haystack" key (which can be configured) will be used for this "non-specified" search.

 const test_data = { haystack: 'this is a test' };
 
 const filter = build_fn('test');
 const filter2 = build_fn('foo');
 
 const result = filter(test_data);
 const result2 = filter2(test_data);

 expect(result).toBe(true);
 expect(result2).toBe(false);

Of course, we want to support much more complex searches as well:

 const test_data = [{ name: 'Han', age: 35 }, { name: 'Leia', age: 21 }];

 const search_string = 'age:>=35';
 const filter = build_fn(search_string);
 const result = test_data.filter(filter);

 expect(result.length).toBe(1);
 expect(result[0].name).toBe('Han');

We should support OR cases:

 const test_data = [{ cool: 20 }, { cool: 10 }];

 const search_string = 'cool:or,20,10';
 const results = test_data.filter(build_fn(search_string));
 expect(results.length).toBe(2);

and AND cases

 const test_data = [{ cool: 50 }, { cool: 50 }];

 const search_string = 'cool:and,20,10';
 const results = test_data.filter(build_fn(search_string));
 expect(results.length).toBe(0);

case insensitive searches

 const test_data = [{ words: 'WELCOME' }];

 const search_string = 'words:i/welcome';
 const filter = build_fn(search_string);
 const result = test_data.filter(filter);

 expect(result.length).toBe(1);
 expect(result[0].words).toBe('WELCOME');

We support "digging" into objects based on child objects:

(% is similar to SQL LIKE)

 const test_data = [
     {
       host_id: 54321,
       build_history: {
       timestamp: 1411075727,
       version: {
         number: 'Service-mac-3.1.213',
         },
       },
     },
     {
       host_id: 12345,
       build_history: {
       timestamp: 1411075729,
       version: {
         number: 'Service-linux-3.1.213',
         },
       },
     },
 ];

 const search_string = 'build_history.version.number:%mac';
 const filter = build_fn(search_string);

 const result = test_data.filter(filter);

 expect(result.length).toBe(1);
 expect(result[0].host_id).toBe(54321);

what about regex?

 const test_data = [{ name: 'han' }, { name: 'luke' }];

 const search_string = 'name:/^..ke$';
 const filter = build_fn(search_string);
 const result = test_data.filter(filter);

 expect(result.length).toBe(1);
 expect(result[0].name).toBe('luke');

Fuzzy search:

 const test_data = [
     { host_id: 12345, name: 'graham' },
     { host_id: 54321, name: 'vutran' },
 ];

 const search_string = 'name:gra';

 const filter = build_fn(search_string);

 let result: Array<any> = [];
 for (var item of test_data) {
     if (filter([item])) {
         result.push(item);
     }
 }

 expect(result.length).toBe(1);
 expect(result[0].host_id).toBe(12345);

exact search:

 const test_data = [
     { host_id: 12345, name: 'graham' },
     { host_id: 54321, name: 'vutran' },
 ];

 const search_string = 'name:=gra';

 const filter = build_fn(search_string);

 let result: Array<any> = [];
 for (var item of test_data) {
     if (filter([item])) {
         result.push(item);
     }
 }

 expect(result.length).toBe(0);

Negative (or reductive) search:

 const test_data = [{ cool: 20 }, { cool: 10 }];

 const search_string = '-cool:20';
 const results = test_data.filter(build_fn(search_string));
 expect(results.length).toBe(1);

And macros

 const test_data = [{ name: 'Han' }, { name: 'Leia' }];

 const make_case_insensitive_macro_func = (
     key: string,
     arg_list: Array<string>
 ) => {
     if (arg_list) {
          return [key, arg_list.map(item => 'i/' + item)];
     }
 };

 const filter = build_fn('name:han', {
     macros: { name: make_case_insensitive_macro_func },
 });

 const results = test_data.filter(filter);
 expect(results.length).toBe(1);
 expect(results[0].name).toBe('Han');

We also support emojis :)

The tests and benchmarks will provide you with most of what you need to get started. If you have any ideas for additional comparisons, or features, just create an issue and I'll write it as fast as I can.