casing.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // ------------------------------------------------------------------------------
  2. // Helpers
  3. // ------------------------------------------------------------------------------
  4. /**
  5. * Capitalize a string.
  6. * @param {string} str
  7. */
  8. function capitalize(str) {
  9. return str.charAt(0).toUpperCase() + str.slice(1)
  10. }
  11. /**
  12. * Checks whether the given string has symbols.
  13. * @param {string} str
  14. */
  15. function hasSymbols(str) {
  16. return /[!"#%&'()*+,./:;<=>?@[\\\]^`{|}]/u.exec(str) // without " ", "$", "-" and "_"
  17. }
  18. /**
  19. * Checks whether the given string has upper.
  20. * @param {string} str
  21. */
  22. function hasUpper(str) {
  23. return /[A-Z]/u.exec(str)
  24. }
  25. /**
  26. * Convert text to kebab-case
  27. * @param {string} str Text to be converted
  28. * @return {string}
  29. */
  30. function kebabCase(str) {
  31. return str
  32. .replace(/_/gu, '-')
  33. .replace(/\B([A-Z])/gu, '-$1')
  34. .toLowerCase()
  35. }
  36. /**
  37. * Checks whether the given string is kebab-case.
  38. * @param {string} str
  39. */
  40. function isKebabCase(str) {
  41. if (
  42. hasUpper(str) ||
  43. hasSymbols(str) ||
  44. /^-/u.exec(str) || // starts with hyphen is not kebab-case
  45. /_|--|\s/u.exec(str)
  46. ) {
  47. return false
  48. }
  49. return true
  50. }
  51. /**
  52. * Convert text to snake_case
  53. * @param {string} str Text to be converted
  54. * @return {string}
  55. */
  56. function snakeCase(str) {
  57. return str
  58. .replace(/\B([A-Z])/gu, '_$1')
  59. .replace(/-/gu, '_')
  60. .toLowerCase()
  61. }
  62. /**
  63. * Checks whether the given string is snake_case.
  64. * @param {string} str
  65. */
  66. function isSnakeCase(str) {
  67. if (hasUpper(str) || hasSymbols(str) || /-|__|\s/u.exec(str)) {
  68. return false
  69. }
  70. return true
  71. }
  72. /**
  73. * Convert text to camelCase
  74. * @param {string} str Text to be converted
  75. * @return {string} Converted string
  76. */
  77. function camelCase(str) {
  78. if (isPascalCase(str)) {
  79. return str.charAt(0).toLowerCase() + str.slice(1)
  80. }
  81. return str.replace(/[-_](\w)/gu, (_, c) => (c ? c.toUpperCase() : ''))
  82. }
  83. /**
  84. * Checks whether the given string is camelCase.
  85. * @param {string} str
  86. */
  87. function isCamelCase(str) {
  88. if (
  89. hasSymbols(str) ||
  90. /^[A-Z]/u.exec(str) ||
  91. /-|_|\s/u.exec(str) // kebab or snake or space
  92. ) {
  93. return false
  94. }
  95. return true
  96. }
  97. /**
  98. * Convert text to PascalCase
  99. * @param {string} str Text to be converted
  100. * @return {string} Converted string
  101. */
  102. function pascalCase(str) {
  103. return capitalize(camelCase(str))
  104. }
  105. /**
  106. * Checks whether the given string is PascalCase.
  107. * @param {string} str
  108. */
  109. function isPascalCase(str) {
  110. if (
  111. hasSymbols(str) ||
  112. /^[a-z]/u.exec(str) ||
  113. /-|_|\s/u.exec(str) // kebab or snake or space
  114. ) {
  115. return false
  116. }
  117. return true
  118. }
  119. const convertersMap = {
  120. 'kebab-case': kebabCase,
  121. snake_case: snakeCase,
  122. camelCase,
  123. PascalCase: pascalCase
  124. }
  125. const checkersMap = {
  126. 'kebab-case': isKebabCase,
  127. snake_case: isSnakeCase,
  128. camelCase: isCamelCase,
  129. PascalCase: isPascalCase
  130. }
  131. /**
  132. * Return case checker
  133. * @param { 'camelCase' | 'kebab-case' | 'PascalCase' | 'snake_case' } name type of checker to return ('camelCase', 'kebab-case', 'PascalCase')
  134. * @return {isKebabCase|isCamelCase|isPascalCase|isSnakeCase}
  135. */
  136. function getChecker(name) {
  137. return checkersMap[name] || isPascalCase
  138. }
  139. /**
  140. * Return case converter
  141. * @param { 'camelCase' | 'kebab-case' | 'PascalCase' | 'snake_case' } name type of converter to return ('camelCase', 'kebab-case', 'PascalCase')
  142. * @return {kebabCase|camelCase|pascalCase|snakeCase}
  143. */
  144. function getConverter(name) {
  145. return convertersMap[name] || pascalCase
  146. }
  147. module.exports = {
  148. allowedCaseOptions: ['camelCase', 'kebab-case', 'PascalCase'],
  149. /**
  150. * Return case converter
  151. * @param {string} name type of converter to return ('camelCase', 'kebab-case', 'PascalCase')
  152. * @return {kebabCase|camelCase|pascalCase|snakeCase}
  153. */
  154. getConverter,
  155. /**
  156. * Return case checker
  157. * @param {string} name type of checker to return ('camelCase', 'kebab-case', 'PascalCase')
  158. * @return {isKebabCase|isCamelCase|isPascalCase|isSnakeCase}
  159. */
  160. getChecker,
  161. /**
  162. * Return case exact converter.
  163. * If the converted result is not the correct case, the original value is returned.
  164. * @param { 'camelCase' | 'kebab-case' | 'PascalCase' | 'snake_case' } name type of converter to return ('camelCase', 'kebab-case', 'PascalCase')
  165. * @return {kebabCase|camelCase|pascalCase|snakeCase}
  166. */
  167. getExactConverter(name) {
  168. const converter = getConverter(name)
  169. const checker = getChecker(name)
  170. return (str) => {
  171. const result = converter(str)
  172. return checker(result) ? result : str /* cannot convert */
  173. }
  174. },
  175. camelCase,
  176. pascalCase,
  177. kebabCase,
  178. snakeCase,
  179. isCamelCase,
  180. isPascalCase,
  181. isKebabCase,
  182. isSnakeCase,
  183. capitalize
  184. }