cleanup Ox.wordwrap; remove Ox.basename

This commit is contained in:
rolux 2012-06-04 13:49:10 +02:00
parent cce58246fd
commit 6116f4b7f1

View file

@ -1,16 +1,5 @@
'use strict'; '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 <f> Alias for String.fromCharCode Ox.char <f> Alias for String.fromCharCode
@*/ @*/
@ -570,36 +559,52 @@ Ox.words = function(string) {
/*@ /*@
Ox.wordwrap <f> Wrap a string at word boundaries Ox.wordwrap <f> Wrap a string at word boundaries
> Ox.wordwrap("Anticonstitutionellement, Paris s'eveille", 25, '<br/>') (string, length) -> <s> Wrapped string
"Anticonstitutionellement, <br/>Paris s'eveille" (string, length, newline) -> <s> Wrapped string
> Ox.wordwrap("Anticonstitutionellement, Paris s'eveille", 16, '<br/>') (string, length, balanced) -> <s> Wrapped string
"Anticonstitution<br/>ellement, Paris <br/>s'eveille" (string, length, balanced, newline) -> <s> Wrapped string
> Ox.wordwrap('These are short words', 16, '<br/>', true) (string, length, newline, balanced) -> <s> Wrapped string
'These are <br/>short words' string <s> String
length <n|80> Line length
balanced <b|false> If true, lines will have similar length
newline <s|'\n'> New line character or string
> Ox.wordwrap('Anticonstitutionellement, Paris s\'eveille', 25)
'Anticonstitutionellement, \nParis s\'eveille'
> Ox.wordwrap('Anticonstitutionellement, Paris s\'eveille', 25, '<br>')
'Anticonstitutionellement, <br>Paris s\'eveille'
> Ox.wordwrap('Anticonstitutionellement, Paris s\'eveille', 16, '<br>')
'Anticonstitution<br>ellement, Paris <br>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) { Ox.wordwrap = function(string, length) {
// fixme: bad API, sep/bal/spa should be in options object var balanced, lines, max, newline, words;
var str = str === null ? '' : str.toString(), string = String(string);
len = len || 80, length = length || 80;
sep = sep || '<br/>', balanced = Ox.isBoolean(arguments[2]) ? arguments[2]
bal = bal || false, : Ox.isBoolean(arguments[3]) ? arguments[3]
spa = Ox.isUndefined(spa) ? true : spa, : false;
words = str.split(' '), newline = Ox.isString(arguments[2]) ? arguments[2]
lines; : Ox.isString(arguments[3]) ? arguments[3]
if (bal) { : '\n';
// balance lines: test if same number of lines words = string.split(' ');
if (balanced) {
// balanced lines: test if same number of lines
// can be achieved with a shorter line length // 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) { if (lines.length > 1) {
// test shorter line, unless // test shorter line, unless
// that means cutting a word // that means cutting a word
var max = Ox.max(words.map(function(word) { max = Ox.max(words.map(function(word) {
return word.length; return word.length;
})); }));
while (len > max) { while (length > max) {
len--; if (Ox.wordwrap(
if (Ox.wordwrap(str, len, sep, false).split(sep).length > lines.length) { string, --length, newline
len++; ).split(newline).length > lines.length) {
length++;
break; break;
} }
} }
@ -607,29 +612,25 @@ Ox.wordwrap = function(str, len, sep, bal, spa) {
} }
lines = ['']; lines = [''];
words.forEach(function(word) { words.forEach(function(word) {
var chr; var index;
if ((lines[lines.length - 1] + word + ' ').length <= len + 1) { if ((lines[lines.length - 1] + word).length <= length) {
// word fits in current line // word fits in current line
lines[lines.length - 1] += word + ' '; lines[lines.length - 1] += word + ' ';
} else { } else {
if (word.length <= len) { if (word.length <= length) {
// word fits in next line // word fits in next line
lines.push(word + ' '); lines.push(word + ' ');
} else { } else {
// word is longer than line // word is longer than line
chr = len - lines[lines.length - 1].length; index = length - lines[lines.length - 1].length;
lines[lines.length - 1] += word.slice(0, chr); lines[lines.length - 1] += word.slice(0, index);
Ox.loop(chr, word.length, len, function(pos) { while (index < word.length) {
lines.push(word.substr(pos, len)); lines.push(word.substr(index, length));
}); index += length;
}
lines[lines.length - 1] += ' '; lines[lines.length - 1] += ' ';
} }
} }
}); });
if (!spa) { return lines.join(newline).trim();
lines = lines.map(function(line) {
return line.trim();
});
}
return lines.join(sep).trim();
}; };