NormalizationStep
KIT · interface · 类型合同
One ordered transformation.
导入
ts
import type { NormalizationStep } from '@jangts/agent-core/kits/normalization';js
/** @typedef {import('@jangts/agent-core/kits/normalization').NormalizationStep} NormalizationStep */其他公开入口:@jangts/agent-core/types、@jangts/agent-core/kits/normalization、@jangts/agent-core/kits。
定义
ts
interface NormalizationStep<Value, Context>泛型参数
| 类型参数 | 约束 | 默认类型 |
|---|---|---|
Value | — | — |
Context | — | — |
字段结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
id | string | 必需 | 只读。Unique, non-blank step id used in errors. |
when | ((value: Value, context: Context) => boolean) | undefined | 可省略 | 只读。Skips the step when it returns false; must be synchronous boolean. |
normalize | (value: Value, context: Context) => Value | 必需 | 只读。Returns the next value; receives the output of the previous step. Must be synchronous. |
成员目录
| 成员 | 用途 |
|---|---|
normalize | Returns the next value; receives the output of the previous step. Must be synchronous. |
成员详情
normalize
Returns the next value; receives the output of the previous step. Must be synchronous.
ts
NormalizationStep.normalize(value: Value, context: Context): Value| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
value | Value | 必需 | — |
context | Context | 必需 | — |
返回: Value。
等待后的值: Awaited<Value>。
使用示例
- Normalization:有顺序的值转换:把清理、替换和停止条件集中为一份实际参与执行的转换清单。
先只清理同一段查询
入门 · Node.js
ts
import { Normalization } from '@jangts/agent-core/kits/normalization';
const query = Normalization.defineNormalization<string>([
{ id: 'trim', normalize: value => value.trim() },
]);
console.log(query.normalize(' Agent Core ', undefined));js
import { Normalization } from '@jangts/agent-core/kits/normalization';
const query = Normalization.defineNormalization([
{ id: 'trim', normalize: value => value.trim() },
]);
console.log(query.normalize(' Agent Core ', undefined));继续增加步骤和停止条件
配置 · Node.js
ts
import { Normalization } from '@jangts/agent-core/kits/normalization';
const query = Normalization.defineNormalization<string>([
{ id: 'trim', normalize: value => value.trim() },
{ id: 'collapse-space', normalize: value => value.replace(/\s+/g, ' ') },
{ id: 'lowercase', when: value => value !== '*', normalize: value => value.toLowerCase() },
], { isComplete: value => value === '*' });
const values = [' Agent Core ', 'agent core', '*'].map(value => query.normalize(value, undefined));
console.log(Normalization.uniqueBy(values, value => value));js
import { Normalization } from '@jangts/agent-core/kits/normalization';
const query = Normalization.defineNormalization([
{ id: 'trim', normalize: value => value.trim() },
{ id: 'collapse-space', normalize: value => value.replace(/\s+/g, ' ') },
{ id: 'lowercase', when: value => value !== '*', normalize: value => value.toLowerCase() },
], { isComplete: value => value === '*' });
const values = [' Agent Core ', 'agent core', '*'].map(value => query.normalize(value, undefined));
console.log(Normalization.uniqueBy(values, value => value));