@tpmjs/tools-normalize-whitespace
Normalize whitespace in text by trimming lines, collapsing multiple spaces, and standardizing line endings. Useful for cleaning up text data, formatting content, or preparing text for processing.
Test @tpmjs/tools-normalize-whitespace (normalizeWhitespaceTool) with AI-powered execution
0/2000 characters
Install this tool and use it with the AI SDK
npm install @tpmjs/tools-normalize-whitespacepnpm add @tpmjs/tools-normalize-whitespaceyarn add @tpmjs/tools-normalize-whitespacebun add @tpmjs/tools-normalize-whitespacedeno add npm:@tpmjs/tools-normalize-whitespaceimport { normalizeWhitespaceTool } from '@tpmjs/tools-normalize-whitespace';import { generateText } from 'ai';
import { openai } from '@ai-sdk/openai';
import { normalizeWhitespaceTool } from '@tpmjs/tools-normalize-whitespace';
const result = await generateText({
model: openai('gpt-4o'),
tools: { normalizeWhitespaceTool },
prompt: 'Your prompt here...',
});
console.log(result.text);Available configuration options
textstringThe text to normalize
optionsobjectNormalization options
Schema extracted: 1/1/2026, 8:18:41 AM
Normalize whitespace in text by trimming lines, collapsing spaces, and standardizing line endings.
npm install @tpmjs/tools-normalize-whitespace
import { normalizeWhitespaceTool } from '@tpmjs/tools-normalize-whitespace'; // Use with AI SDK const result = await normalizeWhitespaceTool.execute({ text: ' Hello World \n This is a test \r\n', options: { trimLines: true, collapseSpaces: true, normalizeLineEndings: true } }); console.log(result.text); // "Hello World\nThis is a test" console.log(result.changes); // { // linesTrimmed: 2, // spacesCollapsed: 5, // lineEndingsNormalized: 1, // originalLength: 44, // normalizedLength: 24 // }
\r\n) to LF (\n)| Parameter | Type | Required | Description |
|---|---|---|---|
| text | string | Yes | The text to normalize |
| options | object | No | Normalization options (see below) |
| Option | Type | Default | Description |
|---|---|---|---|
| trimLines | boolean | true | Trim whitespace from start/end of lines |
| collapseSpaces | boolean | true | Collapse multiple spaces into one |
| normalizeLineEndings | boolean | true | Convert CRLF to LF |
interface NormalizeWhitespaceResult { text: string; // The normalized text changes: { linesTrimmed: number; // Number of lines that were trimmed spacesCollapsed: number; // Number of spaces removed by collapsing lineEndingsNormalized: number; // Number of CRLF converted to LF originalLength: number; // Character count before normalization normalizedLength: number; // Character count after normalization }; }
const result = await normalizeWhitespaceTool.execute({ text: ' Hello \n World ', options: { trimLines: true, collapseSpaces: false, normalizeLineEndings: false } }); console.log(result.text); // "Hello\nWorld"
const result = await normalizeWhitespaceTool.execute({ text: 'Hello World Test', options: { trimLines: false, collapseSpaces: true, normalizeLineEndings: false } }); console.log(result.text); // "Hello World Test"
const result = await normalizeWhitespaceTool.execute({ text: 'Line 1\r\nLine 2\r\nLine 3', options: { trimLines: false, collapseSpaces: false, normalizeLineEndings: true } }); console.log(result.text); // "Line 1\nLine 2\nLine 3"
const result = await normalizeWhitespaceTool.execute({ text: ' Hello World \r\n Line 2 ' }); console.log(result.text); // "Hello World\nLine 2" console.log(result.changes); // { // linesTrimmed: 2, // spacesCollapsed: 5, // lineEndingsNormalized: 1, // originalLength: 35, // normalizedLength: 18 // }
MIT
Downloads/month
0
Quality Score