1/** 2 * Utility functions for parsing and manipulating string values. 3 * @module jira/util/strings 4 */ 5define('jira/util/strings', ['exports'], function (exports) { 6 'use strict'; 7 8 var escapeHtml = AJS.escapeHtml; // eslint-disable-line no-undef 9 10 var isString = function isString(value) { 11 return typeof value === 'string' || value instanceof String; 12 }; 13 var isEmpty = function isEmpty(value) { 14 return !isString(value) || value.length === 0; 15 }; 16 17 /** 18 * Returns a boolean if the passed string is "true" or "false", ignoring case, else returns the original string. 19 * @param {String} value 20 * @returns {Boolean|String} 21 * @since 5.0 22 */ 23 exports.asBooleanOrString = function asBooleanOrString(value) { 24 var lc = value ? value.toLowerCase() : ''; 25 var result = value; 26 if (lc === 'true') { 27 result = true; 28 } else if (lc === 'false') { 29 result = false; 30 } 31 return result; 32 }; 33 34 /** 35 * Looks up a search string from the beginning of the given string 36 * @param str the given string 37 * @param searchString search string 38 * @returns {boolean} true if string starts with a search string. 39 */ 40 exports.startsWith = function (str, searchString) { 41 if (isEmpty(str)) { 42 return false; 43 } 44 if (isEmpty(searchString)) { 45 return false; 46 } 47 return str.indexOf(searchString) === 0; 48 }; 49 50 /** 51 * Check whether given string ends with a search string 52 * @param str the given string 53 * @param searchString search string 54 * @returns {boolean} true if the given string ends with a search string 55 */ 56 exports.endsWith = function (str, searchString) { 57 if (isEmpty(str)) { 58 return false; 59 } 60 if (isEmpty(searchString)) { 61 return false; 62 } 63 return str.indexOf(searchString) + searchString.length === str.length; 64 }; 65 66 /** 67 * Looks up a search string in a given string, returns a substring of the given string after a full match. 68 * @param str the given string 69 * @param searchString search string 70 * @returns {string} the substring of the given string after a full match. Returns empty string if the search string was not found. 71 */ 72 exports.substringAfter = function (str, searchString) { 73 if (isEmpty(str)) { 74 return str; 75 } 76 if (isEmpty(searchString)) { 77 return ''; 78 } 79 var indexOf = str.indexOf(searchString); 80 if (indexOf < 0) { 81 return ''; 82 } 83 return str.substring(indexOf + searchString.length); 84 }; 85 86 /** 87 * Looks up a search string in a given string, returns a substring of the given string before a full match. 88 * @param str the given string 89 * @param searchString search string 90 * @returns {string} the substring of the given string before a full match. Returns empty string if the search string was not found. 91 */ 92 exports.substringBefore = function (str, searchString) { 93 if (isEmpty(str)) { 94 return str; 95 } 96 if (isEmpty(searchString)) { 97 return ''; 98 } 99 var indexOf = str.indexOf(searchString); 100 if (indexOf < 0) { 101 return ''; 102 } 103 return str.substring(0, indexOf); 104 }; 105 106 /** 107 * Looks up a search string in a given string. 108 * @param str the given string 109 * @param searchString search string 110 * @returns {boolean} true, if substring exists in a given string. 111 */ 112 exports.contains = function (str, searchString) { 113 if (isEmpty(str)) { 114 return false; 115 } 116 return str.indexOf(searchString) >= 0; 117 }; 118 119 /** 120 * Generates a new string by repeating the given string n times 121 * @param str the given string 122 * @param times times to repeat the given string 123 * @returns {string} the generated string 124 */ 125 exports.repeat = function (str, times) { 126 if (isEmpty(str)) { 127 return ''; 128 } 129 return new Array(times + 1).join(str); 130 }; 131 132 /** 133 * Returns the length of the string. 134 * @param str string 135 * @returns {int} length 136 */ 137 exports.length = function (str) { 138 if (isEmpty(str)) { 139 return 0; 140 } 141 return str.length; 142 }; 143 144 /** 145 * Replaces each exact match of the searchString in a given string with a replaceString. 146 * Does not support regular expressions. 147 * @param {string} str original string 148 * @param {string} searchString search string 149 * @param {string} [replaceString=''] replace string 150 * @returns {string} 151 */ 152 exports.replace = function (str, searchString) { 153 var replaceString = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : ''; 154 if (isEmpty(str)) { 155 return ''; 156 } 157 if (isEmpty(searchString)) { 158 return ''; 159 } 160 var searchStart = 0; 161 var i = str.indexOf(searchString); 162 while (i >= 0) { 163 var beforeOccurrence = str.substring(0, i); 164 var afterOccurrence = str.substring(i + searchString.length); 165 str = beforeOccurrence + replaceString + afterOccurrence; 166 searchStart = i + replaceString.length; 167 i = str.indexOf(searchString, searchStart); 168 } 169 return str.substring(i, str.length); 170 }; 171 172 /** 173 * Returns a hash code of a passed string by calculating 174 * product of characters using 31 as multiplication factor. 175 * It is good for anonymity, not suitable for security purposes. 176 * 177 * @param {String} toHash 178 * @returns {String} hash code 179 */ 180 exports.hashCode = function hashCode(toHash) {
181 var charCode; 182 var hash = 0; 183 if (!toHash) { 184 return ''; 185 } 186 for (var i = 0; i < toHash.length; i += 1) { 187 charCode = toHash.charCodeAt(i); 188 hash = hash * 31 + charCode; 189 hash |= 0; // force 32-bit representation 190 } 191 return "".concat(hash); 192 }; 193 194 /** 195 * AMD-compatible replacement for deprecated AJS.escapeHtml. 196 * 197 * @param {String} html 198 * @returns {String} escaped html 199 */ 200 exports.escapeHtml = escapeHtml; 201 202 /** 203 * Capitalises first letter of given string. 204 * 205 * @param {String} str 206 * @returns {String} 207 */ 208 exports.toSentenceCase = function (str) { 209 if (!str) { 210 return ''; 211 } 212 str += ''; 213 return str.charAt(0).toUpperCase() + str.substring(1); 214 }; 215});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.