Skip to content
English

breakWhile ​

CORE · function · 运行时值

Reject as soon as one of several conditions holds.

Ordinary processing stays an ordinary if; this helper is for validation tables whose entries are evaluated in order and stop at the first failure. Behaviour that depends on earlier entries is expressed with a callback form. Reject when any predicate holds, reporting the given observer tags. Reject when any structured check holds.

Each check may carry its own reason and tags; the first match stops the evaluation. Reject when a single value is truthy.

Ordinary truthiness applies, so a function passed as a value counts as a failure; use the predicate form to evaluate it instead. Reject when a single value is truthy, reporting the given observer tags.

导入 ​

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

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

定义 ​

重载 1 ​

ts
breakWhile(occur: readonly BreakPredicate[], error?: BreakReason, tags?: readonly string[]): void

Reject as soon as one of several conditions holds.

Ordinary processing stays an ordinary if; this helper is for validation tables whose entries are evaluated in order and stop at the first failure. Behaviour that depends on earlier entries is expressed with a callback form.

参数类型传递说明
occurreadonly BreakPredicate[]必需—
errorBreakReason | undefined可省略—
tagsreadonly string[] | undefined可省略—

返回: void。

重载 2 ​

ts
breakWhile(occur: readonly BreakPredicate[], tags: readonly string[]): void

Reject when any predicate holds, reporting the given observer tags.

参数类型传递说明
occurreadonly BreakPredicate[]必需—
tagsreadonly string[]必需—

返回: void。

重载 3 ​

ts
breakWhile(checks: readonly BreakCheck[], tags?: readonly string[]): void

Reject when any structured check holds.

Each check may carry its own reason and tags; the first match stops the evaluation.

参数类型传递说明
checksreadonly BreakCheck[]必需—
tagsreadonly string[] | undefined可省略—

返回: void。

重载 4 ​

ts
breakWhile(occur: unknown, error?: BreakReason, tags?: readonly string[]): void

Reject when a single value is truthy.

Ordinary truthiness applies, so a function passed as a value counts as a failure; use the predicate form to evaluate it instead.

参数类型传递说明
occurunknown必需—
errorBreakReason | undefined可省略—
tagsreadonly string[] | undefined可省略—

返回: void。

重载 5 ​

ts
breakWhile(occur: unknown, tags: readonly string[]): void

Reject when a single value is truthy, reporting the given observer tags.

参数类型传递说明
occurunknown必需—
tagsreadonly string[]必需—

返回: void。

使用示例 ​

一个明确的拒绝 ​

入门 · 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);

声明式优先分发与顺序转换 ​

扩展 · 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);

相关策略源码 ​

相关定义 ​

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