TextTools API Description

TextTools Library - User Documentation

Overview

TextTools is a text processing library providing over 100 functions for manipulating, transforming, and analyzing text data. All functions are accessible via the `TextTools` object when used in scripts.

Basic Operations

Text Cleaning

// Remove HTML tags
TextTools.stripHtml(html)

// Remove emojis and Unicode Emoji symbols
TextTools.removeEmojis(text)

// Remove diacritical marks
TextTools.removeDiacritics(text)

// Remove empty lines (whitespace/tabs only)
TextTools.removeEmptyLines(text)

// Remove duplicate lines
TextTools.removeDuplicateLines(text)

// Remove only adjacent duplicate lines
TextTools.removeAdjacentDuplicates(text)

// Remove control characters and ANSI escape codes
TextTools.removeControlChars(text)

// Remove extra spaces
TextTools.removeExtraSpaces(text)

// Keep only duplicate lines
TextTools.keepOnlyDuplicates(text)

Working with Quotes

// Replace quotes of various types
// fromType: 'smart' | 'straight' | 'backtick' | 'angle' | 'all'
// toType: 'smart' | 'straight' | 'backtick' | 'angle'
TextTools.replaceQuotes(text, fromType, toType)

// Replace smart quotes with straight quotes
TextTools.replaceSmartQuotes(text)

Text Transformation

// Replace tabs with spaces (default: 2 spaces per tab)
TextTools.tabsToSpaces(text, spacesPerTab = 2)

// Replace spaces with tabs (default: 2 spaces = 1 tab)
TextTools.spacesToTabs(text, tabSize = 2)

// Increment/decrement numbers in text
TextTools.incrementNumbers(text, increment = 1)

// Padding strings
TextTools.padString(text, length, direction = 'start', padChar = ' ')

Splitting and Joining Text

// Split lines by delimiter (string or regex)
TextTools.splitLines(text, delimiter = /\s+/, treatAsRegex = false)

// Join lines
TextTools.joinLines(lines, delimiter = ' ', joinEvery = 2)

Extraction with Regex

// Extract information using regular expressions
TextTools.extractWithRegex(text, pattern, replacement = '$0')

Counting and Statistics

// Count line occurrences
TextTools.countLineOccurrences(text)

// Format occurrences count into readable form
TextTools.formatOccurrences(counts)

// Count words and characters
TextTools.countStats(text)

// Select random lines
TextTools.selectRandomLines(text, count)

Filtering

// Remove or keep only numbers
TextTools.filterNumbers(text, keepNumbers = true)

Data Extraction ()

Contact Information Extraction

// Extract email addresses
// format: 'list' | 'csv' | 'json'
// removeDuplicates: boolean
TextTools.extractEmails(text, format = 'list', removeDuplicates = true)

// Extract phone numbers
TextTools.extractPhoneNumbers(text, format = 'list', removeDuplicates = true)
// Extract URLs (main version)
// normalizeUrls: normalize URLs (add protocol, remove www, etc.)
TextTools.extractUrls(text, format = 'list', removeDuplicates = true, normalizeUrls = true)

// More aggressive URL extraction
TextTools.extractUrlsAdvanced(text, format = 'list', removeDuplicates = true, normalizeUrls = true)

// Extract Markdown links
TextTools.extractMarkdownLinks(text, format = 'list', removeDuplicates = true)

Special Data Extraction

// Extract hashtags
// sortByFrequency: sort by frequency of use
TextTools.extractHashtags(text, format = 'list', removeDuplicates = true, sortByFrequency = false)

// Extract mentions (@username)
TextTools.extractMentions(text, format = 'list', removeDuplicates = true)

// Extract IP addresses
TextTools.extractIPAddresses(text, format = 'list', removeDuplicates = true)

// Extract MAC addresses
TextTools.extractMacAddresses(text, format = 'list', removeDuplicates = true)

// Extract dates
// dateFormat: date format ('YYYY-MM-DD', 'DD.MM.YYYY', etc.)
TextTools.extractDates(text, format = 'list', removeDuplicates = true, dateFormat = 'YYYY-MM-DD')

// Extract numbers
// includeDecimals: include decimal numbers
// includeHex: include hexadecimal numbers
TextTools.extractNumbers(text, format = 'list', removeDuplicates = true, includeDecimals = true, includeHex = true)

Case Operations ()

Case Transformation

