Skip to content

Commit 4dfa207

Browse files
jnarebgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e2895de commit 4dfa207

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

gitweb/static/js/lib/common-lib.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,14 @@
2222
*
2323
* @param {Number|String} input: number to pad
2424
* @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'
2626
* @returns {String} INPUT prefixed with STR x (WIDTH - INPUT.length)
2727
*/
2828
function padLeftStr(input, width, str) {
2929
var prefix = '';
30+
if (typeof str === 'undefined') {
31+
ch = '\u00A0'; // using '&nbsp;' doesn't work in all browsers
32+
}
3033

3134
width -= input.toString().length;
3235
while (width > 0) {
@@ -38,16 +41,21 @@ function padLeftStr(input, width, str) {
3841

3942
/**
4043
* 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'))
4246
*
4347
* @param {String} input: input value converted to string.
4448
* @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'.
4650
*
4751
* @returns {String} Modified string, at least SIZE length.
4852
*/
4953
function padLeft(input, width, ch) {
5054
var s = input + "";
55+
if (typeof ch === 'undefined') {
56+
ch = '0';
57+
}
58+
5159
while (s.length < width) {
5260
s = ch + s;
5361
}

0 commit comments

Comments
 (0)