• Stars
    star
    1,702
  • Rank 27,429 (Top 0.6 %)
  • Language
    JavaScript
  • License
    Apache License 2.0
  • Created over 8 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Fast and easy searching inside a page

Holmes.js

Fast and easy searching inside a page.

Build Status Coverage Status npm version Bower version CDNJS version Join the chat at https://gitter.im/Haroenv/holmes gzipped size

Holmes filters a list of elements based on the value of a input in just ~2KB.

Installation

You can install Holmes with either npm or bower under the package name holmes.js. For npm that looks like this:

$ yarn add holmes.js # or via npm

After which you can add it in your page with i.e. webpack, rollup, browserify or loading the module in a different script tag.

You have to make sure that you have a css rule for the class .hidden that hides elements however you want. One option is to have this:

.hidden {
  display: none;
}

but this could be any css you want.

Usage

demo

When should I use it?

You should use Holmes when

  • you have a limited amount of items
  • you don't need typo-tolerance
  • you only want to add a really small library
  • all items are already visible on the page

In cases where you have a more complicated expectation, I'd suggest using a service like Algolia.

Fair disclosure: I currently work at Algolia, does it seem interesting? Join us!

Simple example

holmes({
  input: '.search input', // default: input[type=search]
  find: '.results div' // querySelectorAll that matches each of the results individually
})

Options

full documentation

input

default: input[type=search]

querySelector for the input

examples: input, .search input

find required

querySelectorAll for elements to search in

examples: blockquote p, .result, .results div

class

class.hidden

default: hidden

Class to add when the a .find doesn't contain the search query.

examples: hidden, dn, none

class.visible

default: false

Class to add to visible items if they contain the search query.

examples: visible, vis, nohidden

placeholder

default: false

html to show when no results.

examples: <p> No results </p>, Didn't find anything.

dynamic

default: false

Enable this option if you want Holmes to query the value of the .find at every input.

examples: true, false

instant โš ๏ธ DEPRECATED

default: false

This option is deprecated. To use Holmes in an async environment, initialise it with:

holmes(options).start();
// or
const h = new holmes(options);
h.start();

This way it'll start immediately, just like it used to do with instant: true. Sorry for the inconvenience.

By default Holmes will wait on a DOMContentLoaded event to start searching. If you're loading the elements by AJAX for example this event comes too early. In that case you can enable instant, and start Holmes when your content is ready.

examples: true, false

minCharacters

default: 0

A minimum amount of characters need to be typed before Holmes starts filtering.

examples: 2, 5

mark

default: false

To start showing the result in a <mark> tag inside the .find, you should enable this. To change the colour this match is shown in, you should style the mark background-color.

โ— this will break event listeners on nested content

โ— this won't work if the character after the match is a literal >.

๐Ÿ—ฃ๏ธ If you really have to use this character, you can replace all occurences of > by &gt;

examples: true, false

hiddenAttr

default: true

Adds hidden="true" to hidden elements. Interesting link explaining its use.

shouldShow

default match judgment is a partial match of the input value.

function(htmlText, search) {
  return htmlText.indexOf(search) !== -1;
}

A custom matching function to be called with as first argument the text of an element, and as second argument the current input text. This should return true if you want the element to show, and false if it needs to be hidden.

var customMatching = function(htmlText, search) {
  return search.split(/\s+/).every(function(v, i) {
    if (htmlText.indexOf(v) === -1) {
      return false;
    }
    return true;
  });
}
holmes({
  shouldShow: customMatching
})

onHidden

Callback for when an item is hidden.

function(el) {
  console.log('hide',el);
}

onVisible

Callback for when an item is visible again.

function(el) {
  console.log('show',el);
}

onEmpty

Callback for when no items were found.

function(placeholder) {
  console.log('nothing found',placeholder);
}

onFound

Callback for when items are found after being empty.

function(placeholder) {
  console.log('something found',placeholder);
}

onInput

Callback for every input.

function(input) {
  console.log('current input',input);
}

Methods and members

For all of the methods you should initialise a new instance of Holmes like this:

var h = new holmes(options);

