Skip to content

Commit

Permalink
Merge pull request #197 from sergeyshushlyapin/bug/#192
Browse files Browse the repository at this point in the history
Abbreviations displayed incorrectly with spaces between letters
  • Loading branch information
dirkrombauts committed May 12, 2015
2 parents 7daab38 + a519362 commit ef3bd74
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
22 changes: 19 additions & 3 deletions src/Pickles/Pickles.BaseDhtmlFiles/js/stringFormatting.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,28 @@
// IE does not implement trim() and the following replaces the functionality if unavailable
// http://stackoverflow.com/questions/2308134/trim-in-javascript-not-working-in-ie
if (typeof String.prototype.trim !== 'function') {
String.prototype.trim = function() {
String.prototype.trim = function () {
return this.replace(/^\s+|\s+$/g, '');
}
}

return unformattedString.replace(/([A-Z])/g, " $1").trim();

var formattedString = '';
var i = 0;
while (i <= unformattedString.length) {
var ch = unformattedString.charAt(i);
var nextChar = unformattedString.charAt(i + 1);

if (ch == ch.toLowerCase() && nextChar == nextChar.toUpperCase()) {
formattedString = formattedString.trim() + ch + ' ';
} else if (ch == ch.toUpperCase() && nextChar == nextChar.toLowerCase() && nextChar != nextChar.toUpperCase() && nextChar != '') {
formattedString = formattedString.trim() + ' ' + ch;
} else {
formattedString += ch;
}
i++;
}

return formattedString.trim();
}

function removeBeginningHash(hashedString) {
Expand Down
5 changes: 4 additions & 1 deletion src/Pickles/Pickles.BaseDhtmlFiles/tests/tests.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
test("Can convert camel cased string to spaced string", function () {
equal(addSpacesToCamelCasedString("ThisIsATestString"), "This Is A Test String", "Happy path test");
equal(addSpacesToCamelCasedString("00ThisIsATestString"), "00 This Is A Test String", "String prefaced by numbers");
equal(addSpacesToCamelCasedString("ThisIsATestString."), "This Is A Test String.", "String ending with puncuation.");
equal(addSpacesToCamelCasedString("ThisIsATestString."), "This Is A Test String.", "String ending with punctuation.");
equal(addSpacesToCamelCasedString("ThisIsATestStringABC"), "This Is A Test String ABC", "String with abbreviation at the end");
equal(addSpacesToCamelCasedString("ThisIsATestStringABC."), "This Is A Test String ABC.", "String with abbreviation and punctuation");
equal(addSpacesToCamelCasedString("ABCThisIsATestString"), "ABC This Is A Test String", "String with abbreviation at the beginning");
});

test("Can pull off # from hashtag", function() {
Expand Down

0 comments on commit ef3bd74

Please sign in to comment.