All files / src/helpers classTransforms.ts

100% Statements 25/25
100% Branches 9/9
100% Functions 4/4
100% Lines 23/23
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 29 30 31 32 33 34 35 36 37 38 39 40 41 422x         2x 8x   7x 31x   31x   4x 4x 4x 3x   4x     4x 4x 4x 2x   4x     4x 4x   4x 4x   15x 15x     31x    
import { camelCase } from 'lodash';
 
// The below is based on the CSS Modules implementation found here:
// https://github.com/webpack-contrib/css-loader/blob/master/lib/compile-exports.js
 
const dashesCamelCase = (className: string) =>
  className.replace(/-+(\w)/g, (match, firstLetter) => firstLetter.toUpperCase());
 
export const transformClasses = (camelCaseOption?: boolean | string) => (className: string) => {
  const entries: string[] = [];
 
  switch (camelCaseOption) {
    case true: {
      entries.push(className);
      const targetClassName = camelCase(className);
      if (targetClassName !== className) {
        entries.push(targetClassName);
      }
      break;
    }
    case 'dashes': {
      entries.push(className);
      const targetClassName = dashesCamelCase(className);
      if (targetClassName !== className) {
        entries.push(targetClassName);
      }
      break;
    }
    case 'only':
      entries.push(camelCase(className));
      break;
    case 'dashesOnly':
      entries.push(dashesCamelCase(className));
      break;
    default:
      entries.push(className);
      break;
  }
 
  return entries;
};