All files / src/lib/util snakeCase.ts

100% Statements 18/18
100% Branches 4/4
100% Functions 3/3
100% Lines 15/15

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 2870x 621x 621x 621x 3802x 3802x 107x   3695x     621x     70x     159x   159x 619x   619x     159x    
export const snakeCase = (input: string): string => {
    const result = [];
    const splitString = input.split('');
    for (let i = 0; i < splitString.length; i++) {
        const char = splitString[i];
        if (i !== 0 && char.toLocaleUpperCase() === char) {
            result.push('_', char.toLocaleLowerCase());
        } else {
            result.push(char.toLocaleLowerCase());
        }
    }
    return result.join('');
};
 
export const snakeCaseKeys = (obj: {
    [index: string]: any;
}): { [index: string]: any } => {
    const objResult: { [index: string]: any } = {};
 
    Object.keys(obj).forEach((key) => {
        const snakeCaseKey = snakeCase(key);
 
        objResult[snakeCaseKey] = obj[key];
    });
 
    return objResult;
};