defineNormalization
KIT · function · 运行时值
Compile an ordered normalization pipeline.
Steps run in declaration order. Each step may be skipped by its when predicate, receives the value produced by the previous step, and isComplete can stop the walk early, so the usual contract is "first completion wins" rather than "always run everything".
导入
import { defineNormalization } from '@jangts/agent-core/kits/normalization';import { defineNormalization } from '@jangts/agent-core/kits/normalization';其他公开入口:@jangts/agent-core/types、@jangts/agent-core/kits/normalization、@jangts/agent-core/kits。
定义
泛型参数
| 类型参数 | 约束 | 默认类型 |
|---|---|---|
Value | — | — |
Context | — | void |
调用
defineNormalization<Value, Context = void>(definitions: readonly NormalizationStep<Value, Context>[], options?: NormalizationOptions<Value, Context>): Readonly<{ steps: readonly Readonly<{ id: string; when: ((value: Value, context: Context) => boolean) | undefined; normalize: (value: Value, context: Context) => Value; }>[]; normalize(value: Value, context: Context): Value; }>Compile an ordered normalization pipeline.
Steps run in declaration order. Each step may be skipped by its when predicate, receives the value produced by the previous step, and isComplete can stop the walk early, so the usual contract is "first completion wins" rather than "always run everything".
| 类型参数 | 约束 | 默认类型 |
|---|---|---|
Value | — | — |
Context | — | void |
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
definitions | readonly NormalizationStep<Value, Context>[] | 必需 | — |
options | NormalizationOptions<Value, Context> | 可省略 | — |
options 的字段
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
isComplete | ((value: Value, context: Context) => boolean) | undefined | 可省略 | 只读。Stops the walk after the step that first returns true; later steps then do not run. |
返回: Readonly<{ steps: readonly Readonly<{ id: string; when: ((value: Value, context: Context) => boolean) \| undefined; normalize: (value: Value, context: Context) => Value; }>[]; normalize(value: Value, context: Context): Value; }>。
返回值结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
steps | readonly Readonly<{ id: string; when: ((value: Value, context: Context) => boolean) | undefined; normalize: (value: Value, context: Context) => Value; }>[] | 必需 | 见类型定义及调用场景。 |
normalize | (value: Value, context: Context) => Value | 必需 | 见类型定义及调用场景。 |
使用示例
- Normalization:有顺序的值转换:把清理、替换和停止条件集中为一份实际参与执行的转换清单。
先只清理同一段查询
入门 · Node.js
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));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
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));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));