Skip to main content

BuildTrieOptions

Options that apply to the trie globally.
type BuildTrieOptions = {
  normalizeApostrophes?: boolean;
};
normalizeApostrophes
boolean
When true, treats all apostrophe-like characters as equivalent during matching. This allows a rule with “don’t” to match “don’t”, “don’t”, “don`t”, etc.

ConfirmCallback

A callback function used to determine if a rule should be considered based on confirmation options.
type ConfirmCallback = (confirmOptions: ConfirmOptions) => boolean;
confirmOptions
ConfirmOptions
required
The options specifying the conditions for confirmation.
return
boolean
A boolean indicating whether the rule is considered (true) or not (false).

OptimizeResult

Result of rule optimization including optimized rules and statistics.
type OptimizeResult = {
  optimizedRules: Rule[];
  savings: {
    rulesRemoved: number;
    sourcesRemoved: number;
  };
  warnings: {
    conflicts: Array<{
      from: string;
      conflictingTo: string[];
    }>;
    matchTypeConflicts: Array<{
      from: string;
      rules: Rule[];
    }>;
    overwrittenRules: Array<{
      from: string;
      kept: Rule;
      discarded: Rule[];
    }>;
  };
};
optimizedRules
Rule[]
The optimized array of rules with redundancies removed and options consolidated.
savings
object
Statistics about the optimization process.
warnings
object
Warnings about potential issues found during optimization.

Rule

Represents a single search and replace rule. Defines the source words to search for and the target replacement.
type Rule = {
  from: string[];
  to: string;
  options?: RuleOptions;
};
from
string[]
required
An array of strings representing the words or patterns to search for. Each string in the array is treated as an individual search target.
to
string
required
The target string to replace the matched source words with. Can include additional formatting or contextual information.
options
RuleOptions
Options that modify the behavior of the rule, such as casing and clipping.

RuleOptions

Options that define how a rule behaves during search and replace operations.
type RuleOptions = {
  casing?: CaseSensitivity;
  clipEndPattern?: RegExp | TriePattern;
  clipStartPattern?: RegExp | TriePattern;
  confirm?: ConfirmOptions;
  match?: MatchType;
  prefix?: string;
};
casing
CaseSensitivity
Specifies how casing should be handled during replacement. Defaults to case-sensitive if not provided.
clipEndPattern
RegExp | TriePattern
A regular expression or predefined pattern to determine characters to clip at the end of a match. Useful for removing trailing punctuation or symbols.
clipStartPattern
RegExp | TriePattern
A regular expression or predefined pattern to determine characters to clip at the start of a match. Useful for removing leading punctuation or symbols.
confirm
ConfirmOptions
Options that determine whether the rule should be confirmed before being applied.
match
MatchType
Specifies the type of match required for the rule to be applied. Determines the context in which the rule should match words.
prefix
string
A prefix string to be added before the replacement text. Useful for maintaining consistent formatting or adding necessary context.

SearchAndReplaceOptions

Options for configuring search and replace operations.
type SearchAndReplaceOptions = {
  confirmCallback?: ConfirmCallback;
  log?({ node }: { node: TrieNode }): void;
};
confirmCallback
ConfirmCallback
A callback function that determines whether a rule should be considered based on confirmation options. Allows for dynamic decision-making during the replacement process.
log
function
A logging function that receives information about the current node being processed. Useful for debugging or tracking the replacement process.

TrieNode

Represents a node within a trie data structure used for efficient search and replace operations.
type TrieNode = {
  [key: string]: boolean | BuildTrieOptions | RuleOptions | string | TrieNode | undefined;
  buildOptions?: BuildTrieOptions;
  isEndOfWord?: boolean;
  options?: RuleOptions;
  target?: string;
};
[key: string]
boolean | BuildTrieOptions | RuleOptions | string | TrieNode | undefined
An index signature allowing dynamic properties. Each key represents a character, and the value can be:
  • A boolean indicating if it’s the end of a word.
  • RuleOptions modifying the rule’s behavior.
  • A string representing the target replacement.
  • Another TrieNode for nested characters.
  • Undefined if the character path does not exist.
buildOptions
BuildTrieOptions
Build options stored at the trie root to be used during search operations. Only present on the root node.
isEndOfWord
boolean
Indicates whether the current node marks the end of a complete word. Useful for determining when a match is found.
options
RuleOptions
Options associated with the rule at this node. Can modify behavior such as casing, clipping, and matching types.
target
string
The target replacement string for the rule at this node. Defines what the matched word should be replaced with.

Build docs developers (and LLMs) love