Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 const preferences = { | |
2 string: function(key, value) { //Gets or sets a string value | |
3 if(value == undefined) //Gets | |
Felix Dahlke
2012/10/08 05:57:02
That's a bit dangerous, undefined could in fact ha
| |
4 return widget.preferences.getItem(key); | |
5 else //Sets | |
6 widget.preferences.setItem(key, value); | |
7 }, | |
8 bool: function(key, value) { //Gets or sets a bool value | |
9 if(value == undefined) { //Gets | |
Felix Dahlke
2012/10/08 05:57:02
See comment about the same code above.
| |
10 switch(widget.preferences.getItem(key)) { | |
Felix Dahlke
2012/10/08 05:57:02
How about return key === "true" instead?
| |
11 case 'true': | |
12 return true; | |
13 break; | |
14 default: //Returns false if the value doesn't ex ist | |
15 return false; | |
16 break; | |
17 } | |
18 } | |
19 else { //Sets | |
20 if(value) | |
21 widget.preferences.setItem(key, 'true'); | |
22 else | |
23 this.remove(key); //When set to false, remove th e key to save space | |
24 } | |
25 }, | |
26 int: function(key, value) { //Gets or sets a int value | |
27 if(value == undefined) { //Gets | |
Felix Dahlke
2012/10/08 05:57:02
See comment about the same code above.
| |
28 value = parseInt(widget.preferences.getItem(key)); | |
29 if(value == NaN) | |
Felix Dahlke
2012/10/08 05:57:02
No, this doesn't work. Use isNan().
| |
30 return 0; //On error return 0 | |
Felix Dahlke
2012/10/08 05:57:02
I think it's dangerous to return 0 in case of an e
| |
31 else | |
32 return value; | |
33 } | |
34 else //Sets | |
35 widget.preferences.setItem(key, value); | |
36 }, | |
37 float: function(key, value) { //Gets or sets a float value | |
38 if(value == undefined) { //Gets | |
Felix Dahlke
2012/10/08 05:57:02
See comment about the same code above.
| |
39 value = parseFloat(widget.preferences.getItem(key)); | |
40 if(value == NaN) | |
Felix Dahlke
2012/10/08 05:57:02
See above, use isNan()
| |
41 return 0; //On error return 0 | |
Felix Dahlke
2012/10/08 05:57:02
See above, not sure if this is a good idea.
| |
42 else | |
43 return value; | |
44 } | |
45 else //Sets | |
46 widget.preferences.setItem(key, value); | |
47 }, | |
48 array: function(key, value) { //Gets or sets an array in the preferences | |
49 if(value == undefined) { //Gets | |
Felix Dahlke
2012/10/08 05:57:02
See comment about the same code above.
| |
50 if(this.exist(key)) { | |
51 value = widget.preferences.getItem(key); | |
52 if(value.indexOf("\n") > -1) | |
Felix Dahlke
2012/10/08 05:57:02
This whole if/else can be replaced by: return valu
| |
53 return value.split("\n"); | |
54 else { | |
55 return [value]; | |
56 } | |
57 } | |
58 return new Array(); //Returns an empty array when no val ue exists | |
Felix Dahlke
2012/10/08 05:57:02
Shorter: return []
| |
59 } | |
60 else if(value instanceof Array) { //Sets | |
61 value = value.join("\n"); | |
62 widget.preferences.setItem(key, value); | |
63 } | |
64 }, | |
65 invert: function(key) { //Inverts a true/false setting | |
66 switch(widget.preferences.getItem(key)) { | |
Felix Dahlke
2012/10/08 05:57:02
This switch can be replaced with: widget.preferenc
| |
67 case 'true': | |
68 widget.preferences.setItem(key, 'false'); | |
69 break; | |
70 default: | |
71 widget.preferences.setItem(key, 'true'); | |
72 break; | |
73 } | |
74 }, | |
75 exist: function(key) { //Says if a preference exists | |
Felix Dahlke
2012/10/08 05:57:02
This will return false for a numerical value set t
| |
76 if(widget.preferences.getItem(key) != null && widget.preferences .getItem(key) != '') | |
77 return true; | |
78 return false; | |
79 }, | |
80 remove: function(key) { | |
81 widget.preferences.removeItem(key); | |
82 } | |
83 }; | |
OLD | NEW |