source: branches/4.2/com/jeroenwijering/utils/Strings.as @ 68

Revision 68, 2.8 KB checked in by jeroen, 5 years ago (diff)

added new feeditems, polished simple/thin skins and made sure large YT images are loaded by default

  • Property svn:executable set to *
Line 
1/**
2* A couple of commonly used string operations.
3**/
4package com.jeroenwijering.utils {
5
6
7public class Strings {
8
9
10        /**
11        * URLDecode a string and filter "asfunction" occurences ( can be used for XSS exploits).
12        *
13        * @param str    The string to decode.
14        * @return               The decoded string.
15        **/
16        public static function decode(str:String):String {
17                if(str.indexOf('asfunction') == -1) {
18                        return unescape(str);
19                } else {
20                        return '';
21                }
22       
23        };
24
25
26        /**
27        * Convert a number to a digital-clock like string.
28        *
29        * @param nbr    The number of seconds.
30        * @return               A MN:SS string.
31        **/
32        public static function digits(nbr:Number):String {
33                var min:Number = Math.floor(nbr/60);
34                var sec:Number = Math.floor(nbr%60);
35                return Strings.zero(min)+':'+Strings.zero(sec);
36        };
37
38
39        /**
40        * Convert a time-representing string to a number.
41        *
42        * @param str    The input string. Supported are 00:03:00.1 / 03:00.1 / 180.1s / 3.2m / 3.2h
43        * @return               The number of seconds.
44        **/
45        public static function seconds(str:String):Number {
46                var arr:Array = str.split(':');
47                var sec:Number = 0;
48                if (str.substr(-1) == 's') {
49                        sec = Number(str.substr(0,str.length-1));
50                } else if (str.substr(-1) == 'm') {
51                        sec = Number(str.substr(0,str.length-1))*60;
52                } else if(str.substr(-1) == 'h') {
53                        sec = Number(str.substr(0,str.length-1))*3600;
54                } else if(arr.length > 1) {
55                        sec = Number(arr[arr.length-1]);
56                        sec += Number(arr[arr.length-2])*60;
57                        if(arr.length == 3) {
58                                sec += Number(arr[arr.length-3])*3600;
59                        }
60                } else {
61                        sec = Number(str);
62                }
63                return sec;
64        };
65
66
67        /**
68        * Basic serialization: string representations of booleans and numbers are returned typed;
69        * strings are returned urldecoded.
70        *
71        * @param val    String value to serialize.
72        * @return               The original value in the correct primitive type.
73        **/
74        public static function serialize(val:String):Object {
75                if (val == 'true') {
76                        return true;
77                } else if (val == 'false') {
78                        return false;
79                } else if (isNaN(Number(val))) {
80                        return Strings.decode(val);
81                } else {
82                        return Number(val);
83                }
84        };
85
86
87        /**
88        * Strip HTML tags and trim whitespace for a string.
89        *
90        * @param str    The string to fix.
91        * @return               The clean string.
92        **/
93        public static function strip(str:String):String {
94                var tmp:Array = str.split("\n");
95                str = tmp.join("");
96                tmp = str.split("\r");
97                str = tmp.join("");
98                var idx:Number = str.indexOf("<");
99                while(idx != -1) {
100                        var end:Number = str.indexOf(">",idx+1);
101                        end == -1 ? end = str.length-1: null;
102                        str = str.substr(0,idx)+" "+str.substr(end+1,str.length);
103                        idx = str.indexOf("<",idx);
104                }
105                return str;
106        };
107
108
109        /**
110        * Add a leading zero to a number.
111        *
112        * @param nbr    The number to convert. Can be 0 to 99.
113        * @ return              A string representation with possible leading 0.
114        **/
115        public static function zero(nbr:Number):String {
116                if(nbr < 10) {
117                        return '0'+nbr;
118                } else {
119                        return ''+nbr;
120                }
121        };
122
123
124}
125
126
127}
Note: See TracBrowser for help on using the repository browser.