uniqueBy
KIT · function · Runtime 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.
Import
ts
import { uniqueBy } from '@jangts/agent-core/kits/normalization';js
import { uniqueBy } from '@jangts/agent-core/kits/normalization';Other public entry points:@jangts/agent-core/types, @jangts/agent-core/kits/normalization, @jangts/agent-core/kits.
Definition
Type parameters
| Type parameter | Constraint | Default type |
|---|---|---|
Value | — | — |
Key | — | — |
Call
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.
| Type parameter | Constraint | Default type |
|---|---|---|
Value | — | — |
Key | — | — |
| Parameter | Type | Passing | Description |
|---|---|---|---|
values | readonly Value[] | Required | — |
key | (value: Value) => Key | Required | — |
initial | Iterable<Key> | Optional | — |
Returns: Value[].
Examples
- Normalization: ordered value transformation: Collect cleaning, replacement, and stop conditions into one transformation list that actually drives execution.
Start by cleaning one query
Beginner · 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));Continue by adding steps and a stop condition
Configuration · 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));