summaryrefslogtreecommitdiff
path: root/scripts/lib/util.js
blob: c4ad60f94c8c681e922ecc16f1fed859340e1cda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
(function () {
    Object.defineProperties(util, 
    { 
        "getBody" :  
        { 
            value :  function(f) 
            {
                if (f && f instanceof Function) 
                {
                    var m = f.toString().match(/\{([\s\S]*)\}/m)[1];
                    m = m.replace(/^\s*\/\/.*$/mg, '');
                    m = m.replace(/^[ \t\r\v\f]*/, '');
                    if (m[0] == "\n")
                        return m.substring(1);
                    else 
                        return m;
                }
                return null;
            }
        },
        "getSelection" : 
        {
            value : function() 
            {
                var frames = tabs.current.allFrames;
                for (var i=frames.length-1; i>=0; --i) 
                {
                    var selection = JSON.parse(frames[i].inject("return document.getSelection().toString()"));
                    if (selection.length > 0)
                        return selection;
                }
                return null;
            }
        },
        "uncamelize" : 
        {
            value : function(text) 
            {
                if (! text || text.length === 0)
                    return text;

                var c = text.charAt(0);
                var uncamelized = c == c.toUpperCase() ? c.toLowerCase() : c;
                for (var i=1, l=text.length; i<l; ++i)
                {
                    c = text.charAt(i);
                    if (c == "-" || c == "_" || c.toUpperCase() != c)
                        uncamelized += c;
                    else 
                        uncamelized += "-" + c.toLowerCase();
                }
                return uncamelized;
            }
        },
        "camelize" : 
        {
            value : function(text) 
            {
                if (! text || text.length === 0)
                    return text;

                var camelized = "", c;
                for (var i=0, l=text.length; i<l; ++i)
                {
                    c = text.charAt(i);
                    if (c == "-" || c == "_") {
                        i++;
                        camelized += text.charAt(i).toUpperCase();
                    }
                    else 
                        camelized += c;
                }
                return camelized;
            }
        }
    });
    Object.freeze(util);
    
    if (Object.prototype.forEach === undefined) 
    {
        Object.defineProperty(Object.prototype, "forEach", 
        { 
            value : function (callback) 
            {
                var key;
                for (key in this) 
                {
                    if (this.hasOwnProperty(key))
                        callback(key, this[key], this); 
                }
            }
        });
    }
    if (Array.prototype.fastIndexOf === undefined) 
    {
        Object.defineProperty(Array.prototype, "fastIndexOf", 
        {
            value : function (v) 
            {
                for (var i=0, l=this.length; i<l; ++i) {
                    if (this[i] == v)
                        return i;
                }
                return -1;
            }
        });
    }
    if (Array.prototype.fastLastIndexOf === undefined) 
    {
        Object.defineProperty(Array.prototype, "fastLastIndexOf", 
        {
            value : function (v) 
            {
                for (var i=this.length-1; i>=0; --i) {
                    if (this[i] == v)
                        return i;
                }
                return -1;
            }
        });
    }
    if (! RegExp.escape)
    {
        Object.defineProperty(RegExp, "escape", 
        {
            value : function(string)
            {
                return string.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
            }
        });
    }
})();