Skip to content
English

MatchRule ​

CORE · interface · 类型合同

Order is priority. Patterns may inspect values, types, state or regular expressions.

导入 ​

ts
import type { MatchRule } from '@jangts/agent-core/support';
js
/** @typedef {import('@jangts/agent-core/support').MatchRule} MatchRule */

其他公开入口:@jangts/agent-core、@jangts/agent-core/types、@jangts/agent-core/structs、@jangts/agent-core/support。

定义 ​

ts
interface MatchRule<Input, Result>

泛型参数 ​

类型参数约束默认类型
Input——
Result—void

字段结构 ​

字段类型必需说明
idstring必需只读。Unique rule id used in diagnostics.
descriptionstring必需只读。见类型定义及调用场景。
pattern((input: Readonly<Input>) => boolean) | undefined可省略只读。Omission is an unconditional match at this position, normally the final fallback.
handle(input: Input) => Result必需只读。Produces the rule's result; only the first matching rule runs.

成员目录 ​

成员用途
handleProduces the rule's result; only the first matching rule runs.

成员详情 ​

handle ​

Produces the rule's result; only the first matching rule runs.

ts
MatchRule.handle(input: Input): Result
参数类型传递说明
inputInput必需—

返回: Result。

等待后的值: Awaited<Result>。

使用示例 ​

一个明确的拒绝 ​

入门 · Node.js

ts
import { Support } from '@jangts/agent-core';
const input = '你好';
Support.breakWhile(!input.trim(), '问题不能为空');
console.log(input);
js
import { Support } from '@jangts/agent-core';
const input = '你好';
Support.breakWhile(!input.trim(), '问题不能为空');
console.log(input);

有依赖关系的检查按序求值 ​

配置 · Node.js

ts
import { Support } from '@jangts/agent-core';
const input: unknown = { name: 'Ivan' };
Support.breakWhile([
  { occur: !input || typeof input !== 'object', error: '需要对象' },
  { occur: () => !('name' in (input as object)), error: '缺少 name' },
]);
console.log(input);
js
import { Support } from '@jangts/agent-core';
const input = { name: 'Ivan' };
Support.breakWhile([
    { occur: !input || typeof input !== 'object', error: '需要对象' },
    { occur: () => !('name' in input), error: '缺少 name' },
]);
console.log(input);

相关策略源码 ​

接口参考、使用示例与开发指南。