Skip to content
中文

MatchRule ​

CORE · interface · Type contract

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

Import ​

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

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

Definition ​

ts
interface MatchRule<Input, Result>

Type parameters ​

Type parameterConstraintDefault type
Input——
Result—void

Field structure ​

FieldTypeRequiredDescription
idstringRequiredRead-only. Unique rule id used in diagnostics.
descriptionstringRequiredRead-only. See the type definition and usage context.
pattern((input: Readonly<Input>) => boolean) | undefinedOptionalRead-only. Omission is an unconditional match at this position, normally the final fallback.
handle(input: Input) => ResultRequiredRead-only. Produces the rule's result; only the first matching rule runs.

Member index ​

MemberPurpose
handleProduces the rule's result; only the first matching rule runs.

Member details ​

handle ​

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

ts
MatchRule.handle(input: Input): Result
ParameterTypePassingDescription
inputInputRequired—

Returns: Result.

Awaited value: Awaited<Result>.

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

API reference, usage examples and developer guides.