Guest User

Untitled

a guest
Oct 15th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. ////////////////////////////////////////////////////////////////////////////////
  2. // Copyright(C) 2018 David Hamiter
  3. ////////////////////////////////////////////////////////////////////////////////
  4. 'use strict';
  5.  
  6. /**
  7. * @author David Hamiter
  8. * Static class for grammar checking
  9. */
  10. abstract class GrammarUtil {
  11. //----------------------------------------------------------------------
  12. //
  13. // Properties
  14. //
  15. //----------------------------------------------------------------------
  16.  
  17. static readonly NUM_SUFFIX: string[] = ["th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"];
  18.  
  19. private static readonly VOWEL_ARRAY: string[] = ["a", "e", "i", "o", "u", "8"];
  20. private static readonly PUNCTUATION_ARRAY: string[] = [".", "!", "?"];
  21.  
  22. //----------------------------------------------------------------------
  23. //
  24. // Constructor
  25. //
  26. //----------------------------------------------------------------------
  27.  
  28. private constructor() { } // Static class cannot be instantiated
  29.  
  30. //----------------------------------------------------------------------
  31. //
  32. // Methods
  33. //
  34. //----------------------------------------------------------------------
  35.  
  36. // Makes 1st letter of a string uppercase
  37. static upCase1st(string: string): string {
  38. return string.substr(0, 1).toUpperCase() + string.substr(1);
  39. }
  40.  
  41. // Makes 1st letter of each sentence of a string uppercase
  42. static upCaseSentences(str: string) {
  43. return str.replace(/.+?[\.\?\!](\s|$)/g, function (txt) {
  44. return txt.charAt(0).toUpperCase() + txt.substr(1);
  45. });
  46. }
  47.  
  48. // Return string number and suffix for a given num. Ex: 42 becomes '42nd'
  49. static suffixNum(num: number): string {
  50. return num + _NUM_SUFFIXES[num % 10];
  51. }
  52.  
  53. // Puts "A" or "An" before string based on its first letter. //
  54. static AorAn(txt: string, UpCase?: boolean): string {
  55. var a: string = (GrammarUtil.VOWEL_ARRAY.indexOf(txt.toLowerCase().charAt(0)) > -1) ? "an " : "a ";
  56. if (UpCase) a = GrammarUtil.upCase1st(a);
  57. return a + txt;
  58. }
  59.  
  60. // Adds punctuation at end of string, if needed. //
  61. static puncCheck(txt: string, UpCase?: boolean): string {
  62. txt = GrammarUtil.stripWhite(txt);
  63. if (txt.length) {
  64. if (GrammarUtil.PUNCTUATION_ARRAY.indexOf(txt.charAt(txt.length - 1)) === -1) {
  65. txt += ".";
  66. }
  67. }
  68. if (UpCase) txt = GrammarUtil.upCaseSentences(txt);
  69. return txt;
  70. }
  71.  
  72. // Strips extra whitespace and optionally converts newlines and returns to spaces
  73. static stripWhite(txt: string, stripReturns?: boolean, stripNewlines?: boolean, newLineAndReturnToHTML?: boolean): string {
  74. const replaceWith: string = newLineAndReturnToHTML ? "<br>" : " ";
  75. // convert returns to spaces
  76. if (stripReturns) {
  77. txt = txt.replace(/\r/g, replaceWith);
  78. }
  79. // convert newlines to spaces
  80. if (stripNewlines) {
  81. txt = txt.replace(/\n/g, replaceWith);
  82. }
  83. // compress spaces
  84. const stringArray: string[] = txt.split(" ");
  85. for (var i = 0; i < stringArray.length; i++) {
  86. if (stringArray[i].length === 0) {
  87. stringArray.splice(i, 1);
  88. }
  89. }
  90. txt = stringArray.join(" ").trim();
  91. return txt;
  92. }
  93.  
  94. }
Add Comment
Please, Sign In to add comment