* 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, options)
| 25693 | * // => 'hi-diddly-ho there, neig [...]' |
| 25694 | */ |
| 25695 | function truncate(string, options) { |
| 25696 | var length = DEFAULT_TRUNC_LENGTH, |
| 25697 | omission = DEFAULT_TRUNC_OMISSION; |
| 25698 | |
| 25699 | if (isObject(options)) { |
| 25700 | var separator = 'separator' in options ? options.separator : separator; |
| 25701 | length = 'length' in options ? toInteger(options.length) : length; |
| 25702 | omission = 'omission' in options ? baseToString(options.omission) : omission; |
| 25703 | } |
| 25704 | string = toString(string); |
| 25705 | |
| 25706 | var strLength = string.length; |
| 25707 | if (hasUnicode(string)) { |
| 25708 | var strSymbols = stringToArray(string); |
| 25709 | strLength = strSymbols.length; |
| 25710 | } |
| 25711 | if (length >= strLength) { |
| 25712 | return string; |
| 25713 | } |
| 25714 | var end = length - stringSize(omission); |
| 25715 | if (end < 1) { |
| 25716 | return omission; |
| 25717 | } |
| 25718 | var result = strSymbols |
| 25719 | ? castSlice(strSymbols, 0, end).join('') |
| 25720 | : string.slice(0, end); |
| 25721 | |
| 25722 | if (separator === undefined) { |
| 25723 | return result + omission; |
| 25724 | } |
| 25725 | if (strSymbols) { |
| 25726 | end += (result.length - end); |
| 25727 | } |
| 25728 | if (isRegExp(separator)) { |
| 25729 | if (string.slice(end).search(separator)) { |
| 25730 | var match, |
| 25731 | substring = result; |
| 25732 | |
| 25733 | if (!separator.global) { |
| 25734 | separator = RegExp(separator.source, toString(reFlags.exec(separator)) + 'g'); |
| 25735 | } |
| 25736 | separator.lastIndex = 0; |
| 25737 | while ((match = separator.exec(substring))) { |
| 25738 | var newEnd = match.index; |
| 25739 | } |
| 25740 | result = result.slice(0, newEnd === undefined ? end : newEnd); |
| 25741 | } |
| 25742 | } else if (string.indexOf(baseToString(separator), end) != end) { |
| 25743 | var index = result.lastIndexOf(separator); |
| 25744 | if (index > -1) { |
| 25745 | result = result.slice(0, index); |
| 25746 | } |
| 25747 | } |
| 25748 | return result + omission; |
| 25749 | } |
| 25750 | |
| 25751 | /** |
| 25752 | * The inverse of `_.escape`; this method converts the HTML entities |
nothing calls this directly
no test coverage detected