nhb-toolbox
Version:
A versatile collection of smart, efficient, and reusable utility functions and classes for everyday development needs.
76 lines • 3.26 kB
TypeScript
import type { CaseFormat, StringCaseOptions } from './types';
/**
* * Converts a string to a specified case format with advanced handling for word boundaries, punctuation, acronyms, and Unicode characters.
*
* @remarks
* - This function is Unicode-aware, treats non-alphanumeric characters (spaces, underscores, dots, slashes, etc.) as word boundaries, and optionally preserves internal acronyms.
* - `Title Case` formatting respects small words such as prepositions, articles, conjunctions, and auxiliary verbs (defined in `LOWERCASE`) — these are not capitalized unless they appear at the start or end of the string.
* - Leading and trailing punctuation (non-letter/number characters) is preserved in the result.
*
* @param value - The input string to convert. Can contain letters, numbers, punctuation,
* spaces, underscores, dashes, etc.
* @param format - The target case format:
* - `'camelCase'` → camelCase (e.g., `myVariableName`)
* - `'PascalCase'` → PascalCase (e.g., `MyVariableName`)
* - `'snake_case'` → snake_case (e.g., `my_variable_name`)
* - `'kebab-case'` → kebab-case (e.g., `my-variable-name`)
* - `'Title Case'` → Title Case (e.g., `My Variable Name`)
* - `'lowercase'` → all lowercase [ It is recommended to use built-in string method `string.toLowerCase()` ]
* - `'UPPERCASE'` → all uppercase [ It is recommended to use built-in string method `string.toUpperCase()` ]
* @param options - Optional configuration options for more control.
*
* @returns The converted string, with leading/trailing punctuation preserved.
*
* @example
* // Basic usage
* convertStringCase('my-example_string', 'camelCase');
* // Returns: 'myExampleString'
*
* convertStringCase('my-example_string', 'snake_case');
* // Returns: 'my_example_string'
*
* convertStringCase('my-example_string', 'kebab-case');
* // Returns: 'my-example-string'
*
* convertStringCase('my example string', 'Title Case');
* // Returns: 'My Example String'
*
* convertStringCase('My example String', 'lowercase');
* // Returns: 'my example string'
*
* convertStringCase('my example string', 'UPPERCASE');
* // Returns: 'MY EXAMPLE STRING'
*
* @example
* // Preserve acronyms
* convertStringCase('get API response', 'camelCase', { preserveAcronyms: true });
* // Returns: 'getAPIResponse'
*
* convertStringCase('get API response', 'PascalCase', { preserveAcronyms: true });
* // Returns: 'GetAPIResponse'
*
* convertStringCase('the API of things', 'Title Case', { preserveAcronyms: true });
* // Returns: 'The API of Things'
*
* @example
* // Leading/trailing punctuation is preserved
* convertStringCase('++hello_world++', 'PascalCase');
* // Returns: '++HelloWorld++'
*
* @example
* // Dashes are preserved in Title Case
* convertStringCase('xml-http_request', 'Title Case');
* // Returns: 'Xml-http Request'
*
* @example
* // Empty string returns empty
* convertStringCase('', 'camelCase');
* // Returns: ''
*
* @example
* // Single token is capitalized properly
* convertStringCase('api', 'PascalCase');
* // Returns: 'Api'
*/
export declare function convertStringCase(value: string, format: CaseFormat, options?: StringCaseOptions): string;
//# sourceMappingURL=case.d.ts.map