1.0.0 • Published 6 years ago

text-finder v1.0.0

Weekly downloads
3
License
MIT
Repository
github
Last release
6 years ago

text-finder

text-finder Search "registered words" in string. Word registering is easy, and search is fast.

Install

npm install text-finder

Usage

simple

const textfinder = require('text-finder');

(async () => {
  // setup is async. (return Promise<void>)
  await textfinder.setup(['apple', 'orange', 'remon']);
  
  const result = textfinder.findOne('I like orange');
  // result: {string: 'orange', start: 7, end: 13}
})();

customize character mapping

"character mapping" means Translate as a other character.

ex) lower/upper case(A => a)

const {TextFinder} = require('./');
const textfinder = new TextFinder({A: 'a', O: 'o', R: 'r'});

(async () => {
  await textfinder.setup(['apple', 'orange', 'remon']);
  
  const result = textfinder.findOne('I like Remon');
  // result: {string: 'Remon', start: 7, end: 12}
})();

APIs

TextFinder#findOne

Returns the first word found.

const result = textfinder.findOne('I like orange and remon.');
// result: {string: 'orange', start: 7, end: 13}

TextFinder#find

Return the all word found as an array.

const result = textfinder.find('I like orange and remon.');
// result: [{string: 'orange', start: 7, end: 13}, {string: 'remon', start, 18, end: 23}]

Benchmark

function100words1000words10000words
Array#indexOf54,045 ops/s9,600 ops/s1,015 ops/s
TextFinder#findOne28,787 ops/s24,683 ops/s12,858 ops/s
TextFinder#find28,891 ops/s24,301 ops/s12,506 ops/s

LICENSE

MIT LICENSE LICENSE