UNPKG

segment-matcher

Version:

Segment Matcher - TypeScript version with dual ESM/CJS format support

88 lines (87 loc) 2.9 kB
import { MessageSegment } from './types'; import { PatternToken } from './pattern_token'; import { MatchResult } from './match_result'; /** * Segment Matcher 主类 * * 用于解析和匹配消息段的匹配器。 * 支持复杂的模式匹配、参数提取和链式回调处理。 * * ``` */ export declare class SegmentMatcher { /** 解析后的模式令牌数组 */ private tokens; /** 类型化字面量的字段映射配置 */ private typedLiteralFields; /** * 默认的类型化字面量字段映射规则 * * 定义了不同消息段类型对应的数据字段: * - text: 使用 'text' 字段 * - face: 使用 'id' 字段 * - image: 使用 'file' 或 'url' 字段(支持多字段) * - at: 使用 'user_id' 字段 */ static DEFAULT_TYPED_LITERAL_FIELD_MAP: Record<string, string | string[]>; static create(pattern: string, typedLiteralFields?: Record<string, string | string[]>): SegmentMatcher; /** * 构造函数 * * @param pattern - 命令模式字符串,定义匹配规则 * @param typedLiteralFields - 自定义的类型化字面量字段映射(可选) * * @throws {ValidationError} 当模式为空或格式错误时抛出 * * @example * ```typescript * // 基础用法 * const commander = new Commander('hello <name:text>'); * * // 自定义字段映射 * const customCommander = new Commander('{image:avatar.png}<name:text>', { * image: 'src' // 使用 'src' 字段而不是默认的 'file' 或 'url' * }); * ``` */ constructor(pattern: string, typedLiteralFields?: Record<string, string | string[]>); /** * 匹配消息段 * */ match(segments: MessageSegment[]): MatchResult | null; /** * 获取解析后的模式令牌 * * 主要用于调试和测试,返回模式解析器生成的令牌数组。 * * @returns 模式令牌数组 * * @example * ```typescript * const commander = new Commander('hello <name:text>'); * const tokens = segmentMatcher.getTokens(); * console.log(tokens); // 显示解析后的令牌结构 * ``` */ getTokens(): PatternToken[]; } /** * 便捷函数:创建命令匹配器实例 * * 这是一个工厂函数,提供更简洁的 API 来创建 Commander 实例。 * * @param pattern - 命令模式字符串 * @param typedLiteralFields - 自定义的类型化字面量字段映射(可选) * @returns 新创建的 Commander 实例 * * @example * ```typescript * // 使用便捷函数创建实例 * const commander = match('hello <name:text>'); * * // 等同于 * const commander = new Commander('hello <name:text>'); * ``` */ export declare function createMatcher(pattern: string, typedLiteralFields?: Record<string, string | string[]>): SegmentMatcher;