Simple email application for Android. Original source code: https://framagit.org/dystopia-project/simple-email
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
1.4 KiB

  1. 'use strict';
  2. function preserveCamelCase(str) {
  3. let isLastCharLower = false;
  4. let isLastCharUpper = false;
  5. let isLastLastCharUpper = false;
  6. for (let i = 0; i < str.length; i++) {
  7. const c = str[i];
  8. if (isLastCharLower && /[a-zA-Z]/.test(c) && c.toUpperCase() === c) {
  9. str = str.substr(0, i) + '-' + str.substr(i);
  10. isLastCharLower = false;
  11. isLastLastCharUpper = isLastCharUpper;
  12. isLastCharUpper = true;
  13. i++;
  14. } else if (isLastCharUpper && isLastLastCharUpper && /[a-zA-Z]/.test(c) && c.toLowerCase() === c) {
  15. str = str.substr(0, i - 1) + '-' + str.substr(i - 1);
  16. isLastLastCharUpper = isLastCharUpper;
  17. isLastCharUpper = false;
  18. isLastCharLower = true;
  19. } else {
  20. isLastCharLower = c.toLowerCase() === c;
  21. isLastLastCharUpper = isLastCharUpper;
  22. isLastCharUpper = c.toUpperCase() === c;
  23. }
  24. }
  25. return str;
  26. }
  27. module.exports = function (str) {
  28. if (arguments.length > 1) {
  29. str = Array.from(arguments)
  30. .map(x => x.trim())
  31. .filter(x => x.length)
  32. .join('-');
  33. } else {
  34. str = str.trim();
  35. }
  36. if (str.length === 0) {
  37. return '';
  38. }
  39. if (str.length === 1) {
  40. return str.toLowerCase();
  41. }
  42. if (/^[a-z0-9]+$/.test(str)) {
  43. return str;
  44. }
  45. const hasUpperCase = str !== str.toLowerCase();
  46. if (hasUpperCase) {
  47. str = preserveCamelCase(str);
  48. }
  49. return str
  50. .replace(/^[_.\- ]+/, '')
  51. .toLowerCase()
  52. .replace(/[_.\- ]+(\w|$)/g, (m, p1) => p1.toUpperCase());
  53. };