Subversion Repositories HomeAutomation

Rev

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

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