0f84399e6e3a169fcf4f4b00c11c747fea1383a9056a6b140ea6024f7a5109027aa397de2fe512474bfef1d45859fc63dee86be74acf546bbf7a22c3d81a7a 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. const NUMBER_CHAR_RE = /\d/;
  2. const STR_SPLITTERS = ["-", "_", "/", "."];
  3. function isUppercase(char = "") {
  4. if (NUMBER_CHAR_RE.test(char)) {
  5. return void 0;
  6. }
  7. return char !== char.toLowerCase();
  8. }
  9. function splitByCase(str, separators) {
  10. const splitters = separators ?? STR_SPLITTERS;
  11. const parts = [];
  12. if (!str || typeof str !== "string") {
  13. return parts;
  14. }
  15. let buff = "";
  16. let previousUpper;
  17. let previousSplitter;
  18. for (const char of str) {
  19. const isSplitter = splitters.includes(char);
  20. if (isSplitter === true) {
  21. parts.push(buff);
  22. buff = "";
  23. previousUpper = void 0;
  24. continue;
  25. }
  26. const isUpper = isUppercase(char);
  27. if (previousSplitter === false) {
  28. if (previousUpper === false && isUpper === true) {
  29. parts.push(buff);
  30. buff = char;
  31. previousUpper = isUpper;
  32. continue;
  33. }
  34. if (previousUpper === true && isUpper === false && buff.length > 1) {
  35. const lastChar = buff.at(-1);
  36. parts.push(buff.slice(0, Math.max(0, buff.length - 1)));
  37. buff = lastChar + char;
  38. previousUpper = isUpper;
  39. continue;
  40. }
  41. }
  42. buff += char;
  43. previousUpper = isUpper;
  44. previousSplitter = isSplitter;
  45. }
  46. parts.push(buff);
  47. return parts;
  48. }
  49. function upperFirst(str) {
  50. return str ? str[0].toUpperCase() + str.slice(1) : "";
  51. }
  52. function lowerFirst(str) {
  53. return str ? str[0].toLowerCase() + str.slice(1) : "";
  54. }
  55. function pascalCase(str, opts) {
  56. return str ? (Array.isArray(str) ? str : splitByCase(str)).map((p) => upperFirst(opts?.normalize ? p.toLowerCase() : p)).join("") : "";
  57. }
  58. function camelCase(str, opts) {
  59. return lowerFirst(pascalCase(str || "", opts));
  60. }
  61. function kebabCase(str, joiner) {
  62. return str ? (Array.isArray(str) ? str : splitByCase(str)).map((p) => p.toLowerCase()).join(joiner ?? "-") : "";
  63. }
  64. function snakeCase(str) {
  65. return kebabCase(str || "", "_");
  66. }
  67. function flatCase(str) {
  68. return kebabCase(str || "", "");
  69. }
  70. function trainCase(str, opts) {
  71. return (Array.isArray(str) ? str : splitByCase(str)).filter(Boolean).map((p) => upperFirst(opts?.normalize ? p.toLowerCase() : p)).join("-");
  72. }
  73. const titleCaseExceptions = /^(a|an|and|as|at|but|by|for|if|in|is|nor|of|on|or|the|to|with)$/i;
  74. function titleCase(str, opts) {
  75. return (Array.isArray(str) ? str : splitByCase(str)).filter(Boolean).map(
  76. (p) => titleCaseExceptions.test(p) ? p.toLowerCase() : upperFirst(opts?.normalize ? p.toLowerCase() : p)
  77. ).join(" ");
  78. }
  79. export { camelCase, flatCase, isUppercase, kebabCase, lowerFirst, pascalCase, snakeCase, splitByCase, titleCase, trainCase, upperFirst };