* luci/themes/openwrt.org: added usability patch thanks Alina!
[project/luci.git] / themes / openwrt.org / htdocs / luci-static / openwrt.org / VarType.js
1 /*
2 Copyright (C) 2008  Alina Friedrichsen <x-alina@gmx.net>
3
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
7 1. Redistributions of source code must retain the above copyright
8    notice, this list of conditions and the following disclaimer.
9 2. Redistributions in binary form must reproduce the above copyright
10    notice, this list of conditions and the following disclaimer in the
11    documentation and/or other materials provided with the distribution.
12
13 THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 SUCH DAMAGE.
24 */
25
26 function VarType() {
27 }
28
29 VarType.isNull = function(obj) {
30         if(typeof obj == "undefined") return true;
31         if(typeof obj == "object" && (!obj)) return true;
32         return false;
33 };
34
35 VarType.toFloat = function(value) {
36         value = Number(value);
37         return value;
38 };
39
40 VarType.toDecimal = function(value) {
41         value = Number(value);
42         if(!isFinite(value)) value = 0.0;
43         return value;
44 };
45
46 VarType.toInt = function(value) {
47         value = Number(value);
48         if(!isFinite(value)) value = 0.0;
49         value = Math.floor(value);
50         return value;
51 };
52
53 VarType.toUInt = function(value) {
54         value = Number(value);
55         if(!isFinite(value)) value = 0.0;
56         else if(value < 0.0) value = 0.0;
57         value = Math.floor(value);
58         return value;
59 };
60
61 VarType.toStr = function(value) {
62         if(VarType.isNull(value)) value = "";
63         value = String(value);
64         return value;
65 };
66
67 VarType.toBool = function(value) {
68         value = Boolean(value);
69         return value;
70 };
71
72 VarType.needObject = function(obj) {
73         if(typeof obj != "object" || (!obj)) throw new TypeError();
74 };
75
76 VarType.needInstanceOf = function(obj, type) {
77         if(!(obj instanceof type)) throw new TypeError();
78 };
79
80 VarType.needFunction = function(obj) {
81         if(typeof obj != "function") throw new TypeError();
82 };
83
84 VarType.needNode = function(obj, type) {
85         VarType.needObject(obj);
86         if(VarType.isNull(obj.nodeType)) throw new TypeError();
87         if(!VarType.isNull(type)) {
88                 type = VarType.toInt(type);
89                 if(obj.nodeType != type) throw new TypeError();
90         }
91 };
92 /*
93 Copyright (C) 2008  Alina Friedrichsen <x-alina@gmx.net>
94
95 Redistribution and use in source and binary forms, with or without
96 modification, are permitted provided that the following conditions
97 are met:
98 1. Redistributions of source code must retain the above copyright
99    notice, this list of conditions and the following disclaimer.
100 2. Redistributions in binary form must reproduce the above copyright
101    notice, this list of conditions and the following disclaimer in the
102    documentation and/or other materials provided with the distribution.
103
104 THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
105 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
106 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
107 ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
108 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
109 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
110 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
111 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
112 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
113 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
114 SUCH DAMAGE.
115 */
116
117 function VarType() {
118 }
119
120 VarType.isNull = function(obj) {
121         if(typeof obj == "undefined") return true;
122         if(typeof obj == "object" && (!obj)) return true;
123         return false;
124 };
125
126 VarType.toFloat = function(value) {
127         value = Number(value);
128         return value;
129 };
130
131 VarType.toDecimal = function(value) {
132         value = Number(value);
133         if(!isFinite(value)) value = 0.0;
134         return value;
135 };
136
137 VarType.toInt = function(value) {
138         value = Number(value);
139         if(!isFinite(value)) value = 0.0;
140         value = Math.floor(value);
141         return value;
142 };
143
144 VarType.toUInt = function(value) {
145         value = Number(value);
146         if(!isFinite(value)) value = 0.0;
147         else if(value < 0.0) value = 0.0;
148         value = Math.floor(value);
149         return value;
150 };
151
152 VarType.toStr = function(value) {
153         if(VarType.isNull(value)) value = "";
154         value = String(value);
155         return value;
156 };
157
158 VarType.toBool = function(value) {
159         value = Boolean(value);
160         return value;
161 };
162
163 VarType.needObject = function(obj) {
164         if(typeof obj != "object" || (!obj)) throw new TypeError();
165 };
166
167 VarType.needInstanceOf = function(obj, type) {
168         if(!(obj instanceof type)) throw new TypeError();
169 };
170
171 VarType.needFunction = function(obj) {
172         if(typeof obj != "function") throw new TypeError();
173 };
174
175 VarType.needNode = function(obj, type) {
176         VarType.needObject(obj);
177         if(VarType.isNull(obj.nodeType)) throw new TypeError();
178         if(!VarType.isNull(type)) {
179                 type = VarType.toInt(type);
180                 if(obj.nodeType != type) throw new TypeError();
181         }
182 };