Comprehensive JavaScript utility library with 150+ functions for objects, arrays, dates, strings, numbers, and more
String manipulation utilities including case conversion, padding, trimming, templating, and HTML escaping. Essential for text processing, formatting, and user interface string handling with comprehensive Unicode support.
Convert strings between different naming conventions and case formats.
/**
* Convert string to camelCase
* @param str - String to convert
* @returns camelCase formatted string
*/
function camelCase(str: string): string;
/**
* Convert string to kebab-case (dash-separated)
* @param str - String to convert
* @returns kebab-case formatted string
*/
function kebabCase(str: string): string;Add padding characters to strings for alignment and formatting.
/**
* Pad string at the beginning to reach target length
* @param str - String to pad
* @param targetLength - Desired final length
* @param padString - String to use for padding (default: ' ')
* @returns Padded string
*/
function padStart(str: string, targetLength: number, padString?: string): string;
/**
* Pad string at the end to reach target length
* @param str - String to pad
* @param targetLength - Desired final length
* @param padString - String to use for padding (default: ' ')
* @returns Padded string
*/
function padEnd(str: string, targetLength: number, padString?: string): string;Remove whitespace or custom characters from string edges.
/**
* Remove whitespace or specified characters from both ends
* @param str - String to trim
* @param chars - Characters to remove (default: whitespace)
* @returns Trimmed string
*/
function trim(str: string, chars?: string): string;
/**
* Remove whitespace or specified characters from the beginning
* @param str - String to trim
* @param chars - Characters to remove (default: whitespace)
* @returns Left-trimmed string
*/
function trimLeft(str: string, chars?: string): string;
/**
* Remove whitespace or specified characters from the end
* @param str - String to trim
* @param chars - Characters to remove (default: whitespace)
* @returns Right-trimmed string
*/
function trimRight(str: string, chars?: string): string;Create strings by repeating patterns.
/**
* Repeat string a specified number of times
* @param str - String to repeat
* @param count - Number of times to repeat
* @returns Repeated string
*/
function repeat(str: string, count: number): string;Test string patterns and positions.
/**
* Check if string starts with specified substring
* @param str - String to test
* @param searchString - Substring to search for
* @param position - Position to start search from (default: 0)
* @returns True if string starts with searchString
*/
function startsWith(str: string, searchString: string, position?: number): boolean;
/**
* Check if string ends with specified substring
* @param str - String to test
* @param searchString - Substring to search for
* @param position - Position to end search at (default: string length)
* @returns True if string ends with searchString
*/
function endsWith(str: string, searchString: string, position?: number): boolean;Escape and unescape HTML entities for safe web display.
/**
* Escape HTML entities in string
* @param str - String to escape
* @returns String with HTML entities escaped
*/
function escape(str: string): string;
/**
* Unescape HTML entities in string
* @param str - String to unescape
* @returns String with HTML entities unescaped
*/
function unescape(str: string): string;Template string processing with variable substitution and formatting.
/**
* Process template string with variable substitution
* @param str - Template string with placeholders
* @param obj - Object with values for substitution
* @param options - Template processing options
* @returns Processed string with substituted values
*/
function template(str: string, obj: any, options?: TemplateOptions): string;
interface TemplateOptions {
/** Regular expression for template matching */
tmplRE?: RegExp;
/** Whether to escape HTML entities (default: false) */
escape?: boolean;
}
/**
* Format string with placeholder substitution
* @param str - Format string with numbered placeholders
* @param args - Values to substitute into placeholders
* @returns Formatted string
*/
function toFormatString(str: string, ...args: any[]): string;Convert values to string representations.
/**
* Convert value to string with default fallback
* @param obj - Value to convert
* @param defaultValue - Default value if conversion fails
* @returns String representation or default value
*/
function toValueString(obj: any, defaultValue?: string): string;
/**
* Alias for toValueString
* @param obj - Value to convert
* @param defaultValue - Default value if conversion fails
* @returns String representation or default value
*/
function toString(obj: any, defaultValue?: string): string;Usage Examples:
import {
camelCase, kebabCase, padStart, padEnd, trim, repeat,
startsWith, endsWith, escape, unescape, template, toFormatString
} from 'xe-utils';
// Case conversion
console.log(camelCase('hello-world-example')); // 'helloWorldExample'
console.log(camelCase('user_first_name')); // 'userFirstName'
console.log(kebabCase('helloWorldExample')); // 'hello-world-example'
console.log(kebabCase('UserFirstName')); // 'user-first-name'
// String padding
console.log(padStart('123', 6, '0')); // '000123'
console.log(padEnd('abc', 8, '.')); // 'abc.....'
console.log(padStart('5', 3)); // ' 5' (space padding)
// Number formatting with padding
const formatNumber = (num) => padStart(String(num), 4, '0');
console.log(formatNumber(42)); // '0042'
// String trimming
console.log(trim(' hello world ')); // 'hello world'
console.log(trim('###hello###', '#')); // 'hello'
console.log(trimLeft(' hello world ')); // 'hello world '
console.log(trimRight(' hello world ')); // ' hello world'
// String repetition
console.log(repeat('*', 5)); // '*****'
console.log(repeat('abc', 3)); // 'abcabcabc'
// Create dividers and patterns
const divider = repeat('-', 40);
const pattern = repeat('AB', 10); // 'ABABABABABABABABAB'
// String testing
const filename = 'document.pdf';
console.log(startsWith(filename, 'doc')); // true
console.log(endsWith(filename, '.pdf')); // true
const url = 'https://api.example.com/users';
console.log(startsWith(url, 'https://')); // true
console.log(endsWith(url, '/users')); // true
// HTML escaping
const userInput = '<script>alert("xss")</script>';
const safeHtml = escape(userInput); // '<script>alert("xss")</script>'
console.log(unescape(safeHtml)); // Back to original
// Template processing
const greeting = template('Hello, {{name}}! You have {{count}} messages.', {
name: 'Alice',
count: 5
});
console.log(greeting); // 'Hello, Alice! You have 5 messages.'
// Advanced template with options
const htmlTemplate = template(
'Welcome {{name}}, your balance is {{balance}}',
{ name: '<Alice>', balance: '$100' },
{ escape: true }
);
// Format string with placeholders
const message = toFormatString('User {0} has {1} points and {2} badges',
'Alice', 150, 3
);
console.log(message); // 'User Alice has 150 points and 3 badges'
// Practical examples
function formatCurrency(amount) {
return padStart(String(amount.toFixed(2)), 10);
}
function createProgressBar(current, total, width = 20) {
const filled = Math.round((current / total) * width);
const empty = width - filled;
return '[' + repeat('=', filled) + repeat('-', empty) + ']';
}
console.log(createProgressBar(7, 10)); // '[==============------]'
function formatTableCell(content, width, align = 'left') {
const str = String(content);
if (align === 'right') {
return padStart(str, width);
} else if (align === 'center') {
const padding = width - str.length;
const leftPad = Math.floor(padding / 2);
const rightPad = padding - leftPad;
return repeat(' ', leftPad) + str + repeat(' ', rightPad);
} else {
return padEnd(str, width);
}
}
// Table formatting
const headers = ['Name', 'Age', 'Score'];
const rows = [
['Alice', 25, 95],
['Bob', 30, 87],
['Charlie', 28, 92]
];
console.log(headers.map(h => formatTableCell(h, 10, 'center')).join('|'));
rows.forEach(row => {
console.log(row.map(cell => formatTableCell(cell, 10, 'center')).join('|'));
});Install with Tessl CLI
npx tessl i tessl/npm-xe-utils