unified

Project: unified-doc/unified-doc

Package: unified-doc-search-micromatch@1.0.8

  1. Dependents: 0
  2. unified-doc micromatch search algorithm.
  1. unified 181
  2. unist 132
  3. hast 74
  4. file 34
  5. content 20
  6. document 10
  7. doc 8
  8. search 6
  9. nlp 6

unified-doc-search-micromatch

unified-doc micromatch search algorithm.

Install

npm install unified-doc-search-micromatch

Use

Supports the matching features in the micromatch package when searching doc instances managed by unified-doc.

import search from 'unified-doc-search-micromatch';

const content = 'a TO the b TO the c';

expect(search(content, 'TO', { nocase: true })).toEqual([
  { start: 2, end: 4, value: 'TO' },
  { start: 11, end: 13, value: 'TO' },
]);

expect(search(content, 'to', { nocase: false })).toEqual([]);

expect(search(content, 'a*b')).toEqual([
  { start: 0, end: 10, value: 'a TO the b' },
]);

expect(search(content, 'a????')).toEqual([
  { start: 0, end: 5, value: 'a TO ' },
]);

expect(search(content, 'a TO the (b|c)')).toEqual([
  { start: 0, end: 10, value: 'a TO the b' },
]);

expect(search(content, 'a TO the (c|d)')).toEqual([]);

expect(search(content, 'a TO the !(c|d)')).toEqual([
  { start: 0, end: 9, value: 'a TO the ' },
]);

API

search(content, query[, options])

Interface

function search(
  content: string,
  query: string,
  options?: Record<string, any>,
): SearchResult[]

Uses the unified SearchAlgorithm interface to search on content with a provided query string and micromatch-specific options. Returns unified SearchResult data.

Please refer to the micromatch documentation for configurable options and match behaviors.

type SearchAlgorithm = (
  /** string content to search on */
  content: string,
  /** query string */
  query: string,
  /** search algorithm options */
  options?: Record<string, any>,
) => SearchResult[];

interface SearchResult {
  /** start offset of the search result relative to the `textContent` of the `doc` */
  start: number;
  /** end offset of the search result relative to the `textContent` of the `doc` */
  end: number;
  /** matched text value in the `doc` */
  value: string;
  /** additional data can be stored here */
  data?: Record<string, any>;
}