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