]> Lady’s Gitweb - Gitweb/commitdiff
gitweb.js: Provide default values for padding in padLeftStr and padLeft
authorJakub Narebski <redacted>
Thu, 28 Apr 2011 19:04:03 +0000 (21:04 +0200)
committerLady <redacted>
Mon, 6 Apr 2026 04:51:31 +0000 (00:51 -0400)
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 <redacted>
Signed-off-by: Junio C Hamano <redacted>
static/js/lib/common-lib.js

index 8cd7804abdc358d2427f556c620e90269bf485b6e47a11f862fd5cd0429d321e..dc0a1c305e13b1569c2284472593e810d3d4f8e7e8e4f2f1fe235d3b53cb0660 100644 (file)
  *
  * @param {Number|String} input: number to pad
  * @param {Number} width: visible width of output
  *
  * @param {Number|String} input: number to pad
  * @param {Number} width: visible width of output
- * @param {String} str: string to prefix to string, e.g. '\u00A0'
+ * @param {String} str: string to prefix to string, defaults to '\u00A0'
  * @returns {String} INPUT prefixed with STR x (WIDTH - INPUT.length)
  */
 function padLeftStr(input, width, str) {
        var prefix = '';
  * @returns {String} INPUT prefixed with STR x (WIDTH - INPUT.length)
  */
 function padLeftStr(input, width, str) {
        var prefix = '';
+       if (typeof str === 'undefined') {
+               ch = '\u00A0'; // using '&nbsp;' doesn't work in all browsers
+       }
 
        width -= input.toString().length;
        while (width > 0) {
 
        width -= input.toString().length;
        while (width > 0) {
@@ -38,16 +41,21 @@ function padLeftStr(input, width, str) {
 
 /**
  * Pad INPUT on the left to WIDTH, using given padding character CH,
 
 /**
  * Pad INPUT on the left to WIDTH, using given padding character CH,
- * for example padLeft('a', 3, '_') is '__a'.
+ * for example padLeft('a', 3, '_') is '__a'
+ *             padLeft(4, 2) is '04' (same as padLeft(4, 2, '0'))
  *
  * @param {String} input: input value converted to string.
  * @param {Number} width: desired length of output.
  *
  * @param {String} input: input value converted to string.
  * @param {Number} width: desired length of output.
- * @param {String} ch: single character to prefix to string.
+ * @param {String} ch: single character to prefix to string, defaults to '0'.
  *
  * @returns {String} Modified string, at least SIZE length.
  */
 function padLeft(input, width, ch) {
        var s = input + "";
  *
  * @returns {String} Modified string, at least SIZE length.
  */
 function padLeft(input, width, ch) {
        var s = input + "";
+       if (typeof ch === 'undefined') {
+               ch = '0';
+       }
+
        while (s.length < width) {
                s = ch + s;
        }
        while (s.length < width) {
                s = ch + s;
        }
This page took 0.211349 seconds and 4 git commands to generate.