* Truncates `string` if it's longer than the given maximum string length. * The last characters of the truncated string are replaced with the omission * string which defaults to "...". * * @static * @memberOf _ * @since 4.0.0 * @category String * @param {strin
(string, options)
| 15194 | * // => 'hi-diddly-ho there, neig [...]' |
| 15195 | */ |
| 15196 | function truncate(string, options) { |
| 15197 | var length = DEFAULT_TRUNC_LENGTH, |
| 15198 | omission = DEFAULT_TRUNC_OMISSION; |
| 15199 | |
| 15200 | if (isObject(options)) { |
| 15201 | var separator = 'separator' in options ? options.separator : separator; |
| 15202 | length = 'length' in options ? toInteger(options.length) : length; |
| 15203 | omission = 'omission' in options ? baseToString(options.omission) : omission; |
| 15204 | } |
| 15205 | string = toString(string); |
| 15206 | |
| 15207 | var strLength = string.length; |
| 15208 | if (hasUnicode(string)) { |
| 15209 | var strSymbols = stringToArray(string); |
| 15210 | strLength = strSymbols.length; |
| 15211 | } |
| 15212 | if (length >= strLength) { |
| 15213 | return string; |
| 15214 | } |
| 15215 | var end = length - stringSize(omission); |
| 15216 | if (end < 1) { |
| 15217 | return omission; |
| 15218 | } |
| 15219 | var result = strSymbols |
| 15220 | ? castSlice(strSymbols, 0, end).join('') |
| 15221 | : string.slice(0, end); |
| 15222 | |
| 15223 | if (separator === undefined) { |
| 15224 | return result + omission; |
| 15225 | } |
| 15226 | if (strSymbols) { |
| 15227 | end += (result.length - end); |
| 15228 | } |
| 15229 | if (isRegExp(separator)) { |
| 15230 | if (string.slice(end).search(separator)) { |
| 15231 | var match, |
| 15232 | substring = result; |
| 15233 | |
| 15234 | if (!separator.global) { |
| 15235 | separator = RegExp(separator.source, toString(reFlags.exec(separator)) + 'g'); |
| 15236 | } |
| 15237 | separator.lastIndex = 0; |
| 15238 | while ((match = separator.exec(substring))) { |
| 15239 | var newEnd = match.index; |
| 15240 | } |
| 15241 | result = result.slice(0, newEnd === undefined ? end : newEnd); |
| 15242 | } |
| 15243 | } else if (string.indexOf(baseToString(separator), end) != end) { |
| 15244 | var index = result.lastIndexOf(separator); |
| 15245 | if (index > -1) { |
| 15246 | result = result.slice(0, index); |
| 15247 | } |
| 15248 | } |
| 15249 | return result + omission; |
| 15250 | } |
| 15251 | |
| 15252 | /** |
| 15253 | * The inverse of `_.escape`; this method converts the HTML entities |
nothing calls this directly
no test coverage detected