source: code/Website/open-flash-chart/Parser.as@ 7849

Last change on this file since 7849 was 7849, checked in by dennisw, 15 years ago
File size: 5.2 KB
RevLine 
[7849]1package {
2 public class Parser {
3
4 //test if undefined or null
5 public static function isEmptyValue ( value:Object ):Boolean {
6// if( value == undefined || value == null ) {
7// return true;
8// } else {
9 return false;
10// }
11 }
12
13
14
15
16 // get valid String value from input value
17 // if value is not defined, return default value
18 // default value is valid String (cannot be undefined or null)
19 // in case that isEmptyStringValid is false - take defaultvalue instead of value
20 public static function getStringValue (
21 value:Object,
22 defaultValue:String ,
23 isEmptyStringValid:Boolean ):String{
24
25 //defaultValue if not defined - set to empty String
26 if( Parser.isEmptyValue (defaultValue)) {
27 defaultValue = "";
28 }
29
30 //for undefined / null - return defaultValue
31 if( Parser.isEmptyValue (value)) {
32 return defaultValue;
33 }
34
35 if (!isEmptyStringValid && value.length == 0) {
36 return defaultValue;
37 }
38
39 return String(value);
40 }
41
42
43
44
45
46 // get valid Number value from input value
47 // if value is not defined, return default value
48 // default value is valid String (cannot be undefined or null)
49 // in case that isEmptyStringValid is false - take defaultvalue instead of value
50 public static function getNumberValue (
51 value:Object,
52 defaultValue:Number ,
53 isZeroValueValid:Boolean ,
54 isNegativeValueValid:Boolean
55 ):Number{
56
57 //defaultValue if not defined - set to zero
58 if( Parser.isEmptyValue (defaultValue)
59 || isNaN(defaultValue)
60 ) {
61 defaultValue = 0;
62 }
63
64 //for undefined / null - return defaultValue
65 if( Parser.isEmptyValue (value) ) {
66 return defaultValue;
67 }
68
69 var numValue:Number = Number(value);
70 if ( isNaN (numValue) ){
71 return defaultValue;
72 }
73
74 if (!isZeroValueValid && numValue==0) {
75 return defaultValue;
76 }
77
78 if (!isNegativeValueValid && numValue<0) {
79 return defaultValue;
80 }
81
82 return numValue;
83
84 }
85
86
87
88 public static function getBooleanValue (
89 value:Object,
90 defaultValue:Boolean
91 ):Boolean{
92
93 if( Parser.isEmptyValue (value) ) {
94 return defaultValue;
95 }
96
97 var numValue:Number = Number(value);
98 if ( !isNaN (numValue) ){
99 //for numeric value then 0 is false, everything else is true
100 if (numValue==0) {
101 return false;
102 } else {
103 return true;
104 }
105 }
106
107 //parse string falue 'true' -> true; else false
108 var strValue:String = Parser.getStringValue (value,"false", false);
109 //trace ("0------------------" + strValue);
110 strValue = strValue.toLowerCase();
111 //trace ("1------------------" + strValue);
112 if (strValue.indexOf('true') !=-1){
113 return true;
114 } else {
115 return false;
116 }
117
118 }
119
120
121
122 public static function runTests():void{
123 var notDefinedNum:Number;
124 trace ("testing Parser.getStringValue...");
125 trace("1) stringOK '" + Parser.getStringValue("stringOK","myDefault",true) + "'");
126 trace("2) '' '" + Parser.getStringValue("","myDefault",true) + "'");
127 trace("3) myDefault '" + Parser.getStringValue("","myDefault",false) + "'");
128// trace("4) '' '" + Parser.getStringValue(notDefinedNum) + "'");
129// trace("5) 999 '" + Parser.getStringValue(999) + "'");
130
131
132 trace ("testing Parser.getNumberValue...");
133 trace("01) 999 '" + Parser.getNumberValue(999,22222222,true,true) + "'");
134 trace("02) 999 '" + Parser.getNumberValue("999",22222222,true,true) + "'");
135// trace("03) 999 '" + Parser.getNumberValue("999") + "'");
136// trace("04) 0 '" + Parser.getNumberValue("abc") + "'");
137// trace("05) -1 '" + Parser.getNumberValue("abc",-1) + "'");
138 trace("06) -1 '" + Parser.getNumberValue("abc",-1, false, false) + "'");
139 trace("07) -1 '" + Parser.getNumberValue(null,-1, false, false) + "'");
140// trace("08) 22222222 '" + Parser.getNumberValue(0,22222222) + "'");
141// trace("09) 0 '" + Parser.getNumberValue(0,22222222,true) + "'");
142// trace("10) 22222222 '" + Parser.getNumberValue(0,22222222,false) + "'");
143 trace("11) 22222222 '" + Parser.getNumberValue(0,22222222,false,false) + "'");
144 trace("12) 22222222 '" + Parser.getNumberValue(-0.1,22222222,false,false) + "'");
145 trace("13) -0.1 '" + Parser.getNumberValue(-0.1,22222222,false,true) + "'");
146 trace("13) 22222222 '" + Parser.getNumberValue("-0.1x",22222222,false,true) + "'");
147
148 trace ("testing Parser.getBooleanValue...");
149 trace("true '" + Parser.getBooleanValue("1",false) + "'");
150 trace("true '" + Parser.getBooleanValue("-1",false) + "'");
151 trace("false '" + Parser.getBooleanValue("0.000",false) + "'");
152 trace("false '" + Parser.getBooleanValue("",false) + "'");
153 trace("true '" + Parser.getBooleanValue("",true) + "'");
154 trace("false '" + Parser.getBooleanValue("false",false) + "'");
155 trace("false '" + Parser.getBooleanValue("xxx",false) + "'");
156 trace("true '" + Parser.getBooleanValue("true",true) + "'");
157 trace("true '" + Parser.getBooleanValue("TRUE",true) + "'");
158 trace("true '" + Parser.getBooleanValue(" TRUE ",true) + "'");
159 }
160
161 }
162}
Note: See TracBrowser for help on using the repository browser.