Normalization
KIT · constant · Runtime value
Import
import { Normalization } from '@jangts/agent-core/kits/normalization';import { Normalization } from '@jangts/agent-core/kits/normalization';Other public entry points:@jangts/agent-core/types, @jangts/agent-core/kits/normalization, @jangts/agent-core/kits.
Definition
Member index
| Member | Purpose |
|---|---|
defineNormalization | See the overload below for full inputs and outputs. |
uniqueBy | See the overload below for full inputs and outputs. |
Member details
defineNormalization
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".
| Type parameter | Constraint | Default type |
|---|---|---|
Value | — | — |
Context | — | void |
| Parameter | Type | Passing | Description |
|---|---|---|---|
definitions | readonly NormalizationStep<Value, Context>[] | Required | — |
options | NormalizationOptions<Value, Context> | Optional | — |
Fields of options
| Field | Type | Required | Description |
|---|---|---|---|
isComplete | ((value: Value, context: Context) => boolean) | undefined | Optional | Read-only. Stops the walk after the step that first returns true; later steps then do not run. |
Returns: 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; }>.
Return value structure
| Field | Type | Required | Description |
|---|---|---|---|
steps | readonly Readonly<{ id: string; when: ((value: Value, context: Context) => boolean) | undefined; normalize: (value: Value, context: Context) => Value; }>[] | Required | See the type definition and usage context. |
normalize | (value: Value, context: Context) => Value | Required | See the type definition and usage context. |
uniqueBy
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.
| 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
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));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
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));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));