cleanup Ox.wordwrap; remove Ox.basename
This commit is contained in:
parent
cce58246fd
commit
6116f4b7f1
1 changed files with 49 additions and 48 deletions
|
@ -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 <f> Alias for String.fromCharCode
|
||||
@*/
|
||||
|
@ -570,36 +559,52 @@ Ox.words = function(string) {
|
|||
|
||||
/*@
|
||||
Ox.wordwrap <f> Wrap a string at word boundaries
|
||||
> 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, '<br/>', true)
|
||||
'These are <br/>short words'
|
||||
(string, length) -> <s> Wrapped string
|
||||
(string, length, newline) -> <s> Wrapped string
|
||||
(string, length, balanced) -> <s> Wrapped string
|
||||
(string, length, balanced, newline) -> <s> Wrapped string
|
||||
(string, length, newline, balanced) -> <s> Wrapped string
|
||||
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) {
|
||||
// fixme: bad API, sep/bal/spa should be in options object
|
||||
var str = str === null ? '' : str.toString(),
|
||||
len = len || 80,
|
||||
sep = sep || '<br/>',
|
||||
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();
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue