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

extract-highlight-text

v2.0.1

Published

高性能多文本关键字匹配模块,用于前端文本高亮

Downloads

6

Readme

extract-highlight-text

基于字典树数据结构的多关键字查找匹配模块,用于前端文本高亮,时间复杂度为O(N)

安装

npm install extract-highlight-text --save

特性

  1. 支持模糊匹配(忽略大小写)
  2. 对于比较庞大的目标字符串拥有良好的性能

使用

import sliceTextByKeywords from 'extract-highlight-text';
const keywords = ['Apple'];
const result = sliceTextByKeywords({
    text: 'I have a Apple',
    // 开启模糊匹配
    enableLazyMatch: true,
    keywords: keywords
});

以上代码会输出如下内容, type为highlight表示匹配成功的子串

  // result
  [
    {
      type: 'normal',
      text: 'I have a ',
      start: 0,
      end: 9
    }, {
      type: 'highlight',
      text: 'Apple',
      start: 9,
      end: 14
    }
  ]

有时会对一批文本高亮同样的关键词组,可以先对关键字构造好字典树,避免匹配时多次重复构造,提高效率

import sliceTextByKeywords, { buildTrieTree } from 'extract-highlight-text';
const keywords = ['明月', '地上'];
const trieTreeRoot = buildTrieTree(keywords);

const result1 = sliceTextByKeywords({
    text: '床前明月光,疑似地上霜',
    keywords: keywords,
    trieTreeRoot: trieTreeRoot
});

const result2 = sliceTextByKeywords({
    text: '举头望明月,低头思故乡',
    keywords: keywords,
    trieTreeRoot: trieTreeRoot
});

trieTreeRoot结构如下

{
    '明': {
        '月': { isLeaf: true} // 认为是叶子节点
    },
    '地': {
        '上': { isLeaf: true}
    }
}

以上代码会输出如下内容, type为highlight表示匹配成功的子串

  // result1
  [
    { type: 'normal', text: '床前', start: 0, end: 2 },
    { type: 'highlight', text: '明月', start: 2, end: 4 },
    { type: 'normal', text: '光,疑似', start: 4, end: 8 },
    { type: 'highlight', text: '地上', start: 8, end: 10 },
    { type: 'normal', text: '霜', start: 10, end: 11 }
  ]

  // result2
  [
    { type: 'normal', text: '举头望', start: 0, end: 3 },
    { type: 'highlight', text: '明月', start: 3, end: 5 },
    { type: 'normal', text: ',低头思故乡', start: 5, end: 11 }
  ]