From 6116f4b7f1e2893b9af059c1baea2b3be5a2b0ec Mon Sep 17 00:00:00 2001 From: rolux Date: Mon, 4 Jun 2012 13:49:10 +0200 Subject: [PATCH] cleanup Ox.wordwrap; remove Ox.basename --- source/Ox/js/String.js | 97 +++++++++++++++++++++--------------------- 1 file changed, 49 insertions(+), 48 deletions(-) diff --git a/source/Ox/js/String.js b/source/Ox/js/String.js index 2650a768..22f4df17 100644 --- a/source/Ox/js/String.js +++ b/source/Ox/js/String.js @@ -1,16 +1,5 @@ 'use strict'; -Ox.basename = function(string) { - /* - fixme: deprecate - >>> Ox.basename("foo/bar/foo.bar") - "foo.bar" - >>> Ox.basename("foo.bar") - "foo.bar" - */ - return string.replace(/^.*[\/\\]/g, ''); -}; - /*@ Ox.char Alias for String.fromCharCode @*/ @@ -570,36 +559,52 @@ Ox.words = function(string) { /*@ Ox.wordwrap Wrap a string at word boundaries - > Ox.wordwrap("Anticonstitutionellement, Paris s'eveille", 25, '
') - "Anticonstitutionellement,
Paris s'eveille" - > Ox.wordwrap("Anticonstitutionellement, Paris s'eveille", 16, '
') - "Anticonstitution
ellement, Paris
s'eveille" - > Ox.wordwrap('These are short words', 16, '
', true) - 'These are
short words' + (string, length) -> Wrapped string + (string, length, newline) -> Wrapped string + (string, length, balanced) -> Wrapped string + (string, length, balanced, newline) -> Wrapped string + (string, length, newline, balanced) -> Wrapped string + string String + length Line length + balanced If true, lines will have similar length + newline New line character or string + > Ox.wordwrap('Anticonstitutionellement, Paris s\'eveille', 25) + 'Anticonstitutionellement, \nParis s\'eveille' + > Ox.wordwrap('Anticonstitutionellement, Paris s\'eveille', 25, '
') + 'Anticonstitutionellement,
Paris s\'eveille' + > Ox.wordwrap('Anticonstitutionellement, Paris s\'eveille', 16, '
') + 'Anticonstitution
ellement, Paris
s\'eveille' + > Ox.wordwrap('These are short words', 16) + 'These are short \nwords' + > Ox.wordwrap('These are short words', 16, true) + 'These are \nshort words' @*/ -Ox.wordwrap = function(str, len, sep, bal, spa) { - // fixme: bad API, sep/bal/spa should be in options object - var str = str === null ? '' : str.toString(), - len = len || 80, - sep = sep || '
', - bal = bal || false, - spa = Ox.isUndefined(spa) ? true : spa, - words = str.split(' '), - lines; - if (bal) { - // balance lines: test if same number of lines +Ox.wordwrap = function(string, length) { + var balanced, lines, max, newline, words; + string = String(string); + length = length || 80; + balanced = Ox.isBoolean(arguments[2]) ? arguments[2] + : Ox.isBoolean(arguments[3]) ? arguments[3] + : false; + newline = Ox.isString(arguments[2]) ? arguments[2] + : Ox.isString(arguments[3]) ? arguments[3] + : '\n'; + words = string.split(' '); + if (balanced) { + // balanced lines: test if same number of lines // can be achieved with a shorter line length - lines = Ox.wordwrap(str, len, sep, false).split(sep); + lines = Ox.wordwrap(string, length, newline).split(newline); if (lines.length > 1) { // test shorter line, unless // that means cutting a word - var max = Ox.max(words.map(function(word) { + max = Ox.max(words.map(function(word) { return word.length; })); - while (len > max) { - len--; - if (Ox.wordwrap(str, len, sep, false).split(sep).length > lines.length) { - len++; + while (length > max) { + if (Ox.wordwrap( + string, --length, newline + ).split(newline).length > lines.length) { + length++; break; } } @@ -607,29 +612,25 @@ Ox.wordwrap = function(str, len, sep, bal, spa) { } lines = ['']; words.forEach(function(word) { - var chr; - if ((lines[lines.length - 1] + word + ' ').length <= len + 1) { + var index; + if ((lines[lines.length - 1] + word).length <= length) { // word fits in current line lines[lines.length - 1] += word + ' '; } else { - if (word.length <= len) { + if (word.length <= length) { // word fits in next line lines.push(word + ' '); } else { // word is longer than line - chr = len - lines[lines.length - 1].length; - lines[lines.length - 1] += word.slice(0, chr); - Ox.loop(chr, word.length, len, function(pos) { - lines.push(word.substr(pos, len)); - }); + index = length - lines[lines.length - 1].length; + lines[lines.length - 1] += word.slice(0, index); + while (index < word.length) { + lines.push(word.substr(index, length)); + index += length; + } lines[lines.length - 1] += ' '; } } }); - if (!spa) { - lines = lines.map(function(line) { - return line.trim(); - }); - } - return lines.join(sep).trim(); + return lines.join(newline).trim(); };