Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

programmingGu/search.ts

Open more actions menu
 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

66 Commits
66 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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[]
}

About

Searches for the target string in the string

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • TypeScript 66.7%
  • JavaScript 33.3%
Morty Proxy This is a proxified and sanitized view of the page, visit original site.