Sprintf Like Utilities For JScript

 
 
This code can be used to produce formated strings. Mostly useful for WSH and text areas displayed with fixed fonts.
 
 
  1. // sprintf realization

  2. // Samples:

  3. // WScript.Echo("%15l%10r%10r%10r%10r".format(new Array("NAME","TYPE","LENGTH","PRECISION","SCALE")) );

  4. // or using the sprintf

  5. // WScript.Echo(sprintf("%15l%10r%10r%10r%10r","NAME","TYPE","LENGTH","PRECISION","SCALE");

  6. // Format description:

  7. //  Use format string and attached format function.

  8. //  Pass to it array of printed arguments

  9. //  or use the sprintf helper function.

  10. //  Format specifiers are:

  11. //  %[width]align

  12. //  width   - optional field width

  13. //  align   - l or r - specifies text alignment

  14.  

  15. String.prototype.formatLength = function (n,left) {

  16.     var str = "";

  17.     var i;

  18.     if (this.length < n) {

  19.         if (left) {

  20.             str += this;

  21.             for (i = 0; i < (n - this.length); i ++) str += " ";           

  22.         } else {

  23.             for (i = 0; i < (n - this.length); i ++) str += " ";

  24.             str += this;                       

  25.         }

  26.     } else {

  27.         if (left) {

  28.             str = this.slice(this.length-n-1,-1);   

  29.         } else {

  30.             str = this.slice(0,n-1);

  31.         }

  32.     }

  33.     return str;

  34. }

  35. String.prototype.format = function(arrArgs) {

  36.     var fmt = this;

  37.     var re = /(%d*[lr]{1})/gi;

  38.     var reNum = /d+/gi;

  39.     var fmtParts = fmt.split(re);

  40.     var fmtArgs = fmt.match(re);

  41.     var nP = 0, nA = 0,nAFirst = 0;

  42.     var str = "";

  43.     var fmtArgWidth;

  44.     var Width;

  45.     var strTemp;

  46.    

  47.     if (fmtParts == null) fmtParts = new Array();

  48.     if (fmtArgs == null) fmtArgs = new Array();   

  49.    

  50.     if (fmt.charAt(0) == "%") nAFirst = 1;

  51.     while (nP < fmtArgs.length || nA < fmtParts.length) {

  52.         if (nA < (nP + nAFirst)) {

  53.             if (nA < fmtArgs.length) {

  54.                  if (nA < arrArgs.length) {

  55.                     fmtArgWidth = fmtArgs[nA].match(reNum);

  56.                     if (fmtArgWidth != null) Width = new Number(fmtArgWidth[0]);

  57.                     else Width = 0;

  58.                     strTemp = new String(arrArgs[nA]);

  59.                     if (fmtArgs[nA].charAt(fmtArgs[nA].length - 1) == "l") {

  60.                         str += strTemp.formatLength(Width,true);

  61.                     } else {

  62.                         str += strTemp.formatLength(Width,false);                   

  63.                     }

  64.                  }

  65.             }

  66.             nA ++;

  67.         } else {

  68.             if (nP < fmtParts.length) {

  69.                 str += fmtParts[nP];

  70.             }

  71.             nP ++;

  72.         }

  73.     }

  74.     return str;

  75. }

  76.  

  77. // Helper functions - easy to use but a bit slower

  78. function sprintf(str) {

  79.         var arr = new Array();

  80.         for (i = 1; i < sprintf.arguments.length; i++) {

  81.                 arr[arr.length] = sprintf.arguments[i];

  82.         }

  83.         return str.format(arr);

  84. }

  85. // This one is for WSH only - remove it if gonna use the script in a different environment

  86. function printf(str) {

  87.         var arr = new Array();

  88.         for (i = 1; i < sprintf.arguments.length; i++) {

  89.                 arr[arr.length] = sprintf.arguments[i];

  90.         }

  91.     WScript.Echo(str.format(arr));

  92. }

  93.  
 
Copy to Clipboard      Download code as Text Format
 
  Date entered : 17th Feb 2001
  Rating : No Rating
  Accessed  :  1129
  Submitted by :  micle
 
   Add Comment  Printer friendly
   View All Comments  Email to a friend
   Add to my Favourites Download PDF version
   Rating  
 
                 Click each image to add
this page to each site.
 
 
 
Comments Available

    No comment available at the moment.Be the first one to make a comment

 
Related JavaScript Source Codes
Database error: Invalid SQL: select FLOOR( AVG(b.rating)) as avg_rating,a.sourcecode_id sourcecode_id ,title,description,author,category_id,skill_level,reference,reference_mail,reference_url,a.user_id user_id,date_entered,source,accessed,dev_id,file_url,total_comments,instruction,registered,authorised,fld_deletemark,fld_code_version,fld_searchkeyword,fld_code_status,category_id from tbl_sourcecode a LEFT JOIN tbl_sourcecodecomment b ON a.sourcecode_id = b.sourcecode_id LEFT JOIN tbl_codecategory c ON a.category_id = c.fld_categoryID where a.fld_deletemark = 'N' AND a.fld_enable_disable= 'Y' AND a.authorised='Y' AND c.fld_groupIDFK='6' AND a.sourcecode_id!='247' GROUP BY a.sourcecode_id order by a.sourcecode_id DESC limit 0, 5
MySQL Error: 1030 (Got error 122 from storage engine)
Session halted.
Warning: Unknown(): write failed: Disk quota exceeded (122) in Unknown on line 0

Warning: Unknown(): Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/tmp) in Unknown on line 0