export function toCase()

in fuse-ui-shared/stringCases.ts [83:123]


export function toCase(targetCase: StringCases, text): string {
  const source = detectCase(text);
  let words: string[] = [];

  if (source === targetCase) {
    return text;
  }

  switch (source) {
    case StringCases.kebabCase:
      words = text.split('-');
      break;
    case StringCases.snakeCase:
      words = text.split('_');
      break;
    case StringCases.camelCase:
    case StringCases.pascalCase:
      words = asArray(breakWords(text));
      break;
    default:
      words = [text];
  }

  switch (targetCase) {
    case StringCases.lowerCase:
      return words.map(x => x.toLowerCase()).join('');
    case StringCases.upperCase:
      return words.map(x => x.toUpperCase()).join('');
    case StringCases.camelCase:
      return words.map((x, i) => i === 0 ? x.toLowerCase() : capitalize(x)).join('');
    case StringCases.pascalCase:
      return words.map((x, i) => i === 0 ? capitalize(x) : capitalize(x)).join('');
    case StringCases.kebabCase:
      return words.map(x => x.toLowerCase()).join('-');
    case StringCases.snakeCase:
      return words.map(x => x.toLowerCase()).join('_');
    default:
  }

  return text;
}