-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
truncate.js
73 lines (64 loc) · 2.53 KB
/
truncate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*global module:true*/
/*jslint nomen:true*/
/**
* @module Utility
*/
(function (context, undefined) {
'use strict';
var DEFAULT_TRUNCATE_SYMBOL = '…',
// Limit emails to no more than about 600 chars, well over the max of ~300.
// cf. RFC: https://www.rfc-editor.org/errata_search.php?eid=1690
URL_REGEX = /(((ftp|https?):\/\/)[\-\w@:%_\+.~#?,&\/\/=]+)|((mailto:)?[_.\w-]{1,300}@(.{1,300}\.)[a-zA-Z]{2,3})/g;
function __appendEllipsis(string, options, content) {
if (content.length === string.length || !options.ellipsis) {
return content;
}
content += options.ellipsis;
return content;
}
/**
* Truncate HTML string and keep tag safe.
*
* @method truncate
* @param {String} string string needs to be truncated
* @param {Number} maxLength length of truncated string
* @param {Object} options (optional)
* @param {Boolean|String} [options.ellipsis] omission symbol for truncated string, '...' by default
* @return {String} truncated string
*/
function truncate(string, maxLength, options) {
var content = '', // truncated text storage
matches = true,
remainingLength = maxLength,
result,
index;
options = options || {};
options.ellipsis = (typeof options.ellipsis === "undefined") ? DEFAULT_TRUNCATE_SYMBOL : options.ellipsis;
if (!string || string.length === 0) {
return '';
}
matches = true;
while (matches) {
URL_REGEX.lastIndex = content.length;
matches = URL_REGEX.exec(string);
// Don't try to retain URLs longer than 3k chars, well over the 99th percentile of ~347
if (!matches || (matches.index - content.length) >= remainingLength || URL_REGEX.lastIndex >= (maxLength + 3000)) {
content += string.substring(content.length, maxLength);
return __appendEllipsis(string, options, content, maxLength);
}
result = matches[0];
index = matches.index;
content += string.substring(content.length, index + result.length);
remainingLength -= index + result.length;
if (remainingLength <= 0) {
break;
}
}
return __appendEllipsis(string, options, content, maxLength);
}
if ('undefined' !== typeof module && module.exports) {
module.exports = truncate;
} else {
context.truncate = truncate;
}
}(String));