Debian Codegraph @

src/truncate/TruncateMiddle.js

raw symbols SCIP
1 /*global Autolinker */
2 /**
3 * Date: 2015-10-05
4 * Author: Kasper Søfren <[email protected]> (https://github.com/kafoso)
5 *
6 * A truncation feature, where the ellipsis will be placed in the dead-center of the URL.
7 *
8 * @param {String} url A URL.
9 * @param {Number} truncateLen The maximum length of the truncated output URL string.
10 * @param {String} ellipsisChars The characters to place within the url, e.g. "..".
11 * @return {String} The truncated URL.
12 */
13 Autolinker.truncate.TruncateMiddle = function(url, truncateLen, ellipsisChars){
14 if (url.length <= truncateLen) {
15 return url;
16 }
17
18 var ellipsisLengthBeforeParsing;
19 var ellipsisLength;
20
21 if(ellipsisChars == null) {
22 ellipsisChars = '&hellip;';
23 ellipsisLengthBeforeParsing = 8;
24 ellipsisLength = 3;
25 } else {
26 ellipsisLengthBeforeParsing = ellipsisChars.length;
27 ellipsisLength = ellipsisChars.length;
28 }
29
30 var availableLength = truncateLen - ellipsisLength;
31 var end = "";
32 if (availableLength > 0) {
33 end = url.substr((-1)*Math.floor(availableLength/2));
34 }
35 return (url.substr(0, Math.ceil(availableLength/2)) + ellipsisChars + end).substr(0, availableLength + ellipsisLengthBeforeParsing);
36 };