change the signature of Ox.pad to match Ox.truncate

This commit is contained in:
rolux 2012-06-04 11:08:38 +02:00
parent 24bbf95bd5
commit a5c6747b57
10 changed files with 62 additions and 29 deletions

View file

@ -352,7 +352,7 @@ Ox.load.Image = function(options, callback) {
str = Ox[deflate ? 'encodeDeflate' : 'encodeUTF8'](str);
len = str.length;
// Prefix the string with its length, as a four-byte value
str = Ox.pad(Ox.encodeBase256(len), 4, '\u0000') + str;
str = Ox.pad(Ox.encodeBase256(len), 'left', 4, '\u0000') + str;
str.length > cap && error('encode');
while (str.length < cap) {
str += str.substr(4, len);

View file

@ -140,7 +140,7 @@ Ox.DateInput = function(options, self) {
autocomplete: Ox.range(1, days + 1).map(function(i) {
return self.options.format == 'short' ? Ox.pad(i, 2) : i.toString();
}),
value: self.options.format == 'short' ? Ox.pad(day, 2) : day.toString()
value: self.options.format == 'short' ? Ox.pad(parseInt(day), 2) : day.toString()
});
self.options.value = join();
}

View file

@ -74,7 +74,7 @@ Ox.Chart = function(options, self) {
});
}
self.sort[key] = key.replace(/(\d+)/g, function(number) {
return Ox.pad(number, 16);
return Ox.pad(parseInt(number), 16);
});
});
self.max = Ox.max(self.totals);
@ -83,7 +83,7 @@ Ox.Chart = function(options, self) {
if (self.subData) {
Ox.forEach(self.subData, function(subValue, subKey) {
self.sort[subKey] = subKey.replace(/(\d+)/g, function(number) {
return Ox.pad(number, 16);
return Ox.pad(parseInt(number), 16);
});
});
self.subKeys = Object.keys(self.subData).sort(function(a, b) {

View file

@ -64,7 +64,7 @@ Ox.MapImage = function(options, self) {
function formatColor(color) {
return color.map(function(c) {
return Ox.pad(c.toString(16), 2);
return Ox.pad(c.toString(16), 'left', 2, '0');
}).join('')
}

View file

@ -570,7 +570,7 @@ Ox.range = function() {
sort[value] = mappedValue.toLowerCase()
.replace(/^\W+/, '')
.replace(/\d+/g, function(match) {
return Ox.pad(match, length);
return Ox.pad(parseInt(match), length);
});
Ox.forEach(['a', 'an', 'the'], function(article) {
var length;

View file

@ -108,7 +108,7 @@ Ox.toHex <f> Format RGB array as hex value
@*/
Ox.toHex = function(rgb) {
return rgb.map(function(value) {
return Ox.pad(value.toString(16).toUpperCase(), 2);
return Ox.pad(value.toString(16).toUpperCase(), 'left', 2, '0');
}).join('');
};

View file

@ -553,7 +553,7 @@ Ox.formatDuration = function(seconds/*, decimals, format*/) {
return Ox.filter(Ox.map(values, function(value, index) {
var ret;
if (format == 'none') {
ret = Ox.pad(value, pad[index]);
ret = Ox.pad(value, 'left', pad[index], '0');
} else if (Ox.isNumber(value) ? value : parseFloat(value)) {
ret = value + (format == 'long' ? ' ' : '') + string[index]
+ (format == 'long' && value != 1 ? 's' : '');

View file

@ -189,7 +189,9 @@
if (code < 128) {
char = char in htmlEntities ? htmlEntities[char] : char;
} else if (encodeAll) {
char = '&#x' + Ox.pad(code.toString(16).toUpperCase(), 4) + ';';
char = '&#x'
+ Ox.pad(code.toString(16).toUpperCase(), 'left', 4, '0')
+ ';';
}
return char;
});

View file

@ -60,10 +60,10 @@ Ox.oshash = function(file, callback) {
function hex(h) {
return (
Ox.pad(h[0].toString(16), 4)
+ Ox.pad(h[1].toString(16), 4)
+ Ox.pad(h[2].toString(16), 4)
+ Ox.pad(h[3].toString(16), 4)
Ox.pad(h[0].toString(16), 'left', 4, '0')
+ Ox.pad(h[1].toString(16), 'left', 4, '0')
+ Ox.pad(h[2].toString(16), 'left', 4, '0')
+ Ox.pad(h[3].toString(16), 'left', 4, '0')
).toLowerCase();
}

View file

@ -68,24 +68,51 @@ Ox.isValidEmail = function(string) {
/*@
Ox.pad <f> Pad a string to a given length
(string[, position], length[, padding]) -> <s> Padded string
string <s> String
position <s|'right'> Position ('left' or 'right')
When passing a number as `string`, the default position is 'left'.
length <n> Length
padding <s|' '> Padding
When passing a number as `string`, and leaving out or passing 'left' as
`position`, the default padding is '0'.
> Ox.pad('foo', 6)
'foo '
> Ox.pad('foo', 'left', 6)
' foo'
> Ox.pad('foo', 6, '.')
'foo...'
> Ox.pad('foo', 'left', 6, '.')
'...foo'
> Ox.pad(1, 2)
"01"
> Ox.pad("abc", -6, ".")
"abc..."
> Ox.pad("foobar", -3, ".")
"foo"
> Ox.pad("abc", -6, "123456")
"abc123"
> Ox.pad("abc", 6, "123456")
"456abc"
'01'
> Ox.pad(1, 2, ' ')
' 1'
> Ox.pad(1, 'right', 2)
'1 '
> Ox.pad(1, 'right', 2, '_')
'1_'
> Ox.pad('foo', 6, '123456')
'foo123'
> Ox.pad('foo', 'left', 6, '123456')
'456foo'
> Ox.pad('foobar', 3)
'foo'
> Ox.pad('foobar', 'left', 3)
'bar'
@*/
Ox.pad = function(string, length, padding) {
// fixme: slighly obscure signature
Ox.pad = function(string, position, length, padding) {
// fixme: weird for negative numbers
var pos = length / (length = Math.abs(length));
string = string.toString().slice(0, length);
padding = Ox.repeat(padding || '0', length - string.length);
return pos == 1
var hasPosition = Ox.isString(arguments[1]),
isNumber = Ox.isNumber(arguments[0]),
last = Ox.last(arguments);
position = hasPosition ? arguments[1] : isNumber ? 'left' : 'right';
length = hasPosition ? arguments[2] : arguments[1];
padding = Ox.isString(last) ? last
: isNumber && position == 'left' ? '0' : ' ';
string = string.toString();
padding = Ox.repeat(padding, length - string.length);
return position == 'left'
? (padding + string).slice(-length)
: (string + padding).slice(0, length);
};
@ -129,7 +156,7 @@ Ox.parsePath = function(string) {
filename: matches[2] || '',
extension: matches[3] ? matches[3].slice(1) : ''
};
}
};
/*@
Ox.parseSRT <f> Parses an srt subtitle file
@ -475,6 +502,10 @@ Ox.toUnderscores = function(string) {
/*@
Ox.truncate <f> Truncate a string to a given length
(string[, position], length[, padding]) -> Truncated string
string <s> String
position <s|'right'> Position ('left', 'center' or 'right')
length <n> Length
padding <s|'…'> Padding
> Ox.truncate('anticonstitutionellement', 16)
'anticonstitutio…'
> Ox.truncate('anticonstitutionellement', 'left', 16)