index.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. function isUppercase(char = "") {
  4. return char.toUpperCase() === char;
  5. }
  6. const STR_SPLITTERS = ["-", "_", "/", "."];
  7. function splitByCase(str, splitters = STR_SPLITTERS) {
  8. const parts = [];
  9. let buff = "";
  10. let previusUpper = isUppercase(str[0]);
  11. let previousSplitter = splitters.includes(str[0]);
  12. for (const char of str.split("")) {
  13. const isSplitter = splitters.includes(char);
  14. if (isSplitter) {
  15. parts.push(buff);
  16. buff = "";
  17. previusUpper = false;
  18. previousSplitter = true;
  19. } else if (!previousSplitter && !previusUpper && isUppercase(char)) {
  20. parts.push(buff);
  21. buff = char;
  22. previusUpper = true;
  23. previousSplitter = false;
  24. } else {
  25. buff += char;
  26. previusUpper = isUppercase(char);
  27. previousSplitter = isSplitter;
  28. }
  29. }
  30. if (buff) {
  31. parts.push(buff);
  32. }
  33. return parts;
  34. }
  35. function upperFirst(str) {
  36. if (!str) {
  37. return "";
  38. }
  39. return str[0].toUpperCase() + str.substr(1);
  40. }
  41. function lowerFirst(str) {
  42. if (!str) {
  43. return "";
  44. }
  45. return str[0].toLocaleLowerCase() + str.substr(1);
  46. }
  47. function pascalCase(str = "") {
  48. return (Array.isArray(str) ? str : splitByCase(str)).map((p) => upperFirst(p)).join("");
  49. }
  50. function camelCase(str = "") {
  51. return lowerFirst(pascalCase(str));
  52. }
  53. function kebabCase(str = "", joiner = "-") {
  54. return (Array.isArray(str) ? str : splitByCase(str)).map((p = "") => p.toLocaleLowerCase()).join(joiner);
  55. }
  56. function snakeCase(str = "") {
  57. return kebabCase(str, "_");
  58. }
  59. exports.camelCase = camelCase;
  60. exports.isUppercase = isUppercase;
  61. exports.kebabCase = kebabCase;
  62. exports.lowerFirst = lowerFirst;
  63. exports.pascalCase = pascalCase;
  64. exports.snakeCase = snakeCase;
  65. exports.splitByCase = splitByCase;
  66. exports.upperFirst = upperFirst;