// Change text case
// caseType: 'camel' | 'pascal' | 'snake' | 'kebab' | 'constant' | 'dot' | 
//           'title' | 'sentence' | 'sponge' | 'upper' | 'lower'
TextTools.changeTextCase(text, caseType)

// Quick methods for specific case types
TextTools.reverseText(text)          // Reverse text
TextTools.upperCase(text)           // Uppercase
TextTools.lowerCase(text)           // Lowercase
TextTools.titleCase(text)           // Title case
TextTools.sentenceCase(text)        // Sentence case

Helper Functions

TextTools.countWords(text)          // Count words
TextTools.countGraphemes(text)      // Count graphemes
TextTools.truncateText(text, length, ending = '...') // Truncate text
TextTools.wordWrapText(text, width = 80)  // Word wrap
TextTools.stripHtmlTags(text)       // Remove HTML tags
TextTools.escapeHtml(text)          // Escape HTML
TextTools.unescapeHtml(text)        // Unescape HTML
TextTools.trimString(text)          // Trim whitespace

Formatting ()

Number and String Formatting

// Format numbers with separators (commas for thousands)
TextTools.addCommasToNumbers(text)

// Add line numbers
// style: 'number' | 'letter' | 'roman'
TextTools.addLineNumbers(text, style = 'number')

Alignment and Wrapping

// Center-align text
TextTools.centerAlign(text, maxWidth = 80)

// Word wrap at specified width
TextTools.wordWrap(text, width = 80)

Tables and Wrapping

// Format as table
TextTools.formatAsTable(text, delimiter = /\t/)

// Prefix, suffix, and wrap for lines
TextTools.wrapLines(text, prefix = '', suffix = '', wrapPrefix = '', wrapSuffix = '')

πŸ“Š CSV Operations ()

// Convert CSV to Markdown table
TextTools.csvToMarkdownTable(csvText, delimiter = ',')

// Sort CSV by column
TextTools.sortCsvByColumn(csvText, columnIndex = 0, delimiter = ',')

// Filter CSV by column value
TextTools.filterCsvByColumn(csvText, columnIndex, filterValue, delimiter = ',')

Encoding ()

Basic Encoding Operations

// Encode/decode text
// operation: 'url-encode' | 'url-decode' | 'html-encode' | 'html-decode' | 
//            'base64-encode' | 'base64-decode'
TextTools.encodeDecodeText(text, operation)

// Escape for JSON
TextTools.escapeJson(text)

// Unescape JSON
TextTools.unescapeJson(text)

JWT Operations

// Decode JWT token
TextTools.decodeJwt(token)

// Validate JWT token (basic structure check)
TextTools.validateJwt(token)

Text Comparison ()

// Compare two texts and show differences
TextTools.compareTexts(text1, text2)

// Calculate similarity percentage between two texts
TextTools.calculateTextSimilarity(text1, text2)

// Find common lines in two texts
TextTools.findCommonLines(text1, text2)

JSON Operations ()

Formatting and Validation

// Format JSON with pretty output
// indentSize: indent size (default 2)
// sortKeys: sort keys alphabetically
TextTools.formatJson(text, indentSize = 2, sortKeys = false)

// Minify JSON
TextTools.minifyJson(text, removeWhitespace = true, removeComments = true)

// Validate JSON
TextTools.validateJson(text)

// Extract JSON from text
TextTools.extractJson(text)

Format Conversion

// Convert JSON to other formats
// format: 'yaml' | 'csv' | 'xml' | 'toml'
TextTools.convertJsonTo(text, format)

// Execute JSON Path query
TextTools.jsonPathQuery(text, query, outputFormat = 'json')

Sorting ()

// Sort lines
// order: 'asc' | 'desc'
// method: 'default' | 'length' | 'numeric' | 'ip' | 'semver' | 'word-count' | 'grapheme'
// caseSensitive: boolean
TextTools.sortLines(text, order = 'asc', method = 'default', caseSensitive = false)

// Shuffle lines
TextTools.shuffleLines(text)

Generators ()

Identifier Generation

// Generate GUID/UUID
// format: 'default' | 'no-dashes' | 'braces' | 'parens'
TextTools.generateGuid(format = 'default')

Number Generation

// Insert number sequence
// format: 'decimal' | 'hex' | 'roman'
TextTools.insertNumberSequence(count, start = 1, step = 1, format = 'decimal', uppercaseHex = true)

