UNPKG

@kitiumai/utils-ts

Version:

Comprehensive TypeScript utilities for KitiumAI projects

291 lines 7.07 kB
/** * String utility functions */ /** * Convert string to camelCase * * @param str - The string to convert * @returns The camelCase string * * @example * ```ts * camelCase('hello_world') // 'helloWorld' * camelCase('hello-world') // 'helloWorld' * camelCase('hello world') // 'helloWorld' * ``` */ export declare function camelCase(str: string): string; /** * Convert string to PascalCase * * @param str - The string to convert * @returns The PascalCase string * * @example * ```ts * pascalCase('hello_world') // 'HelloWorld' * pascalCase('hello-world') // 'HelloWorld' * ``` */ export declare function pascalCase(str: string): string; /** * Convert string to kebab-case * * @param str - The string to convert * @returns The kebab-case string * * @example * ```ts * kebabCase('helloWorld') // 'hello-world' * kebabCase('Hello World') // 'hello-world' * ``` */ export declare function kebabCase(str: string): string; /** * Convert string to snake_case * * @param str - The string to convert * @returns The snake_case string * * @example * ```ts * snakeCase('helloWorld') // 'hello_world' * snakeCase('Hello World') // 'hello_world' * ``` */ export declare function snakeCase(str: string): string; /** * Capitalize first letter * * @param str - The string to capitalize * @returns The string with first letter capitalized * * @example * ```ts * capitalize('hello') // 'Hello' * capitalize('HELLO') // 'HELLO' * ``` */ export declare function capitalize(str: string): string; /** * Capitalize first letter of each word * * @param str - The string to convert * @returns The title case string * * @example * ```ts * titleCase('hello world') // 'Hello World' * titleCase('hello WORLD') // 'Hello World' * ``` */ export declare function titleCase(str: string): string; /** * Truncate string to length with suffix * * @param str - The string to truncate * @param length - Maximum length * @param suffix - Suffix to append (default: '...') * @returns The truncated string * * @example * ```ts * truncate('hello world', 8) // 'hello...' * truncate('hello', 10) // 'hello' * ``` */ export declare function truncate(str: string, length: number, suffix?: string): string; /** * Check if string is empty or only whitespace * * @param str - The string to check * @returns True if string is empty or only whitespace * * @example * ```ts * isEmptyString('') // true * isEmptyString(' ') // true * isEmptyString('hello') // false * ``` */ export declare function isEmptyString(str: string): boolean; /** * Pad string to length with character (centered) * * @param str - The string to pad * @param length - Target length * @param char - Character to use for padding (default: ' ') * @returns The padded string * * @example * ```ts * pad('hello', 10) // ' hello ' * pad('hello', 10, '*') // '**hello***' * ``` */ export declare function pad(str: string, length: number, char?: string): string; /** * Pad start of string * * @param str - The string to pad * @param length - Target length * @param char - Character to use for padding (default: ' ') * @returns The padded string * * @example * ```ts * padStart('hello', 10) // ' hello' * padStart('5', 3, '0') // '005' * ``` */ export declare function padStart(str: string, length: number, char?: string): string; /** * Pad end of string * * @param str - The string to pad * @param length - Target length * @param char - Character to use for padding (default: ' ') * @returns The padded string * * @example * ```ts * padEnd('hello', 10) // 'hello ' * padEnd('5', 3, '0') // '500' * ``` */ export declare function padEnd(str: string, length: number, char?: string): string; /** * Remove prefix from string * * @param str - The string * @param prefix - The prefix to remove * @returns The string without the prefix, or original if prefix not found * * @example * ```ts * removePrefix('hello world', 'hello ') // 'world' * removePrefix('hello', 'xyz') // 'hello' * ``` */ export declare function removePrefix(str: string, prefix: string): string; /** * Remove suffix from string * * @param str - The string * @param suffix - The suffix to remove * @returns The string without the suffix, or original if suffix not found * * @example * ```ts * removeSuffix('hello.txt', '.txt') // 'hello' * removeSuffix('hello', '.txt') // 'hello' * ``` */ export declare function removeSuffix(str: string, suffix: string): string; /** * Reverse string * * @param str - The string to reverse * @returns The reversed string * * @example * ```ts * reverse('hello') // 'olleh' * reverse('123') // '321' * ``` */ export declare function reverse(str: string): string; /** * Count occurrences of substring * * @param str - The string to search in * @param search - The substring to count * @returns Number of occurrences * * @example * ```ts * countOccurrences('hello hello', 'hello') // 2 * countOccurrences('aaa', 'aa') // 2 * ``` */ export declare function countOccurrences(str: string, search: string): number; /** * Check if string contains only alphanumeric characters * * @param str - The string to check * @returns True if string contains only letters and numbers * * @example * ```ts * isAlphanumeric('abc123') // true * isAlphanumeric('abc-123') // false * ``` */ export declare function isAlphanumeric(str: string): boolean; /** * Check if string is a valid email * * @param str - The string to check * @returns True if string is a valid email address * * @example * ```ts * isEmail('user@example.com') // true * isEmail('invalid') // false * ``` */ export declare function isEmail(str: string): boolean; /** * Check if string is a valid URL * * @param str - The string to check * @returns True if string is a valid URL * * @example * ```ts * isUrl('https://example.com') // true * isUrl('not a url') // false * ``` */ export declare function isUrl(str: string): boolean; /** * Generate random string * * @param length - Length of the random string * @param chars - Character set to use (default: alphanumeric) * @returns A random string * * @example * ```ts * randomString(10) // 'aB3dEf9GhI' * randomString(5, '0123456789') // '12345' * ``` */ export declare function randomString(length: number, chars?: string): string; /** * Escape HTML special characters * * @param str - The string to escape * @returns The escaped string * * @example * ```ts * escapeHtml('<div>Hello</div>') // '&lt;div&gt;Hello&lt;/div&gt;' * escapeHtml('& "quotes"') // '&amp; &quot;quotes&quot;' * ``` */ export declare function escapeHtml(str: string): string; /** * Unescape HTML entities * * @param str - The string to unescape * @returns The unescaped string * * @example * ```ts * unescapeHtml('&lt;div&gt;Hello&lt;/div&gt;') // '<div>Hello</div>' * unescapeHtml('&amp; &quot;quotes&quot;') // '& "quotes"' * ``` */ export declare function unescapeHtml(str: string): string; //# sourceMappingURL=string.d.ts.map