Subversion Repositories HomeAutomation

Rev

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

  1.  
  2. /*
  3. String.pad(length: Integer, [substring: String = " "], [type: Integer = 0]): String
  4.       Returns the string with a substring padded on the left, right or both sides.
  5.     length
  6.         amount of characters that the string must have
  7.     substring
  8.         string that will be concatenated
  9.     type
  10.         specifies the side where the concatenation will happen, where: 0 = left, 1 = right and 2 = both sides
  11. */
  12. String.prototype.pad = function(l, s, t)
  13. {
  14.   return  s || (s = " "), (l -= this.length) > 0 ? (s = new Array(Math.ceil(l / s.length)
  15.     + 1).join(s)).substr(0, t = !t ? l : t == 1 ? 0 : Math.ceil(l / 2))
  16.     + this + s.substr(0, l - t) : this;
  17. }
  18.  
  19. String.prototype.ltrim = function(charlist)
  20. {
  21.   charlist = !charlist ? ' \s\xA0' : charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '\$1');
  22.   var re = new RegExp('^[' + charlist + ']+', 'g');
  23.   return this.replace(re, '');
  24. }
  25.  
  26. String.prototype.rtrim = function(charlist)
  27. {
  28.   charlist = !charlist ? ' \s\xA0' : charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '\$1');
  29.   var re = new RegExp('[' + charlist + ']+$', 'g');
  30.   return this.replace(re, '');
  31. }
  32.  
  33. String.prototype.trim = function(charlist)
  34. {
  35.   var str = this.ltrim(charlist);
  36.   return str.rtrim(charlist);
  37. }
  38.  
  39. String.prototype.html_entity_decode = function()
  40. {
  41.   var str = this.replace(/Å/g, "Ä");
  42.   str = str.replace(/Ä/g, "Ä");
  43.  
  44.   str = str.replace(/Ä/g, "Å");
  45.   str = str.replace(/Å/g, "Å");
  46.  
  47.   str = str.replace(/Ö/g, "Ö");
  48.   str = str.replace(/Ö/g, "Ö");
  49.  
  50.   str = str.replace(/ä/g, "ä");
  51.   str = str.replace(/ä/g, "ä");
  52.  
  53.   str = str.replace(/å/g, "å");
  54.   str = str.replace(/å/g, "å");
  55.  
  56.   str = str.replace(/ö/g, "ö");
  57.   str = str.replace(/ö/g, "ö");
  58.  
  59.   //FIXME: Add more characters
  60.  
  61.   return str;
  62. }
  63.  
  64. Date.prototype.getTimestamp = function()
  65. {
  66.   return this.getTime()/1000;
  67. }
  68.  
  69. Date.prototype.getDateFormated = function()
  70. {
  71.   return this.getFullYear() + "-" + (this.getMonth()+1).toString().pad(2, "0", 0) + "-" + this.getDate().toString().pad(2, "0", 0);
  72. }
  73.  
  74. Date.prototype.getTimeFormated = function()
  75. {
  76.   return this.getHours().toString().pad(2, "0", 0) + ":" + this.getMinutes().toString().pad(2, "0", 0) + ":" + this.getSeconds().toString().pad(2, "0", 0);
  77. }
  78.  
  79. Date.prototype.getTimeShortFormated = function()
  80. {
  81.   return this.getHours().toString().pad(2, "0", 0) + ":" + this.getMinutes().toString().pad(2, "0", 0);
  82. }
  83.  
  84. Date.prototype.getDateTimeFormated = function()
  85. {
  86.   return this.getDateFormated() + " " + this.getTimeFormated();
  87. }
  88.  
  89. function get_time()
  90. {
  91.     return Math.round(new Date().getTime() / 1000);
  92. }
  93.  
  94. function to_array(obj)
  95. {
  96.     var result = [];
  97.    
  98.     for (var n = 0; n < obj.length; n++)
  99.     {
  100.         result.push(obj[n]);
  101.     }
  102.    
  103.     return result;
  104. }
  105.  
  106. function array_length(array)
  107. {
  108.     var count = 0;
  109.    
  110.     for (var n in array)
  111.     {
  112.         count++;
  113.     }
  114.    
  115.     return count;
  116. }
  117.  
  118. function array_remove_empty(array)
  119. {
  120.     var result = [];
  121.    
  122.     for (var n in array)
  123.     {
  124.         if (array[n] && (typeof array[n] == 'string' && array[n].length > 0))
  125.         {
  126.             result.push(array[n]);
  127.         }
  128.     }
  129.    
  130.     return result;
  131. }
  132.  
  133. function in_array(array, item)
  134. {
  135.     for (var n in array)
  136.     {
  137.         if (array[n] == item)
  138.         {
  139.             return true;
  140.         }
  141.     }
  142.    
  143.     return false;
  144. }
  145.  
  146. function key_in_array(array, key)
  147. {
  148.     for (var n in array)
  149.     {
  150.         if (n == key)
  151.         {
  152.             return true;
  153.         }
  154.     }
  155.    
  156.     return false;
  157. }
  158.  
  159. function get_keys(array)
  160. {
  161.     var result = [];
  162.    
  163.     for (var name in array)
  164.     {
  165.         result.push(name);
  166.     }
  167.    
  168.     return result;
  169. }
  170.  
  171. String.prototype.ltrim = function(charlist)
  172. {
  173.     charlist = !charlist ? ' \s\xA0' : charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '\$1');
  174.     var re = new RegExp('^[' + charlist + ']+', 'g');
  175.     return this.replace(re, '');
  176. }
  177.  
  178. String.prototype.rtrim = function(charlist)
  179. {
  180.     charlist = !charlist ? ' \s\xA0' : charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '\$1');
  181.     var re = new RegExp('[' + charlist + ']+$', 'g');
  182.     return this.replace(re, '');
  183. }
  184.  
  185. String.prototype.trim = function(charlist)
  186. {
  187.     var str = this.ltrim(charlist);
  188.     return str.rtrim(charlist);
  189. }
  190.  
  191. String.prototype.rpad = function(char, length)
  192. {
  193.     var result = this;
  194.    
  195.     while (result.length < length)
  196.     {
  197.         result += char;
  198.     }
  199.    
  200.     return result;
  201. }
  202.  
  203. function create_table(table_lines)
  204. {
  205.     var table_rows = [];
  206.    
  207.     if (table_lines.length == 0)
  208.     {
  209.         return table_rows;
  210.     }
  211.    
  212.     for (var col = 0; col < table_lines[0].length; col++)
  213.     {
  214.         var len = 0;
  215.        
  216.         for (var row = 0; row < table_lines.length; row++)
  217.         {
  218.             if (table_lines[row][col].length > len)
  219.             {
  220.                 len = table_lines[row][col].length;
  221.             }
  222.         }
  223.        
  224.         len += 6;
  225.        
  226.         for (var row = 0; row < table_lines.length; row++)
  227.         {
  228.             if (!table_rows[row])
  229.             {
  230.                 table_rows[row] = "";
  231.             }
  232.            
  233.             var str = table_lines[row][col] + '';
  234.             str = str.rpad(" ", len);
  235.             //Log("col=" + col + ", row=" + row + " len=" + len + ", str=\"" + str + "\", str.length=" + str.length + "\n");
  236.             table_rows[row] += str;
  237.         }
  238.     }
  239.    
  240.     return table_rows;
  241. }
  242.  
  243. function parse_json_rpc(data)
  244. {
  245.   var items = [];
  246.   var position = -1;
  247.  
  248.   //Log(data);
  249.  
  250.   while ((position = data.indexOf("}{")) != -1)
  251.   {
  252.     //Log(data.substr(0, position + 1));
  253.    
  254.     items.push(eval("(" + data.substr(0, position + 1) + ")"));
  255.    
  256.     data = data.substring(position + 1);
  257.    
  258.     //Log(data);
  259.   }
  260.  
  261.   items.push(eval("(" + data + ")"));
  262.  
  263.   return items;
  264. }
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273.