Skip to content
中文

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 parameterConstraintDefault 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 parameterConstraintDefault type
Value——
Key——
ParameterTypePassingDescription
valuesreadonly Value[]Required—
key(value: Value) => KeyRequired—
initialIterable<Key>Optional—

Returns: Value[].

Examples ​

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));

API reference, usage examples and developer guides.