// Generate random numbers
// type: 'single' | 'unique'
// format: 'decimal' | 'hex' | 'binary'
TextTools.generateNumbers(min = 1, max = 100, count = 10, type = 'single', format = 'decimal')

Date Operations

// Insert timestamps
// format: 'iso' | 'unix' or custom format
TextTools.insertTimestamp(format = 'iso')

// Format date to custom format
// format: string with tokens (YYYY, MM, DD, HH, mm, ss, etc.)
TextTools.formatDate(date, format)

Number System Conversion

// Convert numbers between systems
// from: 'dec' | 'hex'
// to: 'dec' | 'hex'
TextTools.convertNumberSystem(value, from, to)

Password Generation ()

// Generate secure password
// excludeSimilar: exclude similar characters (I/l/1, O/0, etc.)
TextTools.generatePassword(
  length = 16,
  includeUppercase = true,
  includeLowercase = true,
  includeNumbers = true,
  includeSymbols = true,
  excludeSimilar = false
)

LLM Output Cleaning ()

// Clean text from LLM-specific artifacts
TextTools.cleanLlmOutput(text)

// Extract code from LLM text (highlights code blocks)
TextTools.extractCodeFromLlmOutput(text)

// Remove only LLM artifacts without changing formatting
TextTools.removeLlmArtifacts(text)

Search and Replace ()

// Apply search and replace with advanced options
TextTools.applyFindReplace(text, options)

// Check regex validity
TextTools.isValidRegex(pattern)

HTML to Markdown ()

// Convert HTML to Markdown
TextTools.htmlToMarkdown(html)

// Convert HTML to plain text
TextTools.htmlToText(html)

// Validate and clean HTML
TextTools.validateAndCleanHtml(html)

// Extract plain text (most aggressive mode)
TextTools.extractPlainText(html)

// HTML to Markdown with extended options
TextTools.htmlToMarkdownExtended(html, options)

Helper Functions ()

// Convert to Roman numerals
TextTools.toRoman(num)

// Convert strings to various formats
TextTools.camelCase(str)      // camelCase
TextTools.snakeCase(str)      // snake_case
TextTools.kebabCase(str)      // kebab-case
TextTools.pascalCase(str)     // PascalCase
TextTools.dotCase(str)        // dot.case
TextTools.constantCase(str)   // CONSTANT_CASE
TextTools.startCase(str)      // Start Case
TextTools.slugify(text)       // slug-case (for URLs)
TextTools.spongeCase(str)     // SpOnGe CaSe
TextTools.upperFirst(str)     // First letter uppercase

// Unicode normalization
// form: 'NFC' | 'NFD' | 'NFKC' | 'NFKD'
TextTools.normalizeUnicode(text, form = 'NFC')

Usage Examples

Example 1: Text Cleaning and Formatting

let text = "Example text with HTML tags <b>and numbers 123456</b>";

// Clean HTML
text = TextTools.stripHtml(text);

// Format numbers
text = TextTools.addCommasToNumbers(text);

// Result: "Example text with HTML tags and numbers 123,456"

Example 2: Data Extraction

const content = "Contacts: email@example.com, phone: +7-999-123-45-67, website: https://example.com";

const emails = TextTools.extractEmails(content, 'list', true);
const phones = TextTools.extractPhoneNumbers(content, 'list', true);
const urls = TextTools.extractUrls(content, 'list', true, true);

Example 3: Format Conversion

const jsonText = '{"name": "John", "age": 30}';

// JSON β†’ YAML
const yaml = TextTools.convertJsonTo(jsonText, 'yaml');

// JSON β†’ CSV
const csv = TextTools.convertJsonTo(jsonText, 'csv');

// HTML β†’ Markdown
const markdown = TextTools.htmlToMarkdown('<h1>Title</h1><p>Text</p>');

Example 4: Text Manipulation

const text = "example text for processing";

// Change case
const camel = TextTools.changeTextCase(text, 'camel');    // exampleTextForProcessing
const snake = TextTools.changeTextCase(text, 'snake');    // example_text_for_processing
const title = TextTools.changeTextCase(text, 'title');    // Example Text For Processing

// Count statistics
const stats = TextTools.countStats(text);
// { characters: 27, charactersNoSpaces: 23, words: 4, lines: 1, sentences: 1, paragraphs: 1 }

pipe for Scripts

