better split logic for names starting with capitols eg: ABBob should split to AB Bob

This commit is contained in:
Sam 2013-09-04 15:02:04 +10:00
parent 9977f3098c
commit 61d3e43744
2 changed files with 23 additions and 6 deletions

View File

@ -13,13 +13,29 @@ Discourse.Formatter = (function(){
var firstPart = string.substr(0, maxLength);
var betterSplit = firstPart.substr(1).search(/[^a-z]/);
if (betterSplit >= 0) {
var offset = 1;
if(string[betterSplit+1] === "_") {
offset = 2;
// work backward to split stuff like ABPoop to AB Poop
var i;
for(i=firstPart.length-1;i>0;i--){
if(firstPart[i].match(/[A-Z]/)){
break;
}
return string.substr(0, betterSplit + offset) + " " + string.substring(betterSplit + offset);
}
// work forwards to split stuff like ab111 to ab 111
if(i===0) {
for(i=1;i<firstPart.length;i++){
if(firstPart[i].match(/[^a-z]/)){
break;
}
}
}
if (i > 0 && i < firstPart.length) {
var offset = 0;
if(string[i] === "_") {
offset = 1;
}
return string.substr(0, i + offset) + " " + string.substring(i + offset);
} else {
return firstPart + " " + string.substr(maxLength);
}

View File

@ -199,5 +199,6 @@ test("breakUp", function(){
equal(b("HeMans"), "He Mans");
equal(b("he_man"), "he_ man");
equal(b("he11111"), "he 11111");
equal(b("HRCBob"), "HRC Bob");
});