Skip to content
English

Normalization:有顺序的值转换 ​

把清理、替换和停止条件集中为一份实际参与执行的转换清单。

输入是有序 NormalizationStep 清单;normalize(value, context) 返回最后一个值。每步可返回新值,也可选择原地修改;对象所有权由 App 决定。when 与 isComplete 必须同步返回 boolean,normalize 必须同步。重复或空步骤 ID、缺少 handler、误返回 Promise 都会拒绝。

uniqueBy 按 App 提供的 key 稳定去重,保留第一次出现的原值;initial 可以排除已经见过的键。它不会猜测两个业务对象是否等价。

由简到繁的同业务示例 ​

先只清理同一段查询 ​

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

策略与实现 ​

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