Subversion Repositories HomeAutomation

Rev

Rev 984 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. /*
  2. String.pad(length: Integer, [substring: String = " "], [type: Integer = 0]): String
  3.         Returns the string with a substring padded on the left, right or both sides.
  4.     length
  5.         amount of characters that the string must have
  6.     substring
  7.         string that will be concatenated
  8.     type
  9.         specifies the side where the concatenation will happen, where: 0 = left, 1 = right and 2 = both sides
  10. */
  11. String.prototype.pad = function(l, s, t)
  12. {
  13.     return s || (s = " "), (l -= this.length) > 0 ? (s = new Array(Math.ceil(l / s.length)
  14.         + 1).join(s)).substr(0, t = !t ? l : t == 1 ? 0 : Math.ceil(l / 2))
  15.         + this + s.substr(0, l - t) : this;
  16. }
  17.  
  18. function extend(descendant, parent)
  19. {
  20.     var sConstructor = parent.toString();
  21.     var aMatch = sConstructor.match(/\s*function (.*)\(/);
  22.    
  23.     if (aMatch != null)
  24.     {
  25.         descendant.prototype[aMatch[1]] = parent;
  26.     }
  27.    
  28.     for (var m in parent.prototype)
  29.     {
  30.         descendant.prototype[m] = parent.prototype[m];
  31.     }
  32. }
  33.  
  34. function time(d)
  35. {
  36.     if (!d)
  37.         d = new Date();
  38.        
  39.     return d.getTime()/1000;
  40. }
  41.  
  42. function formatDate(d)
  43. {
  44.     if (!d)
  45.         d = new Date();
  46.        
  47.     return d.getFullYear() + "-" + (d.getMonth()+1).toString().pad(2, "0", 0) + "-" + d.getDate().toString().pad(2, "0", 0);
  48. }
  49.  
  50. function formatTime(d)
  51. {
  52.     if (!d)
  53.         d = new Date();
  54.        
  55.     return d.getHours().toString().pad(2, "0", 0) + "." + d.getMinutes().toString().pad(2, "0", 0) + "." + d.getSeconds().toString().pad(2, "0", 0);
  56. }
  57.  
  58. function formatDateTime(d)
  59. {
  60.     if (!d)
  61.         d = new Date();
  62.        
  63.     return formatDate(d) + " " + formatTime(d);
  64. }
  65.  
  66. function ltrim(str, charlist)
  67. {
  68.     charlist = !charlist ? ' \s\xA0' : charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '\$1');
  69.     var re = new RegExp('^[' + charlist + ']+', 'g');
  70.     return str.replace(re, '');
  71. }
  72.  
  73. function rtrim(str, charlist)
  74. {
  75.     charlist = !charlist ? ' \s\xA0' : charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '\$1');
  76.     var re = new RegExp('[' + charlist + ']+$', 'g');
  77.     return str.replace(re, '');
  78. }
  79.  
  80. function trim(str, charlist)
  81. {
  82.     str = ltrim(str, charlist);
  83.     return rtrim(str, charlist);
  84. }
  85.