Skip to content

Commit 8047b46

Browse files
author
Toby Brain
committed
Removing duplicate code, refactoring
1 parent 4fe6892 commit 8047b46

File tree

2 files changed

+25
-26
lines changed

2 files changed

+25
-26
lines changed

jquery.truncate.js

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
function isNumber(n) {
33
return !isNaN(parseFloat(n)) && isFinite(n);
44
}
5-
function findTruncPoint(maxWidth, text, start, end, $workerEl, token, fromEnd, count) {
6-
var opt1 = '',
7-
opt2 = '',
8-
mid = 0;
9-
count++;
5+
function findTruncPoint(maxWidth, text, start, end, $workerEl, token, fromEnd) {
6+
var opt1,
7+
opt2,
8+
mid;
9+
1010
if (fromEnd) {
11-
opt1 = start == 0 ? '' : text.slice(-start);
11+
opt1 = start === 0 ? '' : text.slice(-start);
1212
opt2 = text.slice(-end);
1313
} else {
1414
opt1 = text.slice(0, start);
@@ -18,23 +18,22 @@
1818
if ($workerEl.html(opt2 + token).width() < $workerEl.html(opt1 + token).width()) {
1919
return end;
2020
}
21-
mid = parseInt((start + end) / 2, 10);
22-
if (fromEnd) {
23-
opt1 = text.slice(-mid);
24-
} else {
25-
opt1 = text.slice(0, mid);
26-
}
2721

22+
mid = parseInt((start + end) / 2, 10);
23+
opt1 = fromEnd ? text.slice(-mid) : text.slice(0, mid);
2824

29-
if ($workerEl.html(opt1 + token).width() == maxWidth) {
25+
$workerEl.html(opt1 + token);
26+
if ($workerEl.width() === maxWidth) {
3027
return mid;
3128
}
3229

33-
if ($workerEl.html(opt1 + token).width() > maxWidth) {
34-
return findTruncPoint(maxWidth, text, start, (mid - 1), $workerEl, token, fromEnd, count);
30+
if ($workerEl.width() > maxWidth) {
31+
end = mid - 1;
3532
} else {
36-
return findTruncPoint(maxWidth, text, (mid + 1), end, $workerEl, token, fromEnd, count);
33+
start = mid + 1;
3734
}
35+
36+
return findTruncPoint(maxWidth, text, start, end, $workerEl, token, fromEnd);
3837
}
3938

4039
$.fn.truncate = function (options) {
@@ -62,20 +61,20 @@
6261
'display': 'none'
6362
},
6463
elementText = $element.text(),
65-
truncatedText = '',
6664
$truncateWorker = $('<span/>').css(fontCSS).html(elementText).appendTo('body'),
6765
originalWidth = $truncateWorker.width(),
68-
truncateWidth = isNumber(options.width) ? options.width : $element.width();
66+
truncateWidth = isNumber(options.width) ? options.width : $element.width(),
67+
truncatedText;
6968

7069
if (originalWidth > truncateWidth) {
7170
$truncateWorker.text('');
7271
if (options.center) {
73-
truncatedText = elementText.slice(0, findTruncPoint((parseInt(truncateWidth / 2, 10) + 1), elementText, 0, elementText.length, $truncateWorker, options.token, false, 0))
72+
truncateWidth = parseInt(truncateWidth / 2, 10) + 1;
73+
truncatedText = elementText.slice(0, findTruncPoint(truncateWidth, elementText, 0, elementText.length, $truncateWorker, options.token, false))
7474
+ options.token
75-
+ elementText.slice(-1 * findTruncPoint((parseInt(truncateWidth / 2, 10) + 1), elementText, 0, elementText.length, $truncateWorker, '', true, 0));
76-
}
77-
else {
78-
truncatedText = elementText.slice(0, findTruncPoint(truncateWidth, elementText, 0, elementText.length, $truncateWorker, options.token, false, 0)) + options.token;
75+
+ elementText.slice(-1 * findTruncPoint(truncateWidth, elementText, 0, elementText.length, $truncateWorker, '', true));
76+
} else {
77+
truncatedText = elementText.slice(0, findTruncPoint(truncateWidth, elementText, 0, elementText.length, $truncateWorker, options.token, false)) + options.token;
7978
}
8079

8180
if (options.addclass) {

jquery.truncate.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)