1 var baseToString = require('./_baseToString'),
2 castSlice = require('./_castSlice'),
3 hasUnicode = require('./_hasUnicode'),
4 isObject = require('./isObject'),
5 isRegExp = require('./isRegExp'),
6 stringSize = require('./_stringSize'),
7 stringToArray = require('./_stringToArray'),
8 toInteger = require('./toInteger'),
9 toString = require('./toString');
11 /** Used as default options for `_.truncate`. */
12 var DEFAULT_TRUNC_LENGTH = 30,
13 DEFAULT_TRUNC_OMISSION = '...';
15 /** Used to match `RegExp` flags from their coerced string values. */
19 * Truncates `string` if it's longer than the given maximum string length.
20 * The last characters of the truncated string are replaced with the omission
21 * string which defaults to "...".
27 * @param {string} [string=''] The string to truncate.
28 * @param {Object} [options={}] The options object.
29 * @param {number} [options.length=30] The maximum string length.
30 * @param {string} [options.omission='...'] The string to indicate text is omitted.
31 * @param {RegExp|string} [options.separator] The separator pattern to truncate to.
32 * @returns {string} Returns the truncated string.
35 * _.truncate('hi-diddly-ho there, neighborino');
36 * // => 'hi-diddly-ho there, neighbo...'
38 * _.truncate('hi-diddly-ho there, neighborino', {
42 * // => 'hi-diddly-ho there,...'
44 * _.truncate('hi-diddly-ho there, neighborino', {
48 * // => 'hi-diddly-ho there...'
50 * _.truncate('hi-diddly-ho there, neighborino', {
51 * 'omission': ' [...]'
53 * // => 'hi-diddly-ho there, neig [...]'
55 function truncate(string, options) {
56 var length = DEFAULT_TRUNC_LENGTH,
57 omission = DEFAULT_TRUNC_OMISSION;
59 if (isObject(options)) {
60 var separator = 'separator' in options ? options.separator : separator;
61 length = 'length' in options ? toInteger(options.length) : length;
62 omission = 'omission' in options ? baseToString(options.omission) : omission;
64 string = toString(string);
66 var strLength = string.length;
67 if (hasUnicode(string)) {
68 var strSymbols = stringToArray(string);
69 strLength = strSymbols.length;
71 if (length >= strLength) {
74 var end = length - stringSize(omission);
78 var result = strSymbols
79 ? castSlice(strSymbols, 0, end).join('')
80 : string.slice(0, end);
82 if (separator === undefined) {
83 return result + omission;
86 end += (result.length - end);
88 if (isRegExp(separator)) {
89 if (string.slice(end).search(separator)) {
93 if (!separator.global) {
94 separator = RegExp(separator.source, toString(reFlags.exec(separator)) + 'g');
96 separator.lastIndex = 0;
97 while ((match = separator.exec(substring))) {
98 var newEnd = match.index;
100 result = result.slice(0, newEnd === undefined ? end : newEnd);
102 } else if (string.indexOf(baseToString(separator), end) != end) {
103 var index = result.lastIndexOf(separator);
105 result = result.slice(0, index);
108 return result + omission;
111 module.exports = truncate;