acronym.ts 731 B

12345678910111213141516171819202122232425262728
  1. export function parse(phrase: string): string {
  2. const rxUp = /\p{Lu}/u;
  3. if (phrase === "") {
  4. return "";
  5. }
  6. let sub = "";
  7. const words = phrase.split(/[^\w]/)
  8. for (let word of words) {
  9. if (word === "") {
  10. continue;
  11. }
  12. const initial = word.substring(0, 1);
  13. sub += initial.toUpperCase();
  14. let inUpGroup = rxUp.test(initial);
  15. for (let i = 1; i < word.length; i++) {
  16. const c = word.charAt(i);
  17. if (rxUp.test(c)) {
  18. if (inUpGroup) {
  19. continue;
  20. }
  21. sub += c;
  22. continue;
  23. }
  24. inUpGroup = false;
  25. }
  26. }
  27. return sub;
  28. }