Subversion Repositories HomeAutomation

Rev

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

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