Reputation: 4492
function w_3_wid(str, wid03) {
var word = new Array();
var i;
var ret = '';
word = str.split(" ");
for (i = 0; i < word.length; i ++ ) {
if (word[i].length > wid03 && word[i].search(/&\w+;/) < 0) ret += word[i].substr(0, wid03) + ' ' + word[i].substr(wid03) + ' ';
else ret += word[i] + ' ';
}
return ret;
}
function w_4_wid(str, wid03) {
if (str.length <= wid03) return str;
var word = new Array();
word = str.split(" ");
var ret = word[0] + ' ';
var test;
for (i = 1; i < word.length; i ++ ) {
test = ret + word[i];
if (test.length > wid03) return ret + '...';
else ret += word[i] + ' ';
}
return str;
}
function w_6_wid(title) {
title = w_3_wid(title, 15);
title = w_4_wid(title, 60);
return title;
}
w_6_wid(str);
Upvotes: 0
Views: 146
Reputation: 6787
first: your programmer should read the book "Clean Code".
The idea of the function w_3_wid
is - for what ever reason - to trunkate words which are longer than 15 characters AND containing an html entity (like
)
The second function (w_4_wid
) truncates the string at word boundaries.
The last one (w_6_wid
) combines both functions.
To test these code you can append something like this:
alert(w_6_wid('Lorem ipsum abcdefghijklmnopqu abcdefghijklm&bnsp;nopqu'));
Upvotes: 1
Reputation: 22808
w_4_wid
looks like it tries to truncate a sentence to a maximum width of the second parameter(60).
Upvotes: 0
Reputation:
Looks like the code is trying to take a sentence and then first truncate long words given a threshold, and then truncate the whole sentence (given another threshold), ending the sentence with "...".
Upvotes: 1
Reputation: 23169
w_4_wid
appears to truncate a block of text to a maximum number of characters, without splitting words, and if the text is truncated to add '...' elipses at the end of the sentence.
w_3_wid
seems to do something similar, imposing a maximum number of characters per word.
w_6_wid
calls the above two functions in a chain, imposing both constraints on the input text.
Upvotes: 2