2.0.2 • Published 1 year ago

search.ts v2.0.2

Weekly downloads
456
License
ISC
Repository
github
Last release
1 year ago

search.ts

Searches for the target string in the string. Use the Aho-Corasick automaton when search many words. Use the KMP when search single word.

How To Use

npm install search.ts

import { buildSearch } from 'search.ts'

// when you want to search many words
const article = 'She is a so beautiful girl.'
const searcher = buildSearch(['She', 'he', 'girl', 'beautiful'])
console.log(searcher.search(article))
/*
* [
*   0: {word: "She", start: 0, end: 2}
*   1: {word: "he", start: 1, end: 2}
*   2: {word: "beautiful", start: 12, end: 20}
*   3: {word: "girl", start: 22, end: 25}
* ]
*/
// when you want to search single word
const kmpSearcher = buildSearch('She')
console.log(searcher.search(article))
/*
* [
*   0: {start: 0, end: 2}
* ]
*/

Document

API

  • buildSearch

    • buildSearch(searchStr: string | string[]): Search (it will build a match-words-tree and returns an object with search func.)
      • searchStr: The string what you want to find. Can be single word or word array.
      • Search: An object with search function.
  • search

    • search(content: string, formatter?: Formatter): SearchResult []
      • content: The original string.
      • formatter: The custom function. Please refer to the documentation for details.
      • SearchResult: The search result array. You can pass function 'formatter' to change its structure.
  • formatter (You can pass this function to customize search result)

    • formatter(result: [], word: string, start: number, end: number): void
      • result: The function 'search' return
      • word: Matched word
      • start: Matched word start index in content
      • end: Matched word end index in content
  • treeDepthFirstSearch (depth first search in tree)
    • treeDepthFirstSearch<T extends { propName: string: any }>(tree: T, describe: Describe, childKey: string)
      • tree: A tree-shape object which every node may has children.
      • describe: The function with every node as param with boolean return, using for find target node.
      • childKey: Every node's children key, default is 'children'.
  • treeBreadthFirstSearch (breadth first search in tree)
    • treeBreadthFirstSearch<T extends { propName: string: any }>(tree: T, describe: Describe, childKey: string)
      • tree: A tree-shape object which every node may has children.
      • describe: The function with every node as param with boolean return, using for find target node.
      • childKey: Every node's children key, default is 'children'.

Attribute

SearchResult {
    word?: string,
    start?: number,
    end?: number,
}

Formatter = (result: SearchResult[], word: string, start: number, end: number) => void

Search {
    search(content: string, formatter?: Formatter): SearchResult[]
}
2.0.2

1 year ago

2.0.1

3 years ago

2.0.0-beta.1

3 years ago

2.0.0

3 years ago

1.3.3

3 years ago

1.3.2

3 years ago

1.3.2-beta.1

3 years ago

1.3.1

3 years ago

1.3.0

3 years ago

1.2.1-beta.1

3 years ago

1.2.1-beta.2

3 years ago

1.2.0

3 years ago

1.2.3

3 years ago

1.2.4

3 years ago

1.2.3-beta.3

3 years ago

1.2.3-beta.1

3 years ago

1.2.3-beta.2

3 years ago

1.2.2

3 years ago

1.2.1

3 years ago

1.1.5

3 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.1.0-beta.1

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago

0.1.3-beta

3 years ago

0.1.4-beta

3 years ago

0.1.2-beta

3 years ago

0.1.1-beta

3 years ago

0.1.0-beta

3 years ago