All files / lib/util caseUtils.ts

100% Statements 6/6
100% Branches 0/0
100% Functions 4/4
100% Lines 5/5

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28              28x     133x                     28x 12x   12x      
/**
 * Converts a string in snake-case to the equivalent string in camelCase.
 * Inverse of {@link convertCamelCaseToSnakeCase}.
 *
 * @param snakeCase String in snake-case to convert
 * @returns String in camelCase
 */
export function convertSnakeCaseToCamelCase(snakeCase: string): string {
    // convert all substrings of the form "-n" to "N".
    // in other words, remove dashes and capitalize the next letter
    return snakeCase.replace(/-([a-zA-Z0-9])/g, (matchToReplace, letterToCapitalize: string) => letterToCapitalize.toUpperCase());
}
 
/**
 * Converts a string in camelCase to the equivalent string in snake-case.
 * The output string is guaranteed to contain no upper-case letters.
 * Inverse of {@link convertSnakeCaseToCamelCase}.
 *
 * @param camelCase String in camelCase to convert
 * @returns String in snake-case.
 */
export function convertCamelCaseToSnakeCase(camelCase: string): string {
    return camelCase
        // convert all substrings of the form "aB" to "a-B"
        .replace(/([a-z0-9])([A-Z])/g, (matchToReplace, firstLetter, secondLetter) => `${ firstLetter }-${ secondLetter }`)
        // convert the entire string to lower case
        .toLowerCase();
}