Then you can use the following methods:

.clear()

You can clear a holmes input programmatically, by using:

h.clear();

.count()

You can receive informations on what elements are visible, hidden and in total at any point:

h.count(); // {all: 41, hidden: 34, visible: 7}

.start()

Start an even listener for the specified options. Holmes always has .start() running on initialisation.

h.start();

.stop()

Stops the current running event listener. Resolves a Promise when this has been completed.

h.stop();
h.start(); // could accidentally start too soon

h.stop().then(h.start); // might take a small time

.hidden

There's also a member .hidden that gives the count without a function call:

console.log(h.hidden); // 34

.elements

A NodeList of all of the elements that holmes considers. There's also .elementsLength for the amount of elements and .elementsArray with an array of the elements.

.input

The input that holmes looks in. There's also the last search string as .searchString

.placeholder

The current placeholder (DOM Node).

.running

Whether or not this instance is running.

.options

Shows the options chosen chosen for this instance of holmes. You can also set options like this after initialisation.

console.log(h.options); // specified options

note: setting options after it's running might require h.stop().then(h.start)

Showcase

What who image
bullg.it @haroenv screenshot of bullg.it
family.scss @lukyvj screenshot of family.scss
wikeo.be @bistory searching on wikeo.be for pages
lunchbreakapp.be @AndreasBackx searching on lunchbreak for items

I'd love to find out how people use my project, let me know if you want to be featured!

Questions?

Compatible up to IE11. For support of older browsers you'll need to polyfill classList, addEventListener and the input event with for example remy/polyfills. I haven't tried this myself yet, so let me know what you used if you support older browsers!

For IE11 you need to polyfill Object.assign and String.includes, you can do that as described in #90

Let me know on twitter: @haroenv, or in an issue.

Contributing

Contributions are always welcome! Here are some loose guidelines:

  • use feature branches
  • don't make it slower
  • explain why you want a feature
  • npm run doc to recreate the documentation

Building to a UMD is done via rollup (npm run build).

But I don't bite, if you have any questions or insecurities, hit me up for example on gitter.

License

Apache 2.0

More Repositories

1

floating.js

Float a number of things up on a page (hearts, flowers, emojis, words ...)
JavaScript
221
star
2

emojify-service

Convert your boring texts to nice emoji filled text on macOS!
JavaScript
20
star
3

ccna

A summary of CCNA-useable cisco commands by @elsmr and @Haroenv
CSS
18
star
4

brazzerify

a silly "add brazzers logo" site
HTML
17
star
5

no-facebook-emoji

Get rid of those ugly emojis now! [stopped working ๐Ÿ˜ข]
JavaScript
16
star
6

dev-ui

[WIP] Browser UI for npm and yarn (scripts, dependencies โ€ฆ)
JavaScript
15
star
7

read-one-article

A quarantine-ready Wikipedia game, to be played by two people
JavaScript
14
star
8

gulp-boilerplate-static

A simple boilerplate using gulp, jade, scss, browserify
JavaScript
11
star
9

where-am-i

microservice to get the city and country from last foursquare checkin
JavaScript
10
star
10

wedding-website

A website for @abisummers & my wedding
JavaScript
8
star
11

haroenv.github.io

A personal landing page for Haroen Viaene
HTML
7
star
12

dock-spacer

This is a wrapper for the `defaults` commands that changes some things in the OS X Dock
Shell
5
star
13

terribleme.me

a truly terrible meme
HTML
5
star
14

config

Some config files I use often (also some dotfiles)
Shell
4
star
15

is-dev

Check if you're on a development version or the live version of your site
JavaScript
4
star
16

NewBase60

A port of Tantek ร‡elik's NewBase60 in Swift (http://tantek.pbworks.com/NewBase60)
Swift
4
star
17

mars-status

A status dashboard for Mars. #hackthefuture 2016
JavaScript
4
star
18

jekyll-restaurant

Making a restaurant with a changing menu and hours with jekyll [WIP, no current plans of finishing]
CSS
3
star
19

instantsearch-templates-list

List of templates for InstantSearch
JavaScript
3
star
20

