Skip to content
English

Normalization ​

KIT · constant · 运行时值

导入 ​

ts
import { Normalization } from '@jangts/agent-core/kits/normalization';
js
import { Normalization } from '@jangts/agent-core/kits/normalization';

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

定义 ​

成员目录 ​

成员用途
defineNormalization完整输入输出见下方重载。
uniqueBy完整输入输出见下方重载。

成员详情 ​

defineNormalization ​

ts
Normalization.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
参数类型传递说明
definitionsreadonly NormalizationStep<Value, Context>[]必需—
optionsNormalizationOptions<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; }>。

返回值结构
字段类型必需说明
stepsreadonly Readonly<{ id: string; when: ((value: Value, context: Context) => boolean) | undefined; normalize: (value: Value, context: Context) => Value; }>[]必需见类型定义及调用场景。
normalize(value: Value, context: Context) => Value必需见类型定义及调用场景。

uniqueBy ​

ts
Normalization.uniqueBy<Value, Key>(values: readonly Value[], key: (value: Value) => Key, initial?: Iterable<Key>): Value[]

Stable deduplication preserves the original values; the host chooses equivalence.

The first occurrence of each key wins and the input order is kept, so callers can rely on deduplication never reordering their list.

类型参数约束默认类型
Value——
Key——
参数类型传递说明
valuesreadonly Value[]必需—
key(value: Value) => Key必需—
initialIterable<Key>可省略—

返回: Value[]。

使用示例 ​

先只清理同一段查询 ​

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

相关策略源码 ​

相关定义 ​

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