* luci/themes:
[project/luci.git] / themes / base / htdocs / luci-static / resources / 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 };