A collection of useful TypeScript/JavaScript utilities for crypto, date, string, number, JSON operations and more.
—
String manipulation utilities including random generation, splitting with trimming, HTTP header validation, and character replacement operations optimized for web applications and data processing.
Generate random strings with customizable length and character sets.
/**
* Generate random string with specified length and character set
* @param length - String length (defaults to 16)
* @param charSet - Character set to use (defaults to alphanumeric)
* @returns Random string
*/
function randomString(length?: number, charSet?: string): string;Usage Examples:
import { randomString } from "utility";
// Default 16-character alphanumeric string (A-Z, a-z, 0-9)
const id = randomString();
// Result: "a8B3kN9mP2xR7wQ5"
// Custom length
const token = randomString(32);
// Result: 32-character random string
// Numbers only (verification codes)
const numCode = randomString(6, '0123456789');
// Result: "482759"
// Hexadecimal characters (color codes, IDs)
const hexId = randomString(8, '0123456789ABCDEF');
// Result: "A7B2C8F3"
// Lowercase letters only (readable IDs)
const readableId = randomString(8, 'abcdefghijklmnopqrstuvwxyz');
// Result: "xkjmplvr"
// Uppercase letters only (codes)
const upperCode = randomString(4, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ');
// Result: "QMXP"
// URL-safe characters (tokens for URLs)
const urlSafe = randomString(16, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_');
// Result: "A7b-X9_m2PqR8wK5"
// Custom special set (PIN codes avoiding confusing characters)
const pin = randomString(4, '23456789ABCDEFGHJKLMNPQRSTUVWXYZ');
// Result: "7H4K" (excludes 0, 1, I, O to avoid confusion)
// Binary string
const binary = randomString(8, '01');
// Result: "10110100"
// DNA sequence simulation
const dna = randomString(12, 'ATCG');
// Result: "ATCGATCGGTCA"
// Password-like with special characters
const password = randomString(12, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*');
// Result: "K8m@P2x#R7w!"Split strings with automatic trimming and empty element removal.
/**
* Split string to array with trimming and empty removal
* @param str - Input string (defaults to empty string)
* @param sep - Separator (defaults to comma)
* @returns Array of trimmed, non-empty strings
*/
function split(str?: string, sep?: string): string[];
// Alias for compatibility
const splitAlwaysOptimized = split;Usage Examples:
import { split, splitAlwaysOptimized } from "utility";
// Default comma separation with trimming
const tags = split(" tag1 , tag2, tag3 ");
// Result: ["tag1", "tag2", "tag3"]
// Custom separator
const paths = split("/path/to/file", "/");
// Result: ["path", "to", "file"]
// Handles empty elements
const messy = split("a,,b, ,c", ",");
// Result: ["a", "b", "c"]
// Using alias
const cleaned = splitAlwaysOptimized(" item1 ; item2 ; ", ";");
// Result: ["item1", "item2"]Advanced string replacement with function support.
/**
* Replace string with support for function replacers
* @param str - Input string
* @param substr - String or RegExp to find
* @param newSubstr - Replacement string or function
* @returns Modified string
*/
function replace(str: string, substr: string | RegExp, newSubstr: string | StringReplacer): string;
type StringReplacer = (substring: string, ...args: any[]) => string;Usage Examples:
import { replace } from "utility";
// Simple string replacement
const basic = replace("hello world", "world", "universe");
// Result: "hello universe"
// Function-based replacement
const dynamic = replace("hello WORLD", /[A-Z]+/g, (match) => match.toLowerCase());
// Result: "hello world"
// Complex replacement with capture groups
const formatted = replace("2025-09-06", /(\d{4})-(\d{2})-(\d{2})/, (match, year, month, day) => {
return `${day}/${month}/${year}`;
});
// Result: "06/09/2025"Validate and clean HTTP header values according to RFC specifications.
/**
* Replace invalid HTTP header characters with replacement
* @param val - Input header value
* @param replacement - Replacement string or function
* @returns Object with cleaned value and validity flag
*/
function replaceInvalidHttpHeaderChar(val: string, replacement?: string | Replacement): {val: string, invalid: boolean};
/**
* Detect invalid HTTP header characters in a string
* @param val - Input string to validate
* @returns True if string contains invalid characters
*/
function includesInvalidHttpHeaderChar(val: string): boolean;
type Replacement = (char: string) => string;Usage Examples:
import { replaceInvalidHttpHeaderChar, includesInvalidHttpHeaderChar } from "utility";
// Check for invalid characters
const hasInvalid = includesInvalidHttpHeaderChar("Valid-Header-Value");
// Result: false
const hasInvalidChars = includesInvalidHttpHeaderChar("Invalid\nHeader");
// Result: true
// Clean invalid characters
const cleaned = replaceInvalidHttpHeaderChar("Header\nWith\rNewlines");
// Result: { val: "Header With Newlines", invalid: true }
// Custom replacement
const customClean = replaceInvalidHttpHeaderChar("Bad\x00Chars", "_");
// Result: { val: "Bad_Chars", invalid: true }
// Function-based replacement
const funcClean = replaceInvalidHttpHeaderChar("Test\nValue", (char) => {
return char === '\n' ? ' ' : '_';
});
// Result: { val: "Test Value", invalid: true }
// No invalid characters
const alreadyClean = replaceInvalidHttpHeaderChar("Clean-Header");
// Result: { val: "Clean-Header", invalid: false }Install with Tessl CLI
npx tessl i tessl/npm-utility