1.0.3 • Published 2 years ago

@shhhplus/text-matcher v1.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

text-matcher

GitHub license npm version codecov build status

This tool can use specified rules to match text. If you want to use it in React, you can consider choosing @shhhplus/react-text-matcher.

Install

npm install @shhhplus/text-matcher --save

Usage

Basic

import createTextMatcher from '@shhhplus/text-matcher';

const rules = ['everyone', 'birthday'];
const tm = createTextMatcher(rules);
const result = tm.exec('Welcome everyone to come and join my birthday party.');

// result:
[
  'Welcome ',
  {
    text: 'everyone',
    payload: { position: 8, ruleIndex: 0, matchIndex: 0 },
  },
  ' to come and join my ',
  {
    text: 'birthday',
    payload: { position: 37, ruleIndex: 1, matchIndex: 1 },
  },
  ' party.',
];

RegExp

import createTextMatcher from '@shhhplus/text-matcher';

const rules = [new RegExp('apple', 'gi'), new RegExp('food')];
const tm = createTextMatcher(rules);
const result = tm.exec('apple_sun_banana_Apple_sun_food_sun');

// result:
[
  {
    text: 'apple',
    payload: { position: 0, ruleIndex: 0, matchIndex: 0 },
  },
  '_sun_banana_',
  {
    text: 'Apple',
    payload: { position: 17, ruleIndex: 0, matchIndex: 1 },
  },
  '_sun_',
  {
    text: 'food',
    payload: { position: 27, ruleIndex: 1, matchIndex: 2 },
  },
  '_sun',
];

Parameters

nametype
rulesstring | RegExp | Array<string | RegExp>