summaryrefslogtreecommitdiff
path: root/htdocs/js/common/04-util.js
blob: da95e394c7875822334ffafe1aff9bb179d274de (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
function bindEventHandlers(obj) {
    for (var k in obj) {
        if (obj.hasOwnProperty(k)
            && typeof obj[k] == 'function'
            && k.length > 2
            && k.startsWith('on')
            && k[2].charCodeAt(0) >= 65
            && k[2].charCodeAt(0) <= 90) {
            obj[k] = obj[k].bind(obj)
        }
    }
}

function isObject(o) {
    return Object.prototype.toString.call(o) === '[object Object]';
}

function isArray(a) {
    return Object.prototype.toString.call(a) === '[object Array]';
}

function extend(dst, src) {
    if (!isObject(dst)) {
        return console.error('extend: dst is not an object');
    }
    if (!isObject(src)) {
        return console.error('extend: src is not an object');
    }
    for (var key in src) {
        dst[key] = src[key];
    }
    return dst;
}

function timestamp() {
    return Math.floor(Date.now() / 1000)
}

function stackTrace(split) {
    if (split === undefined) {
        split = true;
    }
    try {
        o.lo.lo += 0;
    } catch(e) {
        if (e.stack) {
            var stack = split ? e.stack.split('\n') : e.stack;
            stack.shift();
            stack.shift();
            return stack.join('\n');
        }
    }
    return null;
}

function escape(str) {
    var pre = document.createElement('pre');
    var text = document.createTextNode(str);
    pre.appendChild(text);
    return pre.innerHTML;
}

function parseUrl(uri) {
    var parser = document.createElement('a');
    parser.href = uri;

    return {
        protocol: parser.protocol, // => "http:"
        host: parser.host,         // => "example.com:3000"
        hostname: parser.hostname, // => "example.com"
        port: parser.port,         // => "3000"
        pathname: parser.pathname, // => "/pathname/"
        hash: parser.hash,         // => "#hash"
        search: parser.search,     // => "?search=test"
        origin: parser.origin,     // => "http://example.com:3000"
        path: (parser.pathname || '') + (parser.search || '')
    }
}

function once(fn, context) {
    var result;
    return function() {
        if (fn) {
            result = fn.apply(context || this, arguments);
            fn = null;
        }
        return result;
    };
}