It's a type of Planche
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

title-case.ts 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Standalone function
  2. export const toTitleCase = function(str: string): string {
  3. str = str.replace(/([^\W_]+[^\s-]*) */g, function(txt) {
  4. return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
  5. });
  6. // Certain minor words should be left lowercase unless
  7. // they are the first or last words in the string
  8. const lowers = [
  9. 'A',
  10. 'An',
  11. 'And',
  12. 'As',
  13. 'At',
  14. 'But',
  15. 'By',
  16. 'For',
  17. 'From',
  18. 'In',
  19. 'Into',
  20. 'Is',
  21. 'Near',
  22. 'Nor',
  23. 'Of',
  24. 'On',
  25. 'Onto',
  26. 'Or',
  27. 'The',
  28. 'To',
  29. 'With',
  30. ];
  31. const uppers = ['Id', 'Tv'];
  32. for (let i = 0; i < lowers.length; i++)
  33. str = str.replace(new RegExp('\\s' + lowers[i] + '\\s', 'g'), function(txt) {
  34. return txt.toLowerCase();
  35. });
  36. // Certain words such as initialisms or acronyms should be left uppercase
  37. for (let i = 0; i < uppers.length; i++)
  38. str = str.replace(new RegExp('\\b' + uppers[i] + '\\b', 'g'), uppers[i].toUpperCase());
  39. return str;
  40. };
  41. // Prototype onto String so any string gets "".toTitleCase()
  42. String.prototype.toTitleCase = function() {
  43. let str = this.replace(/([^\W_]+[^\s-]*) */g, function(txt) {
  44. return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
  45. });
  46. // Certain minor words should be left lowercase unless
  47. // they are the first or last words in the string
  48. const lowers = [
  49. 'A',
  50. 'An',
  51. 'And',
  52. 'As',
  53. 'At',
  54. 'But',
  55. 'By',
  56. 'For',
  57. 'From',
  58. 'In',
  59. 'Into',
  60. 'Is',
  61. 'Near',
  62. 'Nor',
  63. 'Of',
  64. 'On',
  65. 'Onto',
  66. 'Or',
  67. 'The',
  68. 'To',
  69. 'With',
  70. ];
  71. const uppers = ['Id', 'Tv'];
  72. for (let i = 0; i < lowers.length; i++)
  73. str = str.replace(new RegExp('\\s' + lowers[i] + '\\s', 'g'), function(txt) {
  74. return txt.toLowerCase();
  75. });
  76. // Certain words such as initialisms or acronyms should be left uppercase
  77. for (let i = 0; i < uppers.length; i++)
  78. str = str.replace(new RegExp('\\b' + uppers[i] + '\\b', 'g'), uppers[i].toUpperCase());
  79. return str;
  80. };