String
The String module provides a comprehensive set of utility functions for manipulating and transforming strings. From basic operations like capitalization and character counting to more complex transformations such as converting to different case formats (camelCase, kebab-case, etc.), this module offers essential tools for efficient text processing in your applications.
Key features:
- Case manipulation (uppercase/lowercase)
- Character and word counting
- Removal of accents and special characters
- String masking
- Conversion to different formats (camelCase, kebab-case, PascalCase, snake_case)
- Template processing
- HTML manipulation
- Slug generation for URLs
Overview
capitalize
capitalize(str: string): stringCapitalizes the first letter of the string and makes the rest lowercase.
charCount
charCount(str: string, char: string): numberCounts how many times a specific character appears in a string.
escapeHtml
escapeHtml(str: string): stringEscapes special HTML characters in a string to their corresponding HTML entities.
interpolate
interpolate(str: string, data: Record<PropertyKey, any>, pattern?: RegExp): stringReplaces placeholders in a string with values from a data object.
invertCase
invertCase(str: string): stringInverts the case of each character in the string (uppercase becomes lowercase and vice versa).
isLowerCase
isLowerCase(str: string): booleanReturns true if all alphabetic characters in the string are lowercase.
isUpperCase
isUpperCase(str: string): booleanReturns true if all alphabetic characters in the string are uppercase.
maskString
maskString(str: string, mask: string, maskStart: number, maskLength: number): stringMasks part of the string by replacing characters with a defined character.
removeAccents
removeAccents(str: string): stringRemoves accents and diacritics from characters in the string.
slugify
slugify(str: string): stringConverts the string into a URL-friendly slug, removing special characters and spaces.
stripHTML
stripHTML(html: string): stringRemoves all HTML tags from the string.
template
template(str: string, options?: TemplateOptions): (data: Record<string, any>) => stringCreates a template function for string interpolation with custom options.
toCamelCase
toCamelCase(str: string): stringConverts the string to camelCase format.
toKebabCase
toKebabCase(str: string): stringConverts the string to kebab-case format.
toPascalCase
toPascalCase(str: string): stringConverts the string to PascalCase format.
toSnakeCase
toSnakeCase(str: string): stringConverts the string to snake_case format.
truncate
truncate(text: string, size: number): stringTruncates the string to a maximum length and adds a suffix if necessary.
wordCount
wordCount(str: string): numberCounts the number of words in a string.
