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

Revision 1, 3.0 kB (checked in by jeroen, 18 months ago)

initial commit of old repository into public one

  • 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        /**
14        * Fit string in a certain textbox, keeping full words.
15        *
16        * @param str    The string to chop.
17        * @param fld    The textfield the string should fit into.
18        **/
19        public static function fit(str:String,fld:TextField):String {
20                var wid = fld.width;
21                var lns = Math.round(fld.height/18);
22                var mxm = Math.round(wid*lns/6)-5;
23                if(str.length < mxm || str.indexOf(" ",mxm) == -1) {
24                        return str;
25                } else {
26                        return str.substr(0,str.indexOf(" ",mxm)) + ' ..';
27                }
28        };
29
30
31        /** Strip tags and trim a string; convert <br> breaks to \n. **/
32        public static function strip(str:String):String {
33                var tmp = str.split("\n");
34                str = tmp.join("");
35                tmp = str.split("\r");
36                str = tmp.join("");
37                var idx = str.indexOf("<");
38                while(idx != -1) {
39                        var end = str.indexOf(">",idx+1);
40                        end == -1 ? end = str.length-1: null;
41                        if(str.substr(idx,3) == '<br') {
42                                str = str.substr(0,idx)+"\n"+str.substr(end+1,str.length);
43                        } else {
44                                str = str.substr(0,idx)+" "+str.substr(end+1,str.length);
45                        }
46                        idx = str.indexOf("<",idx);
47                }
48                return str;
49        };
50
51
52        /** Convert number to MIN:SS string. **/
53        public static function digits(nbr:Number):String {
54                var min = Math.floor(nbr/60);
55                var sec = Math.floor(nbr%60);
56                var str = Strings.zero(min)+':'+Strings.zero(sec);
57                return str;
58        };
59
60
61        /** Add a leading zero to a number. **/
62        public static function zero(nbr:Number):String {
63                if(nbr < 10) {
64                        return '0'+nbr;
65                } else {
66                        return ''+nbr;
67                }
68        };
69
70
71        /**
72        * Convert a string to seconds, with these formats supported:
73        * 00:03:00.1 / 03:00.1 / 180.1s / 3.2m / 3.2h
74        **/
75        public static function seconds(str:String):Number {
76                var arr = str.split(':');
77                var sec = 0;
78                if (str.substr(-1) == 's') {
79                        sec = Number(str.substr(0,str.length-1));
80                } else if (str.substr(-1) == 'm') {
81                        sec = Number(str.substr(0,str.length-1))*60;
82                } else if(str.substr(-1) == 'h') {
83                        sec = Number(str.substr(0,str.length-1))*3600;
84                } else if(arr.length > 1) {
85                        sec = Number(arr[arr.length-1]);
86                        sec += Number(arr[arr.length-2])*60;
87                        if(arr.length == 3) {
88                                sec += Number(arr[arr.length-3])*3600;
89                        }
90                } else {
91                        sec = Number(str);
92                }
93                return sec;
94        };
95
96
97        /**
98        * Compare a string agains it's destination and return datatyped
99        *
100        * @param val    String value to check.
101        * @param cpm    Destination string, number or boolean.
102        *
103        * @return               The checked value in the correct primitive tpe.
104        **/
105        public static function serialize(val:String, cmp:Object):Object {
106                switch(typeof(cmp)) {
107                        case 'boolean':
108                                if (val == 'true') {
109                                        return true;
110                                } else {
111                                        return false;
112                                }
113                        case 'number':
114                                return Number(val);
115                        default:
116                                return Strings.decode(val);
117                }
118        };
119
120
121        /** URLDecode a string and remove possible asfunction reference. **/
122        public static function decode(str:String):String {
123                var idx = str.indexOf('asfunction');
124                if(idx == -1) {
125                        return decodeURI(str);
126                } else {
127                        return decodeURI(str.substr(0,idx)+str.substr(idx+10));
128                }
129       
130        };
131
132
133}
134
135
136}
Note: See TracBrowser for help on using the browser.