copy-case.mjs

  1. /**
  2. * Copy the case of a string into another.
  3. * @param {string} target String to change the case.
  4. * @param {string} source Source of case pattern.
  5. * @return {string} Converted string.
  6. */
  7. export const copyCase = (word, from) => {
  8. const isLower = w => w.toLowerCase() === w;
  9. const isUpper = w => w.toUpperCase() === w;
  10. if (isLower(from)) return word.toLowerCase();
  11. if (isUpper(from)) return word.toUpperCase();
  12. if (isUpper(from[0]) && isLower(from.slice(1))) {
  13. return word[0].toUpperCase() + word.slice(1).toLowerCase();
  14. }
  15. return word;
  16. };