Basic Usage

`pipe` allows creating compact text processing chains in scripts:

// Script for cleaning user input
const cleanInput = pipe(
  TextTools.stripHtml,
  TextTools.removeEmojis,
  TextTools.removeExtraSpaces,
  TextTools.changeTextCase.bind(null, 'lower')
)(userInput);

Quick Recipes

Data Cleaning

// Clean HTML and normalize text
const sanitize = pipe(
  TextTools.stripHtml,
  TextTools.removeControlChars,
  (t) => TextTools.changeTextCase(t, 'sentence')
);

Information Extraction

// Extract all contacts from text
const extractContacts = pipe(
  (t) => ({
    emails: TextTools.extractEmails(t),
    phones: TextTools.extractPhoneNumbers(t),
    urls: TextTools.extractUrls(t)
  }),
  JSON.stringify,
  (t) => TextTools.formatJson(t, 2)
);

Format Conversion

// CSV β†’ Markdown table
const csvToMd = pipe(
  (csv) => TextTools.sortCsvByColumn(csv, 0),
  TextTools.csvToMarkdownTable
);

// HTML β†’ Markdown
const htmlToMd = pipe(
  TextTools.validateAndCleanHtml,
  TextTools.htmlToMarkdown
);

Useful Script Combinations

// Ready-made chains for common tasks
const scripts = {
  // For social media
  social: pipe(
    TextTools.stripHtmlTags,
    (t) => TextTools.truncateText(t, 280),
    TextTools.extractHashtags
  ),
  
  // For SEO
  seo: pipe(
    TextTools.stripHtml,
    (t) => TextTools.changeTextCase(t, 'lower'),
    TextTools.slugify,
    (t) => TextTools.truncateText(t, 60)
  ),
  
  // For logs
  logClean: pipe(
    TextTools.removeEmptyLines,
    TextTools.removeDuplicateLines,
    (t) => TextTools.sortLines(t, 'asc', 'timestamp')
  ),
  
  // For JSON
  jsonClean: pipe(
    TextTools.extractJson,
    TextTools.formatJson.bind(null, 2, true)
  )
};

Shortcuts for Quick Scripts

// Create aliases for frequent operations
const $ = {
  html: TextTools.stripHtml,
  emoji: TextTools.removeEmojis,
  trim: TextTools.removeExtraSpaces,
  lower: (t) => TextTools.changeTextCase(t, 'lower'),
  upper: (t) => TextTools.changeTextCase(t, 'upper'),
  stats: TextTools.countStats
};

// Compact scripts
const quickScript = pipe(
  $.html,
  $.emoji,
  $.lower,
  $.trim
)(input);

Complete Script Examples

1. Email Newsletter Processing Script

const processEmailContent = pipe(
  TextTools.stripHtml,
  (t) => TextTools.truncateText(t, 500),
  TextTools.addLineNumbers('number'),
  TextTools.wordWrapText.bind(null, 72)
);

2. Text Analysis Script

const analyzeText = pipe(
  (t) => {
    const stats = TextTools.countStats(t);
    const emails = TextTools.extractEmails(t).split('\n').length;
    return `πŸ“Š Analysis: ${stats.words} words, ${emails} emails`;
  }
);

3. Content Migration Script

const migrateContent = pipe(
  TextTools.htmlToMarkdown,
  (md) => md.replace(/<img[^>]+>/g, '![image]'),
  TextTools.removeEmptyLines
);

Scripting Tips

  1. Keep chains short (3-5 operations)
  2. Use `bind()` for parameters
  3. Save frequently used chains
  4. Combine with other functions
// ❌ Too long
const badScript = pipe(/* 10+ operations */);

// βœ… Split into parts
const clean = pipe(TextTools.stripHtml, TextTools.removeEmojis);
const format = pipe(
  (t) => TextTools.changeTextCase(t, 'title'),
  TextTools.wordWrapText.bind(null, 80)
);

const goodScript = (input) => format(clean(input));

`pipe` is ideal for creating clean, readable text processing scripts without unnecessary code.

Usage Tips

  1. Error Handling: Most functions handle errors and return the original text in case of problems.

  2. Performance: For large texts, use simpler operations and avoid complex regular expressions.

  3. Unicode Support: All functions correctly handle Unicode characters, including emojis and Cyrillic.

  4. Execution Context: Functions work in both browser and Node.js environments.