From a5c6747b57b6af068d20471ba5188d88662e9512 Mon Sep 17 00:00:00 2001 From: rolux Date: Mon, 4 Jun 2012 11:08:38 +0200 Subject: [PATCH] change the signature of Ox.pad to match Ox.truncate --- source/Ox.Image/Ox.Image.js | 2 +- source/Ox.UI/js/Form/DateInput.js | 2 +- source/Ox.UI/js/List/Chart.js | 4 +- source/Ox.UI/js/Map/MapImage.js | 2 +- source/Ox/js/Array.js | 2 +- source/Ox/js/Color.js | 2 +- source/Ox/js/Format.js | 2 +- source/Ox/js/HTML.js | 4 +- source/Ox/js/Hash.js | 8 ++-- source/Ox/js/String.js | 63 +++++++++++++++++++++++-------- 10 files changed, 62 insertions(+), 29 deletions(-) diff --git a/source/Ox.Image/Ox.Image.js b/source/Ox.Image/Ox.Image.js index 48312af1..8e1ff433 100644 --- a/source/Ox.Image/Ox.Image.js +++ b/source/Ox.Image/Ox.Image.js @@ -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); diff --git a/source/Ox.UI/js/Form/DateInput.js b/source/Ox.UI/js/Form/DateInput.js index 29199cc3..4eadb708 100644 --- a/source/Ox.UI/js/Form/DateInput.js +++ b/source/Ox.UI/js/Form/DateInput.js @@ -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(); } diff --git a/source/Ox.UI/js/List/Chart.js b/source/Ox.UI/js/List/Chart.js index e246ccfb..f37581b5 100644 --- a/source/Ox.UI/js/List/Chart.js +++ b/source/Ox.UI/js/List/Chart.js @@ -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) { diff --git a/source/Ox.UI/js/Map/MapImage.js b/source/Ox.UI/js/Map/MapImage.js index b37d48a8..7cd5ba25 100644 --- a/source/Ox.UI/js/Map/MapImage.js +++ b/source/Ox.UI/js/Map/MapImage.js @@ -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('') } diff --git a/source/Ox/js/Array.js b/source/Ox/js/Array.js index fc18b42b..5f9486d8 100644 --- a/source/Ox/js/Array.js +++ b/source/Ox/js/Array.js @@ -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; diff --git a/source/Ox/js/Color.js b/source/Ox/js/Color.js index de0bb5e4..cb8c5692 100644 --- a/source/Ox/js/Color.js +++ b/source/Ox/js/Color.js @@ -108,7 +108,7 @@ Ox.toHex 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(''); }; diff --git a/source/Ox/js/Format.js b/source/Ox/js/Format.js index 7ff639a5..a8d09630 100644 --- a/source/Ox/js/Format.js +++ b/source/Ox/js/Format.js @@ -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' : ''); diff --git a/source/Ox/js/HTML.js b/source/Ox/js/HTML.js index 90072667..0b3faab7 100644 --- a/source/Ox/js/HTML.js +++ b/source/Ox/js/HTML.js @@ -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; }); diff --git a/source/Ox/js/Hash.js b/source/Ox/js/Hash.js index 2f02e5f0..6407ffcb 100644 --- a/source/Ox/js/Hash.js +++ b/source/Ox/js/Hash.js @@ -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(); } diff --git a/source/Ox/js/String.js b/source/Ox/js/String.js index fd3f479e..9b2f5ef9 100644 --- a/source/Ox/js/String.js +++ b/source/Ox/js/String.js @@ -68,24 +68,51 @@ Ox.isValidEmail = function(string) { /*@ Ox.pad Pad a string to a given length + (string[, position], length[, padding]) -> Padded string + string String + position Position ('left' or 'right') + When passing a number as `string`, the default position is 'left'. + length Length + padding 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 Parses an srt subtitle file @@ -475,6 +502,10 @@ Ox.toUnderscores = function(string) { /*@ Ox.truncate Truncate a string to a given length (string[, position], length[, padding]) -> Truncated string + string String + position Position ('left', 'center' or 'right') + length Length + padding Padding > Ox.truncate('anticonstitutionellement', 16) 'anticonstitutio…' > Ox.truncate('anticonstitutionellement', 'left', 16)