git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 03/11] gitweb.js: Provide default values for padding in padLeftStr and padLeft

From
Jakub Narebski <jnareb@gmail.com>
Date
Apr 15, 2011, 14:43 UTC
Message-ID
<1302878645-458-4-git-send-email-jnareb@gmail.com>
In-Reply-To
<1302878645-458-1-git-send-email-jnareb@gmail.com>

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>
---
This patch is unchanged from v1 version.

This patch is not strictly necessary, but it makes those functions easier to use, I guess. Removing this patch would require though some trivially resolvable changes to subsequent commits in this series.

 gitweb/static/js/lib/common-lib.js |   14 +++++++++++---
 1 files changed, 11 insertions(+), 3 deletions(-)
diff --git a/gitweb/static/js/lib/common-lib.js b/gitweb/static/js/lib/common-lib.js
index 6a6d200..c45454e 100644
--- a/gitweb/static/js/lib/common-lib.js
+++ b/gitweb/static/js/lib/common-lib.js
@@ -22,11 +22,14 @@
  *
  * @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 = '';
+	if (typeof str === 'undefined') {
+		ch = '\u00A0'; // using '&nbsp;' doesn't work in all browsers
+	}
 
 	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,
- * 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} 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 + "";
+	if (typeof ch === 'undefined') {
+		ch = '0';
+	}
+
 	while (s.length < width) {
 		s = ch + s;
 	}
-- 
1.7.3
Previous: Jakub NarebskiNext: Jakub Narebski
Message 4 of 21 in “[PATCHv2 00/11] gitweb: Change timezone in dates using JavaScript”
  1. Jakub NarebskiApr 15, 2011
  2. 01/11 gitweb: Split JavaScript for maintability, combining on buildJakub Narebski, Apr 15, 2011
  3. 02/11 gitweb.js: Update and improve comments in JavaScript filesJakub Narebski, Apr 15, 2011
  4. 03/11 gitweb.js: Provide default values for padding in padLeftStr and padLeftJakub Narebski, Apr 15, 2011
  5. 04/11 gitweb.js: Extract and improve datetime handlingJakub Narebski, Apr 15, 2011
  6. 05/11 gitweb.js: Introduce gitweb/static/js/lib/cookies.jsJakub Narebski, Apr 15, 2011
  7. 06/11 gitweb.js: Provide getElementsByClassName method (if it not exists)Jakub Narebski, Apr 15, 2011
  8. 07/11 gitweb: Refactor generating of long dates into format_timestamp_htmlJakub Narebski, Apr 15, 2011
  9. 08/11 gitweb: Unify the way long timestamp is displayedJakub Narebski, Apr 15, 2011
  10. 09/11 gitweb: JavaScript ability to adjust time based on timezoneJakub Narebski, Apr 15, 2011
  11. 10/11 gitweb.js: Add UI for selecting common timezone to display datesJakub Narebski, Apr 15, 2011
  12. Jakub NarebskiApr 15, 2011
  13. Jakub NarebskiApr 15, 2011
  14. 10/11 gitweb.js: Add UI for selecting common timezone to display datesJakub Narebski, Apr 15, 2011
  15. 11/11 gitweb: Make JavaScript ability to adjust timezones configurableJakub Narebski, Apr 15, 2011
  16. Junio C HamanoApr 19, 2011
  17. Jakub NarebskiApr 19, 2011
  18. Jakub NarebskiApr 19, 2011
  19. Junio C HamanoApr 19, 2011
  20. 12/11 Remove gitweb/gitweb.cgi and other legacy targets from main MakefileJakub Narebski, Apr 19, 2011
  21. 01/11 git-instaweb: Simplify build dependency on gitwebJakub Narebski, Apr 19, 2011

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.