nuxt3-vue-instansearch-example

Vue
3
star
21

mac-processor-info

Open ark.intel.com with your processor (for mac's with intel processors)
Shell
3
star
22

modal

A simple modal at the center of a page in js
JavaScript
3
star
23

alti

A rocket altimeter using the device's gyroscope
HTML
2
star
24

kilometers-ios

The Kilometers app [WIP]
Swift
2
star
25

algolia-blog

The blog for during my internship at Algolia.
HTML
2
star
26

movies

A place to find a movie in your IMDb watchlist
JavaScript
2
star
27

emojibeth

converter from the latin alphabet to lookalike emojis (and more)
HTML
2
star
28

marriage-gift-oui

A mariage gift for a friend
JavaScript
2
star
29

ci-prettier-test

Testing Github Actions fixing prettier
JavaScript
2
star
30

blog

My blog
HTML
2
star
31

dedent.macro

Babel-macro for dedenting template strings
JavaScript
2
star
32

bus

a tracker for the location of a bus
HTML
2
star
33

ris-ssr-mega-demo

Demo of React InstantSearch with React Router / Lazy loading
JavaScript
2
star
34

instantsearch-negative-refinement-list

Gives a list of items to exclude from the current search
TypeScript
2
star
35

notes-eoict

Various notes during the study electronics-ict at Odisee (Ghent) 2014-... (Mostly Dutch) [deprecated in favour of https://ikdoeict-notes.github.io]
PHP
2
star
36

gipod-to-geojson

GIPOD is a service of the Flemish government giving open data of traffic works. This fullfills its claim of being GeoJSON
JavaScript
1
star
37

react-instantsearch-persisting

InfiniteHits, where items do not get lost on navigation
JavaScript
1
star
38

lel.news

The repository for lel news
HTML
1
star
39

notice

A simple notice banner at the top of a page in js
JavaScript
1
star
40

angular-instantsearch-test

This is just a demo repository on how to use InstantSearch (beta) with Angular
TypeScript
1
star
41

algolia-widget-error-sample

TypeScript
1
star
42

thesis

Reimagining InstantSearch.js as a framework-agnostic library, a bachelor thesis
TeX
1
star
43

instantsearch-demo-debounce

demo for comparing instant vs. debounced vs. on enter search
CSS
1
star
44

spectrogram

JavaScript
1
star
45

alter-housing

second version of Project 1 for WebTech 1 at Odisee Ghent
HTML
1
star
46

nuxt-range-example

example for a user
JavaScript
1
star
47

algolia-technical-test

Algolia search with Vue
Vue
1
star
48

simple-jekyll

A base template for jekyll
HTML
1
star
49

tachyons-algolia

CSS toolkit based on tachyons.css, for Algolia branded projects
JavaScript
1
star
50

silent-acres-fest

Project 1 for WebTech 1 at Odisee (Ghent)
HTML
1
star
51

visit-gent

A very simple demo that uses Algolia and open data by Ghent to show touristic features [wip]
HTML
1
star
52

angular-instantsearch-angular-13

little demo showing angular 13 works in angular instantsearch
TypeScript
1
star
53

instantsearch-tagsbox-react

Replaced by InstantSearch labs:
JavaScript
1
star
54

jsdoc-aas

A microservice to transform jsdoc annotated js to markdown
JavaScript
1
star
55

firebase-chat-demo

A demo chat app with Firebase
JavaScript
1
star
56

ci-test-repo

1
star
57

cv

The place where my resumรฉ lives. Written in HTML, exported to pdf
HTML
1
star
58

subtree-test

A test how to use travis to do a subtree push to github pages
HTML
1
star
59

netlify-lambda-test

demo of open search for algolia (with netlify functions)
JavaScript
1
star
60

presentations

Some presentations I've given
JavaScript
1
star
61

nuxt-prefetch-test

test for using serverPrefetch with a nested lib
JavaScript
1
star
62

Toledo

A learning platform made as exercise in Laravel
PHP
1
star
63

trip

Blog about my trip through Belgium
HTML
1
star