Skip to content
中文

Normalization: ordered value transformation ​

Collect cleaning, replacement, and stop conditions into one transformation list that actually drives execution.

The input is an ordered list of NormalizationStep entries; normalize(value, context) returns the last value. Each step may return a new value or modify in place; object ownership is decided by the App. when and isComplete must synchronously return boolean, and normalize must be synchronous. Duplicate or empty step IDs, missing handlers, and accidentally returning a Promise are all rejected.

uniqueBy deduplicates stably by the key the App provides and keeps the first occurrence's original value; initial can exclude keys already seen. It does not guess whether two business objects are equivalent.

Progressive examples for the same business case ​

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

Policies & implementations ​

API reference, usage examples and developer guides.