Skip to content
中文

matchRule ​

CORE · function · Runtime value

Dispatch to the first matching rule.

A rule without a pattern matches unconditionally, so the table's order decides precedence. The first match owns the result even when its handler returns undefined, and later rules are not consulted.

Import ​

ts
import { matchRule } from '@jangts/agent-core/support';
js
import { matchRule } from '@jangts/agent-core/support';

Other public entry points:@jangts/agent-core, @jangts/agent-core/types, @jangts/agent-core/support.

Definition ​

Type parameters ​

Type parameterConstraintDefault type
Input——
Result——

Call ​

ts
matchRule<Input, Result>(rules: readonly MatchRule<Input, Result>[], input: Input): Result | undefined

Dispatch to the first matching rule.

A rule without a pattern matches unconditionally, so the table's order decides precedence. The first match owns the result even when its handler returns undefined, and later rules are not consulted.

Type parameterConstraintDefault type
Input——
Result——
ParameterTypePassingDescription
rulesreadonly MatchRule<Input, Result>[]Required—
inputInputRequired—

Returns: Result \| undefined.

Awaited value: Awaited<Result> \| undefined.

Examples ​

One explicit rejection ​

Beginner · 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);

Checks with dependencies evaluated in order ​

Configuration · 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);

Declarative priority dispatch and sequential transformation ​

Extension · Node.js

ts
import { Support } from '@jangts/agent-core';
const routeName = Support.matchRule([
  { id: 'status', description: '状态意图', pattern: (input: string) => input === 'status', handle: () => 'service' },
  { id: 'fallback', description: '其他意图', handle: () => 'reply' },
], 'status');
const state = { answer: '  服务正常  ' };
Support.walkRules([
  { handle: (value: typeof state) => { value.answer = value.answer.trim(); } },
  { pattern: (value: typeof state) => !!value.answer, handle: value => { value.answer += '。'; } },
], state);
console.log(routeName, state.answer);
js
import { Support } from '@jangts/agent-core';
const routeName = Support.matchRule([
    { id: 'status', description: '状态意图', pattern: (input) => input === 'status', handle: () => 'service' },
    { id: 'fallback', description: '其他意图', handle: () => 'reply' },
], 'status');
const state = { answer: '  服务正常  ' };
Support.walkRules([
    { handle: (value) => { value.answer = value.answer.trim(); } },
    { pattern: (value) => !!value.answer, handle: value => { value.answer += '。'; } },
], state);
console.log(routeName, state.answer);

API reference, usage examples and developer guides.