change the signature of Ox.truncate to allow for passing 'position' without having to pass 'padding', change default padding from '...' to a real ellipsis

This commit is contained in:
rolux 2012-06-03 11:07:39 +02:00
parent 3ec610076a
commit 9ce126263b
2 changed files with 12 additions and 13 deletions

View file

@ -163,7 +163,7 @@ Ox.IconItem = function(options, self) {
// truncated rest (including the last line) and discard all extra lines
if (lines.length > maxLines) {
lines[maxLines - 1] = Ox.truncate(
lines.slice(maxLines - 1).join(' '), maxLength, '...', 'center'
lines.slice(maxLines - 1).join(' '), 'center', maxLength
);
lines = lines.slice(0, maxLines);
}

View file

@ -469,22 +469,21 @@ Ox.toUnderscores = function(string) {
/*@
Ox.truncate <f> Truncate a string to a given length
(string, length) <s> Truncated string
(string, length, position) -> <s> Truncated string
(string, length, placeholder) -> <s> Truncated string
(string, length, position, placeholder) -> <s> Truncated string
(string[, position], length[, padding]) -> Truncated string
> Ox.truncate('anticonstitutionellement', 16)
'anticonstitutio…'
> Ox.truncate('anticonstitutionellement', 'left', 16)
'…itutionellement'
> Ox.truncate('anticonstitutionellement', 16, '...')
'anticonstitut...'
> Ox.truncate('anticonstitutionellement', 16, '...', 'left')
'...utionellement'
> Ox.truncate('anticonstitutionellement', 16, '>')
'anticonstitutio>'
> Ox.truncate('anticonstitutionellement', 16, '...', 'center')
> Ox.truncate('anticonstitutionellement', 'center', 16, '...')
'anticon...lement'
@*/
Ox.truncate = function(string, length, padding, position) {
padding = padding || '...';
position = position || 'right';
Ox.truncate = function(string, position, length, padding) {
var hasPosition = Ox.isString(arguments[1]), last = Ox.last(arguments);
position = hasPosition ? arguments[1] : 'right';
length = hasPosition ? arguments[2] : arguments[1];
padding = Ox.isString(last) ? last : '…';
if (string.length > length) {
if (position == 'left') {
string = padding