artytext
Version:
A comprehensive utility library for text manipulation, slug generation, password generation, case conversion, and more. Works with both TypeScript and JavaScript.
403 lines (395 loc) • 13.8 kB
TypeScript
interface PasswordOptions {
length?: number;
includeUppercase?: boolean;
includeLowercase?: boolean;
includeNumbers?: boolean;
includeSymbols?: boolean;
excludeSimilar?: boolean;
excludeAmbiguous?: boolean;
}
interface SlugOptions {
separator?: string;
lowercase?: boolean;
removeSpecialChars?: boolean;
maxLength?: number;
}
interface RandomOptions {
min?: number;
max?: number;
length?: number;
charset?: string;
}
type CaseType = 'lowercase' | 'uppercase' | 'camelCase' | 'kebab-case' | 'snake_case' | 'pascalCase' | 'titleCase' | 'sentenceCase';
interface TextCaseOptions {
type: CaseType;
preserveWords?: boolean;
}
interface TruncateOptions {
length: number;
suffix?: string;
preserveWords?: boolean;
separator?: string;
}
interface MaskOptions {
pattern: string;
placeholder?: string;
reverse?: boolean;
}
interface FormatOptions {
type: 'phone' | 'credit-card' | 'ssn' | 'zip' | 'currency';
format?: string;
locale?: string;
}
/**
* Generate a random password with specified options
* @param options - Password generation options
* @returns Generated password string
*/
declare function generatePassword(options?: PasswordOptions): string;
/**
* Generate a memorable password using words
* @param wordCount - Number of words to use (default: 4)
* @param separator - Separator between words (default: '-')
* @param includeNumbers - Whether to include numbers (default: true)
* @returns Memorable password string
*/
declare function generateMemorablePassword(wordCount?: number, separator?: string, includeNumbers?: boolean): string;
/**
* Check password strength and return a score
* @param password - Password to check
* @returns Object with score (0-100) and feedback
*/
declare function checkPasswordStrength(password: string): {
score: number;
feedback: string[];
strength: 'weak' | 'fair' | 'good' | 'strong' | 'very-strong';
};
/**
* Generate a URL-friendly slug from a string
* @param text - Text to convert to slug
* @param options - Slug generation options
* @returns URL-friendly slug string
*/
declare function generateSlug(text: string, options?: SlugOptions): string;
/**
* Generate a slug with timestamp for unique URLs
* @param text - Text to convert to slug
* @param options - Slug generation options
* @returns Unique slug with timestamp
*/
declare function generateUniqueSlug(text: string, options?: SlugOptions): string;
/**
* Generate a slug with random suffix for unique URLs
* @param text - Text to convert to slug
* @param options - Slug generation options
* @param suffixLength - Length of random suffix (default: 6)
* @returns Unique slug with random suffix
*/
declare function generateRandomSlug(text: string, options?: SlugOptions, suffixLength?: number): string;
/**
* Convert a slug back to readable text
* @param slug - Slug to convert back
* @param separator - Separator used in the slug (default: '-')
* @returns Readable text
*/
declare function slugToText(slug: string, separator?: string): string;
/**
* Check if a string is a valid slug
* @param slug - String to check
* @param separator - Expected separator (default: '-')
* @returns True if valid slug
*/
declare function isValidSlug(slug: string, separator?: string): boolean;
/**
* Generate a random integer between min and max (inclusive)
* @param min - Minimum value (default: 0)
* @param max - Maximum value (default: 100)
* @returns Random integer
*/
declare function randomInt(min?: number, max?: number): number;
/**
* Generate a random float between min and max
* @param min - Minimum value (default: 0)
* @param max - Maximum value (default: 1)
* @param decimals - Number of decimal places (default: 2)
* @returns Random float
*/
declare function randomFloat(min?: number, max?: number, decimals?: number): number;
/**
* Generate a random string with specified options
* @param options - Random string generation options
* @returns Random string
*/
declare function randomString(options?: RandomOptions): string;
/**
* Generate a random number string
* @param length - Length of the number string (default: 6)
* @returns Random number string
*/
declare function randomNumberString(length?: number): string;
/**
* Generate a random hexadecimal string
* @param length - Length of the hex string (default: 8)
* @param uppercase - Whether to use uppercase letters (default: false)
* @returns Random hex string
*/
declare function randomHex(length?: number, uppercase?: boolean): string;
/**
* Generate a random UUID (version 4)
* @returns Random UUID string
*/
declare function randomUUID(): string;
/**
* Generate a random color in hex format
* @param includeHash - Whether to include # prefix (default: true)
* @returns Random hex color
*/
declare function randomColor(includeHash?: boolean): string;
/**
* Generate a random RGB color
* @returns Random RGB color object
*/
declare function randomRGB(): {
r: number;
g: number;
b: number;
};
/**
* Generate a random item from an array
* @param array - Array to pick from
* @returns Random item from array
*/
declare function randomItem<T>(array: T[]): T;
/**
* Generate multiple random items from an array
* @param array - Array to pick from
* @param count - Number of items to pick
* @param allowDuplicates - Whether to allow duplicate picks (default: false)
* @returns Array of random items
*/
declare function randomItems<T>(array: T[], count: number, allowDuplicates?: boolean): T[];
/**
* Generate a random boolean
* @param probability - Probability of true (0-1, default: 0.5)
* @returns Random boolean
*/
declare function randomBoolean(probability?: number): boolean;
/**
* Generate a random date within a range
* @param startDate - Start date (default: 1 year ago)
* @param endDate - End date (default: now)
* @returns Random date
*/
declare function randomDate(startDate?: Date, endDate?: Date): Date;
/**
* Convert text to different case formats
* @param text - Text to convert
* @param options - Case conversion options
* @returns Converted text
*/
declare function changeCase(text: string, options: TextCaseOptions): string;
/**
* Convert to camelCase
* @param text - Text to convert
* @param preserveWords - Whether to preserve word boundaries
* @returns camelCase text
*/
declare function toCamelCase(text: string, preserveWords?: boolean): string;
/**
* Convert to kebab-case
* @param text - Text to convert
* @param preserveWords - Whether to preserve word boundaries
* @returns kebab-case text
*/
declare function toKebabCase(text: string, preserveWords?: boolean): string;
/**
* Convert to snake_case
* @param text - Text to convert
* @param preserveWords - Whether to preserve word boundaries
* @returns snake_case text
*/
declare function toSnakeCase(text: string, preserveWords?: boolean): string;
/**
* Convert to PascalCase
* @param text - Text to convert
* @param preserveWords - Whether to preserve word boundaries
* @returns PascalCase text
*/
declare function toPascalCase(text: string, preserveWords?: boolean): string;
/**
* Convert to Title Case
* @param text - Text to convert
* @param preserveWords - Whether to preserve word boundaries
* @returns Title Case text
*/
declare function toTitleCase(text: string, preserveWords?: boolean): string;
/**
* Convert to Sentence case
* @param text - Text to convert
* @param preserveWords - Whether to preserve word boundaries
* @returns Sentence case text
*/
declare function toSentenceCase(text: string, preserveWords?: boolean): string;
/**
* Convert text to alternating case (aLtErNaTiNg)
* @param text - Text to convert
* @returns Alternating case text
*/
declare function toAlternatingCase(text: string): string;
/**
* Convert text to sponge case (mOcKiNg)
* @param text - Text to convert
* @returns Sponge case text
*/
declare function toSpongeCase(text: string): string;
/**
* Convert text to dot.case
* @param text - Text to convert
* @returns dot.case text
*/
declare function toDotCase(text: string): string;
/**
* Convert text to CONSTANT_CASE
* @param text - Text to convert
* @returns CONSTANT_CASE text
*/
declare function toConstantCase(text: string): string;
/**
* Truncate text to a specified length
* @param text - Text to truncate
* @param options - Truncation options
* @returns Truncated text
*/
declare function truncate(text: string, options: TruncateOptions): string;
/**
* Reverse a string
* @param text - Text to reverse
* @returns Reversed text
*/
declare function reverse(text: string): string;
/**
* Count words in text
* @param text - Text to count words in
* @returns Number of words
*/
declare function countWords(text: string): number;
/**
* Count characters in text (with or without spaces)
* @param text - Text to count characters in
* @param includeSpaces - Whether to include spaces in count (default: true)
* @returns Number of characters
*/
declare function countCharacters(text: string, includeSpaces?: boolean): number;
/**
* Remove duplicate characters from text
* @param text - Text to remove duplicates from
* @param caseSensitive - Whether to consider case (default: false)
* @returns Text with duplicate characters removed
*/
declare function removeDuplicates(text: string, caseSensitive?: boolean): string;
/**
* Remove duplicate words from text
* @param text - Text to remove duplicate words from
* @param caseSensitive - Whether to consider case (default: false)
* @returns Text with duplicate words removed
*/
declare function removeDuplicateWords(text: string, caseSensitive?: boolean): string;
/**
* Mask text according to a pattern
* @param text - Text to mask
* @param options - Masking options
* @returns Masked text
*/
declare function mask(text: string, options: MaskOptions): string;
/**
* Format text according to common patterns
* @param text - Text to format
* @param options - Formatting options
* @returns Formatted text
*/
declare function format(text: string, options: FormatOptions): string;
/**
* Capitalize first letter of each word
* @param text - Text to capitalize
* @returns Capitalized text
*/
declare function capitalize(text: string): string;
/**
* Capitalize first letter of first word only
* @param text - Text to capitalize
* @returns Text with first letter capitalized
*/
declare function capitalizeFirst(text: string): string;
/**
* Remove extra whitespace from text
* @param text - Text to clean
* @returns Cleaned text
*/
declare function removeExtraWhitespace(text: string): string;
/**
* Remove all whitespace from text
* @param text - Text to clean
* @returns Text without whitespace
*/
declare function removeWhitespace(text: string): string;
/**
* Escape HTML special characters
* @param text - Text to escape
* @returns Escaped text
*/
declare function escapeHtml(text: string): string;
/**
* Unescape HTML special characters
* @param text - Text to unescape
* @returns Unescaped text
*/
declare function unescapeHtml(text: string): string;
declare const artytext: {
generatePassword: typeof generatePassword;
generateMemorablePassword: typeof generateMemorablePassword;
checkPasswordStrength: typeof checkPasswordStrength;
generateSlug: typeof generateSlug;
generateUniqueSlug: typeof generateUniqueSlug;
generateRandomSlug: typeof generateRandomSlug;
slugToText: typeof slugToText;
isValidSlug: typeof isValidSlug;
randomInt: typeof randomInt;
randomFloat: typeof randomFloat;
randomString: typeof randomString;
randomNumberString: typeof randomNumberString;
randomHex: typeof randomHex;
randomUUID: typeof randomUUID;
randomColor: typeof randomColor;
randomRGB: typeof randomRGB;
randomItem: typeof randomItem;
randomItems: typeof randomItems;
randomBoolean: typeof randomBoolean;
randomDate: typeof randomDate;
changeCase: typeof changeCase;
toCamelCase: typeof toCamelCase;
toKebabCase: typeof toKebabCase;
toSnakeCase: typeof toSnakeCase;
toPascalCase: typeof toPascalCase;
toTitleCase: typeof toTitleCase;
toSentenceCase: typeof toSentenceCase;
toAlternatingCase: typeof toAlternatingCase;
toSpongeCase: typeof toSpongeCase;
toDotCase: typeof toDotCase;
toConstantCase: typeof toConstantCase;
truncate: typeof truncate;
reverse: typeof reverse;
countWords: typeof countWords;
countCharacters: typeof countCharacters;
removeDuplicates: typeof removeDuplicates;
removeDuplicateWords: typeof removeDuplicateWords;
mask: typeof mask;
format: typeof format;
capitalize: typeof capitalize;
capitalizeFirst: typeof capitalizeFirst;
removeExtraWhitespace: typeof removeExtraWhitespace;
removeWhitespace: typeof removeWhitespace;
escapeHtml: typeof escapeHtml;
unescapeHtml: typeof unescapeHtml;
};
//# sourceMappingURL=index.d.ts.map
export { CaseType, FormatOptions, MaskOptions, PasswordOptions, RandomOptions, SlugOptions, TextCaseOptions, TruncateOptions, capitalize, capitalizeFirst, changeCase, checkPasswordStrength, countCharacters, countWords, artytext as default, escapeHtml, format, generateMemorablePassword, generatePassword, generateRandomSlug, generateSlug, generateUniqueSlug, isValidSlug, mask, randomBoolean, randomColor, randomDate, randomFloat, randomHex, randomInt, randomItem, randomItems, randomNumberString, randomRGB, randomString, randomUUID, removeDuplicateWords, removeDuplicates, removeExtraWhitespace, removeWhitespace, reverse, slugToText, toAlternatingCase, toCamelCase, toConstantCase, toDotCase, toKebabCase, toPascalCase, toSentenceCase, toSnakeCase, toSpongeCase, toTitleCase, truncate, unescapeHtml };