Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

hit-highlighter

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hit-highlighter

Small and simple hit highlighter for search engines running in the browser and Node.js

  • 1.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
12
increased by20%
Maintainers
1
Weekly downloads
 
Created
Source

hit-highlighter

A small and simple hit highlighter for search engines running in the browser and Node.js. Takes a query array and where the values matches within the item array, it adds hightight code. Goes well with words'n'numbers for extracting words (and numbers) from a string of text.

NPM version NPM downloads MIT License Build Status JavaScript Style Guide

Browser demo

Browser demo

Check out the interactive demo.

Initialize

Browser

<script src="hit-highlighter.js"></script>

<script>
  //highlight() available
</script>

Node.js

const highlight = require ('hit-highlight')
// highlight() available

Usage

hightlight([query array], [item array], {highlighting object})

query and item are arrays of words. hightlighting is an object defining start and end tag for what is highlighted.

highlighting.start defaults to <span class="highlighted"> highlighting.end defaults to </span>

Default highlighting

const query = ['interesting', 'words']
const item = ['some', 'interesting', 'words', 'to', 'remember']

highlight(query, item)

// returns:
//[
//  'some',
//  '<span class="highlighted">interesting</span>',
//  '<span class="highlighted">words</span>',
//  'to',
//  'remember'
//]

Custom highlight.start and .end

const query = ['interesting', 'words']
const item = ['some', 'interesting', 'words', 'to', 'remember']
const highlighting = { start: '**', end: '**' }

highlight(query, item, highlighting)

// returns:
//[
//  'some',
//  '**interesting**',
//  '**words**',
//  'to',
//  'remember'
//]

No hits, returing item untouched

const query = ['no', 'hits']
const item = ['some', 'interesting', 'words', 'to', 'remember']

highlight(query, item)

// returns:
//[
//  'some',
//  'interesting',
//  'words',
//  'to',
//  'remember'
//]

Keywords

FAQs

Package last updated on 21 Sep 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc