Subversion Repositories HomeAutomation

Rev

Rev 1759 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

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