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

keyword-highlighter

v1.0.6

Published

Highlight a set of keywords in the body of text in your search results

Downloads

1,073

Readme

Search Result Keyword Highlighter

Build Status codecov.io Code Climate Dependency Status devDependency Status

Highlight a keyword in the body of text for a given search result.

Why?

People who use search want to have the word(s) they searched for highlighted in the results. We know this. (because Google does it)

google-search-result-keyword-bold

Google have user-tested every feature of their site/experience...

What?

For a given set of keywords that a person is searching for, we highlight all occurrences of the word in the text of the search results.

Features?

Case-insentive Keyword Highlighting

a search for Java, javA or java will highlight JAVA or JaVa in the search result.

Finds Multiple Occurrences of the Same Keyword in Text

Example:

var text = 'London Bridge is Falling Down FALLING down, fALLiNG Down!';
var highlighted = h('falling', text);
console.log(highlighted);

The resulting highlighted

London Bridge is <b class='highlight'>Falling</b> Down
<b class='highlight'>FALLING</b> down,
<b class='highlight'>fALLiNG</b> Down!

Partial match for Keyword in Text

Imagine you are looking for PHP and someone has listed phpunit in their recent experience, this would be highliged as:

We used <b class='highlight'>php</b>unit for our TDD.

Match Several Distinct Keywords

If you pass in a String which contains multiple keywords, it will be split() into an Array and then each word will be highlighted in the text:

How?

#### Install the module from NPM:

npm install keyword-highlighter --save

Use it in your code:

var highlighter = require('keyword-highlighter');
var text   = 'I write JavaScript Code in Lovely London!';
var result = highlighter('javascript london', text);
console.log(result);

This will output:

I write <b class='highlight'>JavaScript</b>
Code in Lovely <b class='highlight'>London</b>!

Done.

Discussion:

Is it Fast?

Try it! And Judge for yourself!

Should we only sub complete words...?

Consider the following example:

var highlighter = require('keyword-highlighter');
var text = 'She sells sea shells on the sea shore.';
var result = highlighter('she sea', text);
console.log(result);

This will output:

<b class='highlight'>She</b> sells
<b class='highlight'>sea</b> <b class='highlight'>she</b>lls
on the <b class='highlight'>sea</b> shore.

I'm not convinced its useful to highlight she in the string shell ... you? Please discuss