gitweb.js: Provide default values for padding in padLeftStr and padLeft

This means that one can use padLeft(4, 2) and it would be equivalent to runing padLeft(4, 2, '0'), and it would return '04' i.e. '4' padded with '0' to width 2, to be used e.g. in formatting date and time. This should make those functions easier to use. Current code doesn't yet make use of this feature. Signed-off-by: Jakub Narebski <jnareb@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jakub Narebski committed Apr 28, 2011 at 21:04 UTC 4dfa207eec44e0d8869c4978cf3d9861aaf40091
1 file changed +11 -3
gitweb/static/js/lib/common-lib.js
+11 -3
@@ -22,11 +22,14 @@
22 *
23 * @param {Number|String} input: number to pad
24 * @param {Number} width: visible width of output
25 - * @param {String} str: string to prefix to string, e.g. '\u00A0'
25 + * @param {String} str: string to prefix to string, defaults to '\u00A0'
26 * @returns {String} INPUT prefixed with STR x (WIDTH - INPUT.length)
27 */
28 function padLeftStr(input, width, str) {
29 var prefix = '';
30 + if (typeof str === 'undefined') {
31 + ch = '\u00A0'; // using '&nbsp;' doesn't work in all browsers
32 + }
33
34 width -= input.toString().length;
35 while (width > 0) {
@@ -38,16 +41,21 @@ function padLeftStr(input, width, str) {
41
42 /**
43 * Pad INPUT on the left to WIDTH, using given padding character CH,
41 - * for example padLeft('a', 3, '_') is '__a'.
44 + * for example padLeft('a', 3, '_') is '__a'
45 + * padLeft(4, 2) is '04' (same as padLeft(4, 2, '0'))
46 *
47 * @param {String} input: input value converted to string.
48 * @param {Number} width: desired length of output.
45 - * @param {String} ch: single character to prefix to string.
49 + * @param {String} ch: single character to prefix to string, defaults to '0'.
50 *
51 * @returns {String} Modified string, at least SIZE length.
52 */
53 function padLeft(input, width, ch) {
54 var s = input + "";
55 + if (typeof ch === 'undefined') {
56 + ch = '0';
57 + }
58 +
59 while (s.length < width) {
60 s = ch + s;
61 }