Rejection checks, first match, and sequential processing
Use real lists to express multiple checks and enumerated dispatch; a single ordinary condition can still use if.
breakWhile(occur,error?) throws when a rejection condition is met; when the reason is omitted it creates an Error. A check array stops at the first rejection in order, conditions that depend on earlier checks use callbacks, and the reason factory only runs on rejection. The array field is still uniformly named occur, without inventing separate names for singular and plural.
Note: when passing breakWhile(() => bad) directly, the function itself is treated as a truthy value and is not evaluated; for a lazy condition use a check object {occur:()=>bad} or a predicate array [()=>bad].
matchRule returns the result of the first matching handler and does not continue even when that result is undefined. walkRules executes all matching items in order, and later items can observe earlier modifications; both predicates synchronously return boolean. A single normal condition does not need to be wrapped in a list.
Progressive examples for the same business case
One explicit rejection
Beginner · Node.js
import { Support } from '@jangts/agent-core';
const input = '你好';
Support.breakWhile(!input.trim(), '问题不能为空');
console.log(input);import { Support } from '@jangts/agent-core';
const input = '你好';
Support.breakWhile(!input.trim(), '问题不能为空');
console.log(input);Checks with dependencies evaluated in order
Configuration · Node.js
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);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
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);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);