root/trunk/as3/com/jeroenwijering/utils/Strings.as @ 4

Revision 4, 2.6 kB (checked in by jeroen, 18 months ago)

implemented stacker; display of media and some controlbaritems still broken

  • Property svn:executable set to *
Line 
1/**
2* A couple of commonly used string operations.
3**/
4package com.jeroenwijering.utils {
5
6
7import flash.text.TextField;
8
9
10public class Strings {
11
12
13        /** Strip tags and trim a string; convert <br> breaks to \n. **/
14        public static function strip(str:String):String {
15                var tmp = str.split("\n");
16                str = tmp.join("");
17                tmp = str.split("\r");
18                str = tmp.join("");
19                var idx = str.indexOf("<");
20                while(idx != -1) {
21                        var end = str.indexOf(">",idx+1);
22                        end == -1 ? end = str.length-1: null;
23                        if(str.substr(idx,3) == '<br') {
24                                str = str.substr(0,idx)+"\n"+str.substr(end+1,str.length);
25                        } else {
26                                str = str.substr(0,idx)+" "+str.substr(end+1,str.length);
27                        }
28                        idx = str.indexOf("<",idx);
29                }
30                return str;
31        };
32
33
34        /** Convert number to MIN:SS string. **/
35        public static function digits(nbr:Number):String {
36                var min = Math.floor(nbr/60);
37                var sec = Math.floor(nbr%60);
38                var str = Strings.zero(min)+':'+Strings.zero(sec);
39                return str;
40        };
41
42
43        /** Add a leading zero to a number. **/
44        public static function zero(nbr:Number):String {
45                if(nbr < 10) {
46                        return '0'+nbr;
47                } else {
48                        return ''+nbr;
49                }
50        };
51
52
53        /**
54        * Convert a string to seconds, with these formats supported:
55        * 00:03:00.1 / 03:00.1 / 180.1s / 3.2m / 3.2h
56        **/
57        public static function seconds(str:String):Number {
58                var arr = str.split(':');
59                var sec = 0;
60                if (str.substr(-1) == 's') {
61                        sec = Number(str.substr(0,str.length-1));
62                } else if (str.substr(-1) == 'm') {
63                        sec = Number(str.substr(0,str.length-1))*60;
64                } else if(str.substr(-1) == 'h') {
65                        sec = Number(str.substr(0,str.length-1))*3600;
66                } else if(arr.length > 1) {
67                        sec = Number(arr[arr.length-1]);
68                        sec += Number(arr[arr.length-2])*60;
69                        if(arr.length == 3) {
70                                sec += Number(arr[arr.length-3])*3600;
71                        }
72                } else {
73                        sec = Number(str);
74                }
75                return sec;
76        };
77
78
79        /**
80        * Compare a string agains it's destination and return datatyped
81        *
82        * @param val    String value to check.
83        * @param cpm    Destination string, number or boolean.
84        *
85        * @return               The checked value in the correct primitive tpe.
86        **/
87        public static function serialize(val:String, cmp:Object):Object {
88                switch(typeof(cmp)) {
89                        case 'boolean':
90                                if (val == 'true') {
91                                        return true;
92                                } else {
93                                        return false;
94                                }
95                        case 'number':
96                                return Number(val);
97                        default:
98                                return Strings.decode(val);
99                }
100        };
101
102
103        /** URLDecode a string and remove possible asfunction reference. **/
104        public static function decode(str:String):String {
105                var idx = str.indexOf('asfunction');
106                if(idx == -1) {
107                        return decodeURI(str);
108                } else {
109                        return decodeURI(str.substr(0,idx)+str.substr(idx+10));
110                }
111       
112        };
113
114
115}
116
117
118}
Note: See TracBrowser for help on using the browser.