uniqueBy
KIT · function · 运行时值
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.
导入
ts
import { uniqueBy } from '@jangts/agent-core/kits/normalization';js
import { uniqueBy } from '@jangts/agent-core/kits/normalization';其他公开入口:@jangts/agent-core/types、@jangts/agent-core/kits/normalization、@jangts/agent-core/kits。
定义
泛型参数
| 类型参数 | 约束 | 默认类型 |
|---|---|---|
Value | — | — |
Key | — | — |
调用
ts
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 | — | — |
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
values | readonly Value[] | 必需 | — |
key | (value: Value) => Key | 必需 | — |
initial | Iterable<Key> | 可省略 | — |
返回: 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));