defineNormalization
KIT · function · Runtime 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".
Import
import { defineNormalization } from '@jangts/agent-core/kits/normalization';import { defineNormalization } 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 | — | — |
Context | — | void |
Call
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. |
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));