From 9ce126263b4628699ba15525dafcfa3ccb82db61 Mon Sep 17 00:00:00 2001 From: rolux Date: Sun, 3 Jun 2012 11:07:39 +0200 Subject: [PATCH] change the signature of Ox.truncate to allow for passing 'position' without having to pass 'padding', change default padding from '...' to a real ellipsis --- source/Ox.UI/js/List/IconItem.js | 2 +- source/Ox/js/String.js | 23 +++++++++++------------ 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/source/Ox.UI/js/List/IconItem.js b/source/Ox.UI/js/List/IconItem.js index 907017c8..cb930c55 100644 --- a/source/Ox.UI/js/List/IconItem.js +++ b/source/Ox.UI/js/List/IconItem.js @@ -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); } diff --git a/source/Ox/js/String.js b/source/Ox/js/String.js index a7cc6dd3..5f9c47fa 100644 --- a/source/Ox/js/String.js +++ b/source/Ox/js/String.js @@ -469,22 +469,21 @@ Ox.toUnderscores = function(string) { /*@ Ox.truncate Truncate a string to a given length - (string, length) Truncated string - (string, length, position) -> Truncated string - (string, length, placeholder) -> Truncated string - (string, length, position, placeholder) -> 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