Skip to content
中文

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 ​

ts
import { defineNormalization } from '@jangts/agent-core/kits/normalization';
js
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 parameterConstraintDefault type
Value——
Context—void

Call ​

ts
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 parameterConstraintDefault type
Value——
Context—void
ParameterTypePassingDescription
definitionsreadonly NormalizationStep<Value, Context>[]Required—
optionsNormalizationOptions<Value, Context>Optional—
Fields of options
FieldTypeRequiredDescription
isComplete((value: Value, context: Context) => boolean) | undefinedOptionalRead-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
FieldTypeRequiredDescription
stepsreadonly Readonly<{ id: string; when: ((value: Value, context: Context) => boolean) | undefined; normalize: (value: Value, context: Context) => Value; }>[]RequiredSee the type definition and usage context.
normalize(value: Value, context: Context) => ValueRequiredSee the type definition and usage context.

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.