github.com/replicatedhq/ship@v0.55.0/web/init/src/utilities/utilities.js (about) 1 export const Utilities = { 2 3 isFailedToFetchErr(message) { 4 if (message === "Failed to fetch" || message === "Could not connect to the server." || message === "NetworkError when attempting to fetch resource.") { 5 return true; 6 } 7 return false; 8 }, 9 10 calcPercent(num1, num2, rounding) { 11 const percentage = (num1 / num2) * 100; 12 if (isFinite(percentage)) { 13 return percentage.toFixed(rounding); 14 } else { 15 return 0; 16 } 17 }, 18 19 // Converts string to titlecase i.e. 'hello' -> 'Hello' 20 // @returns {String} 21 toTitleCase(word) { 22 let i, j, str, lowers, uppers; 23 const _word = typeof word === "string" ? word : this; 24 str = _word.replace(/([^\W_]+[^\s-]*) */g, (txt) => { 25 return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase(); 26 }); 27 28 // Certain minor words should be left lowercase unless 29 // they are the first or last words in the string 30 lowers = ["A", "An", "The", "And", "But", "Or", "For", "Nor", "As", "At", 31 "By", "For", "From", "In", "Into", "Near", "Of", "On", "Onto", "To", "With"]; 32 for (i = 0, j = lowers.length; i < j; i++) { 33 str = str.replace(new RegExp("\\s" + lowers[i] + "\\s", "g"), (txt) => { 34 return txt.toLowerCase(); 35 }); 36 } 37 38 // Certain words such as initialisms or acronyms should be left uppercase 39 uppers = ["Id", "Tv"]; 40 for (i = 0, j = uppers.length; i < j; i++) { 41 str = str.replace(new RegExp("\\b" + uppers[i] + "\\b", "g"), uppers[i].toUpperCase()); 42 } 43 44 return str; 45 }, 46 };