Skip to content

Commit

Permalink
Remove the string reversals
Browse files Browse the repository at this point in the history
  • Loading branch information
Toby Brain committed Apr 4, 2011
1 parent 56fcf0f commit 2841f74
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
23 changes: 10 additions & 13 deletions jquery.truncate.js
Expand Up @@ -2,17 +2,14 @@
function isNumber(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
function reverseString(str) {
return str.split('').reverse().join('');
};
function findTruncPoint(maxWidth, text, start, end, $workerEl, token, reversed, count) {
function findTruncPoint(maxWidth, text, start, end, $workerEl, token, fromEnd, count) {
var opt1 = '',
opt2 = '',
mid = 0;
count++;
if (reversed) {
opt1 = reverseString(reverseString(text).slice(0, start));
opt2 = reverseString(reverseString(text).slice(0, end));
if (fromEnd) {
opt1 = start == 0 ? '' : text.slice(-start);
opt2 = text.slice(-end);
} else {
opt1 = text.slice(0, start);
opt2 = text.slice(0, end);
Expand All @@ -22,8 +19,8 @@
return end;
}
mid = parseInt((start + end) / 2, 10);
if (reversed) {
opt1 = reverseString(reverseString(text).slice(0, mid));
if (fromEnd) {
opt1 = text.slice(-mid);
} else {
opt1 = text.slice(0, mid);
}
Expand All @@ -34,13 +31,13 @@
}

if ($workerEl.html(opt1 + token).width() > maxWidth) {
return findTruncPoint(maxWidth, text, start, (mid - 1), $workerEl, token, reversed, count);
return findTruncPoint(maxWidth, text, start, (mid - 1), $workerEl, token, fromEnd, count);
} else {
return findTruncPoint(maxWidth, text, (mid + 1), end, $workerEl, token, reversed, count);
return findTruncPoint(maxWidth, text, (mid + 1), end, $workerEl, token, fromEnd, count);
}
};
}

$.fn.truncate = function (options) {
$.fn.truncatenew = function (options) {
var defaults = {
width: 'auto',
token: '…',
Expand Down
6 changes: 3 additions & 3 deletions jquery.truncate.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2841f74

Please sign in to comment.