Commiting to continue stuff at home
[swf-overlay.git] / Util.as
1 package {
2 import flash.sampler.StackFrame;
3
4 public final class Util {
5
6 public static function trim(str:String, index:int, array:Array):String {
7 if(str == null) {
8 return "";
9 }
10
11 return str.replace(/^\s+|\s+$/g, "");
12 }
13
14 //Might be better making an abstract class with this method and extending my objs
15 //Extend A with Bs properties. Properties in A will be over written with properties
16 //in B if they exist in both.
17 public static function extend(objA:Object, objB:Object):Object {
18 objA = objA || {};
19 for(var prop:String in objB) {
20 if(typeof(objB[prop]) === "object") {
21 objA[prop] = extend(objA[prop], objB[prop]);
22 } else {
23 objA[prop] = objB[prop];
24 }
25 }
26
27 return objA;
28 }
29
30 //Might be better making an abstract class with this method and extending my objs
31 public static function resolvePropertyChain(dotDelimited:String, obj:Object):* {
32 var properties:Array = dotDelimited.split('.');
33 //var field:String = properties.pop();
34 for each(var prop:String in properties) {
35 obj = obj.hasOwnProperty(prop) && obj[prop];
36 }
37
38 return obj;
39 }
40
41 public static function toPropertyChains(obj:Object):Array {
42 function chainFrom(chain:Array, obj:Object, atEnd:Function) {
43 if(typeof(obj) === "object") {
44 for(var prop:String in obj) {
45 chain.push(prop);
46 chainFrom(chain, obj[prop], atEnd);
47 --chain.length;
48 }
49 } else {
50 atEnd(chain);
51 }
52 }
53
54 var res:Array = [];
55 chainFrom([], obj, function(chain:Array) {
56 res.push(chain.join("."));
57 });
58
59 return res;
60 }
61
62 //Might be better making an abstract class with this method and extending my objs
63 public static function objectIsEmpty(obj:Object):Boolean {
64 for(var s:String in obj) {
65 return false;
66 }
67
68 return true;
69 }
70
71 public static function arrayConcatUnique(arr1:Array, arr2:Array):Array {
72 for each (var value:* in arr2) {
73 if (arr1.indexOf(value) == -1)
74 arr1.push(value);
75 }
76
77 return arr1;
78 }
79
80 }
81
82 }