Skip to content
中文

Normalization ​

KIT · constant · Runtime value

Import ​

ts
import { Normalization } from '@jangts/agent-core/kits/normalization';
js
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 ​

MemberPurpose
defineNormalizationSee the overload below for full inputs and outputs.
uniqueBySee the overload below for full inputs and outputs.

Member details ​

defineNormalization ​

ts
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 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.

uniqueBy ​

ts
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 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.