forked from FGRibreau/node-truncate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
truncate.js
66 lines (57 loc) · 2.32 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
/*global module:true*/
/*jslint nomen:true*/
/**
* @module Utility
*/
(function (context, undefined) {
'use strict';
var DEFAULT_TRUNCATE_SYMBOL = '...',
URL_REGEX = /(((ftp|https?):\/\/)[\-\w@:%_\+.~#?,&\/\/=]+)|((mailto:)?[_.\w-]+@([\w][\w\-]+\.)+[a-zA-Z]{2,3})/g; // Simple regexp
function __appendEllipsis(string, maxLength, options, content){
if(string.length <= maxLength || !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} [options.keepImageTag] flag to specify if keep image tag, false by default
* @param {Boolean|String} [options.ellipsis] omission symbol for truncated string, '...' by default
* @return {String} truncated string
*/
function truncate(string, maxLength, options) {
var total = 0, // record how many characters we traced so far
content = '', // truncated text storage
matches = true,
result,
index;
options = options || {};
options.ellipsis = (typeof options.ellipsis === "undefined") ? DEFAULT_TRUNCATE_SYMBOL : options.ellipsis;
if (total >= maxLength) {
return __appendEllipsis(string, maxLength, options, content);
}
URL_REGEX.lastIndex = 0;
matches = URL_REGEX.exec(string);
if(!matches || matches.index >= maxLength){
content += string.substring(0, maxLength - total);
return __appendEllipsis(string, maxLength, options, content);
}
while(matches){
result = matches[0];
index = matches.index;
content += string.substring(0, (index + result.length) - total);
string = string.substring(index + result.length);
matches = URL_REGEX.exec(string);
}
return __appendEllipsis(string, maxLength, options, content);
}
if ('undefined' !== typeof module && module.exports) {
module.exports = truncate;
} else {
context.truncate = truncate;
}
}(String));