WalkRule
CORE · interface · 类型合同
Synchronous traversal; local rules may omit catalog metadata.
导入
ts
import type { WalkRule } from '@jangts/agent-core/support';js
/** @typedef {import('@jangts/agent-core/support').WalkRule} WalkRule */其他公开入口:@jangts/agent-core、@jangts/agent-core/types、@jangts/agent-core/structs、@jangts/agent-core/support。
定义
ts
interface WalkRule<Input>继承或实现: Partial<MatchRule<Input, unknown>>。
泛型参数
| 类型参数 | 约束 | 默认类型 |
|---|---|---|
Input | — | void |
字段结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
handle | (input: Input) => unknown | 必需 | 只读。Runs when the rule matches; must return synchronously. |
before | ((input: Input) => void) | undefined | 可省略 | 只读。Runs before matching, including when the rule does not match. |
id | string | undefined | 可省略 | 只读。Unique rule id used in diagnostics. |
description | string | undefined | 可省略 | 只读。见类型定义及调用场景。 |
pattern | ((input: Readonly<Input>) => boolean) | undefined | 可省略 | 只读。Omission is an unconditional match at this position, normally the final fallback. |
成员目录
| 成员 | 用途 |
|---|---|
handle | Runs when the rule matches; must return synchronously. |
成员详情
handle
Runs when the rule matches; must return synchronously.
ts
WalkRule.handle(input: Input): unknown| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
input | Input | 必需 | — |
返回: unknown。
使用示例
- 拒绝检查、首个匹配与顺序处理:使用真实清单表达多项检查和枚举分发;单个普通判断仍可直接使用 if。
一个明确的拒绝
入门 · 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);