Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ////////////////////////////////////////////////////////////////////////////////
- // Copyright(C) 2018 David Hamiter
- ////////////////////////////////////////////////////////////////////////////////
- 'use strict';
- /**
- * @author David Hamiter
- * Static class for grammar checking
- */
- abstract class GrammarUtil {
- //----------------------------------------------------------------------
- //
- // Properties
- //
- //----------------------------------------------------------------------
- static readonly NUM_SUFFIX: string[] = ["th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"];
- private static readonly VOWEL_ARRAY: string[] = ["a", "e", "i", "o", "u", "8"];
- private static readonly PUNCTUATION_ARRAY: string[] = [".", "!", "?"];
- //----------------------------------------------------------------------
- //
- // Constructor
- //
- //----------------------------------------------------------------------
- private constructor() { } // Static class cannot be instantiated
- //----------------------------------------------------------------------
- //
- // Methods
- //
- //----------------------------------------------------------------------
- // Makes 1st letter of a string uppercase
- static upCase1st(string: string): string {
- return string.substr(0, 1).toUpperCase() + string.substr(1);
- }
- // Makes 1st letter of each sentence of a string uppercase
- static upCaseSentences(str: string) {
- return str.replace(/.+?[\.\?\!](\s|$)/g, function (txt) {
- return txt.charAt(0).toUpperCase() + txt.substr(1);
- });
- }
- // Return string number and suffix for a given num. Ex: 42 becomes '42nd'
- static suffixNum(num: number): string {
- return num + _NUM_SUFFIXES[num % 10];
- }
- // Puts "A" or "An" before string based on its first letter. //
- static AorAn(txt: string, UpCase?: boolean): string {
- var a: string = (GrammarUtil.VOWEL_ARRAY.indexOf(txt.toLowerCase().charAt(0)) > -1) ? "an " : "a ";
- if (UpCase) a = GrammarUtil.upCase1st(a);
- return a + txt;
- }
- // Adds punctuation at end of string, if needed. //
- static puncCheck(txt: string, UpCase?: boolean): string {
- txt = GrammarUtil.stripWhite(txt);
- if (txt.length) {
- if (GrammarUtil.PUNCTUATION_ARRAY.indexOf(txt.charAt(txt.length - 1)) === -1) {
- txt += ".";
- }
- }
- if (UpCase) txt = GrammarUtil.upCaseSentences(txt);
- return txt;
- }
- // Strips extra whitespace and optionally converts newlines and returns to spaces
- static stripWhite(txt: string, stripReturns?: boolean, stripNewlines?: boolean, newLineAndReturnToHTML?: boolean): string {
- const replaceWith: string = newLineAndReturnToHTML ? "<br>" : " ";
- // convert returns to spaces
- if (stripReturns) {
- txt = txt.replace(/\r/g, replaceWith);
- }
- // convert newlines to spaces
- if (stripNewlines) {
- txt = txt.replace(/\n/g, replaceWith);
- }
- // compress spaces
- const stringArray: string[] = txt.split(" ");
- for (var i = 0; i < stringArray.length; i++) {
- if (stringArray[i].length === 0) {
- stringArray.splice(i, 1);
- }
- }
- txt = stringArray.join(" ").trim();
- return txt;
- }
- }
Add Comment
Please, Sign In to add comment