mirror of
https://github.com/discourse/discourse.git
synced 2025-05-28 01:56:58 +08:00
improve the breakUp user name algorithm, add some tests
This commit is contained in:
@ -4,7 +4,26 @@ Discourse.Formatter = (function(){
|
|||||||
|
|
||||||
var updateRelativeAge, autoUpdatingRelativeAge, relativeAge, relativeAgeTiny,
|
var updateRelativeAge, autoUpdatingRelativeAge, relativeAge, relativeAgeTiny,
|
||||||
relativeAgeMedium, relativeAgeMediumSpan, longDate, toTitleCase,
|
relativeAgeMedium, relativeAgeMediumSpan, longDate, toTitleCase,
|
||||||
shortDate;
|
shortDate, breakUp;
|
||||||
|
|
||||||
|
breakUp = function(string, maxLength){
|
||||||
|
if(string.length <= maxLength) {
|
||||||
|
return string;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
return string.substr(0, betterSplit + offset) + " " + string.substring(betterSplit + offset);
|
||||||
|
} else {
|
||||||
|
return firstPart + " " + string.substr(maxLength);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
shortDate = function(date){
|
shortDate = function(date){
|
||||||
return moment(date).shortDate();
|
return moment(date).shortDate();
|
||||||
@ -189,6 +208,7 @@ Discourse.Formatter = (function(){
|
|||||||
autoUpdatingRelativeAge: autoUpdatingRelativeAge,
|
autoUpdatingRelativeAge: autoUpdatingRelativeAge,
|
||||||
updateRelativeAge: updateRelativeAge,
|
updateRelativeAge: updateRelativeAge,
|
||||||
toTitleCase: toTitleCase,
|
toTitleCase: toTitleCase,
|
||||||
shortDate: shortDate
|
shortDate: shortDate,
|
||||||
|
breakUp: breakUp
|
||||||
};
|
};
|
||||||
})();
|
})();
|
||||||
|
@ -9,17 +9,7 @@ Handlebars.registerHelper('breakUp', function(property, options) {
|
|||||||
prop = Ember.Handlebars.get(this, property, options);
|
prop = Ember.Handlebars.get(this, property, options);
|
||||||
if (!prop) return "";
|
if (!prop) return "";
|
||||||
|
|
||||||
tokens = prop.match(new RegExp(".{1,14}", 'g'));
|
return Discourse.Formatter.breakUp(prop, 13);
|
||||||
if (tokens.length === 1) return prop;
|
|
||||||
|
|
||||||
result = "";
|
|
||||||
_.each(tokens,function(token,index) {
|
|
||||||
result += token;
|
|
||||||
if (token.indexOf(' ') === -1 && (index < tokens.length - 1)) {
|
|
||||||
result += "- ";
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return result;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -320,4 +310,4 @@ Handlebars.registerHelper('faqLink', function(property, options) {
|
|||||||
(Discourse.SiteSettings.faq_url.length > 0 ? Discourse.SiteSettings.faq_url : Discourse.getURL('/faq')) +
|
(Discourse.SiteSettings.faq_url.length > 0 ? Discourse.SiteSettings.faq_url : Discourse.getURL('/faq')) +
|
||||||
"'>" + Em.String.i18n('faq') + "</a>"
|
"'>" + Em.String.i18n('faq') + "</a>"
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
@ -122,3 +122,14 @@ test("updateRelativeAge", function(){
|
|||||||
|
|
||||||
equal($elem.html(), "2 mins ago");
|
equal($elem.html(), "2 mins ago");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("breakUp", function(){
|
||||||
|
|
||||||
|
var b = function(s){ return Discourse.Formatter.breakUp(s,5); };
|
||||||
|
|
||||||
|
equal(b("hello"), "hello");
|
||||||
|
equal(b("helloworld"), "hello world");
|
||||||
|
equal(b("HeMans"), "He Mans");
|
||||||
|
equal(b("he_man"), "he_ man");
|
||||||
|
|
||||||
|
});
|
||||||
|
Reference in New Issue
Block a user