Add new unit tests

Add new unit tests for 'shorten' function in calendar module
This commit is contained in:
Bas van Wetten
2017-07-29 16:02:53 +02:00
parent bf24ee369f
commit 35e3b889c3
3 changed files with 56 additions and 16 deletions

View File

@@ -437,25 +437,27 @@ Module.register("calendar", {
return defaultValue;
},
/* shorten(string, maxLength)
* Shortens a string if it's longer than maxLength.
* Adds an ellipsis to the end.
*
* argument string string - The string to shorten.
* argument maxLength number - The max length of the string.
* argument wrapEvents - Wrap the text after the line has reached maxLength
*
* return string - The shortened string.
/**
* Shortens a string if it's longer than maxLength and add a ellipsis to the end
*
* @param {string} string Text string to shorten
* @param {number} maxLength The max length of the string
* @param {boolean} wrapEvents Wrap the text after the line has reached maxLength
* @returns {string} The shortened string
*/
shorten: function (string, maxLength, wrapEvents) {
if (wrapEvents) {
if (typeof string !== "string") {
return "";
}
if (wrapEvents === true) {
var temp = "";
var currentLine = "";
var words = string.split(" ");
for (var i = 0; i < words.length; i++) {
var word = words[i];
if (currentLine.length + word.length < 25 - 1) { // max - 1 to account for a space
if (currentLine.length + word.length < (typeof maxLength === "number" ? maxLength : 25) - 1) { // max - 1 to account for a space
currentLine += (word + " ");
} else {
if (currentLine.length > 0) {
@@ -467,12 +469,12 @@ Module.register("calendar", {
}
}
return temp + currentLine;
return (temp + currentLine).trim();
} else {
if (string.length > maxLength) {
return string.slice(0, maxLength) + "&hellip;";
if (maxLength && typeof maxLength === "number" && string.length > maxLength) {
return string.trim().slice(0, maxLength) + "&hellip;";
} else {
return string;
return string.trim();
}
}
},