Added the Porter Stemmer module to improve searches. This doesn't deal with some...
[yaffs-website] / node_modules / uncss / node_modules / lodash / truncate.js
1 var isObject = require('./isObject'),
2     isRegExp = require('./isRegExp'),
3     stringSize = require('./internal/stringSize'),
4     stringToArray = require('./internal/stringToArray'),
5     toInteger = require('./toInteger'),
6     toString = require('./toString');
7
8 /** Used as default options for `_.truncate`. */
9 var DEFAULT_TRUNC_LENGTH = 30,
10     DEFAULT_TRUNC_OMISSION = '...';
11
12 /** Used to match `RegExp` flags from their coerced string values. */
13 var reFlags = /\w*$/;
14
15 /** Used to compose unicode character classes. */
16 var rsAstralRange = '\\ud800-\\udfff',
17     rsComboMarksRange = '\\u0300-\\u036f\\ufe20-\\ufe23',
18     rsComboSymbolsRange = '\\u20d0-\\u20f0',
19     rsVarRange = '\\ufe0e\\ufe0f';
20
21 /** Used to compose unicode capture groups. */
22 var rsZWJ = '\\u200d';
23
24 /** Used to detect strings with [zero-width joiners or code points from the astral planes](http://eev.ee/blog/2015/09/12/dark-corners-of-unicode/). */
25 var reHasComplexSymbol = RegExp('[' + rsZWJ + rsAstralRange  + rsComboMarksRange + rsComboSymbolsRange + rsVarRange + ']');
26
27 /**
28  * Truncates `string` if it's longer than the given maximum string length.
29  * The last characters of the truncated string are replaced with the omission
30  * string which defaults to "...".
31  *
32  * @static
33  * @memberOf _
34  * @category String
35  * @param {string} [string=''] The string to truncate.
36  * @param {Object} [options] The options object.
37  * @param {number} [options.length=30] The maximum string length.
38  * @param {string} [options.omission='...'] The string to indicate text is omitted.
39  * @param {RegExp|string} [options.separator] The separator pattern to truncate to.
40  * @returns {string} Returns the truncated string.
41  * @example
42  *
43  * _.truncate('hi-diddly-ho there, neighborino');
44  * // => 'hi-diddly-ho there, neighbo...'
45  *
46  * _.truncate('hi-diddly-ho there, neighborino', {
47  *   'length': 24,
48  *   'separator': ' '
49  * });
50  * // => 'hi-diddly-ho there,...'
51  *
52  * _.truncate('hi-diddly-ho there, neighborino', {
53  *   'length': 24,
54  *   'separator': /,? +/
55  * });
56  * // => 'hi-diddly-ho there...'
57  *
58  * _.truncate('hi-diddly-ho there, neighborino', {
59  *   'omission': ' [...]'
60  * });
61  * // => 'hi-diddly-ho there, neig [...]'
62  */
63 function truncate(string, options) {
64   var length = DEFAULT_TRUNC_LENGTH,
65       omission = DEFAULT_TRUNC_OMISSION;
66
67   if (isObject(options)) {
68     var separator = 'separator' in options ? options.separator : separator;
69     length = 'length' in options ? toInteger(options.length) : length;
70     omission = 'omission' in options ? toString(options.omission) : omission;
71   }
72   string = toString(string);
73
74   var strLength = string.length;
75   if (reHasComplexSymbol.test(string)) {
76     var strSymbols = stringToArray(string);
77     strLength = strSymbols.length;
78   }
79   if (length >= strLength) {
80     return string;
81   }
82   var end = length - stringSize(omission);
83   if (end < 1) {
84     return omission;
85   }
86   var result = strSymbols
87     ? strSymbols.slice(0, end).join('')
88     : string.slice(0, end);
89
90   if (separator === undefined) {
91     return result + omission;
92   }
93   if (strSymbols) {
94     end += (result.length - end);
95   }
96   if (isRegExp(separator)) {
97     if (string.slice(end).search(separator)) {
98       var match,
99           substring = result;
100
101       if (!separator.global) {
102         separator = RegExp(separator.source, toString(reFlags.exec(separator)) + 'g');
103       }
104       separator.lastIndex = 0;
105       while ((match = separator.exec(substring))) {
106         var newEnd = match.index;
107       }
108       result = result.slice(0, newEnd === undefined ? end : newEnd);
109     }
110   } else if (string.indexOf(separator, end) != end) {
111     var index = result.lastIndexOf(separator);
112     if (index > -1) {
113       result = result.slice(0, index);
114     }
115   }
116   return result + omission;
117 }
118
119 module.exports = truncate;