summaryrefslogtreecommitdiffstats
path: root/theme/javascript/utils/url.js
blob: dbb157031cacc23e62dce4d89b712ffe940f5e00 (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
define([
    "vendors/URIjs/src/URI"
], function(URI) {
    // Joins path segments.  Preserves initial "/" and resolves ".." and "."
    // Does not support using ".." to go above/outside the root.
    // This means that join("foo", "../../bar") will not resolve to "../bar"
    function join(baseUrl, url) {
        var theUrl = new URI(url);
        if (theUrl.is("relative")) {
            theUrl = theUrl.absoluteTo(baseUrl);
        }
        return theUrl.toString();
    }

    // A simple function to get the dirname of a path
    // Trailing slashes are ignored. Leading slash is preserved.
    function dirname(path) {
        return join(path, "..");
    }

    // test if a path or url is absolute
    function isAbsolute(path) {
        if (!path) return false;

        return (path[0] == "/" || path.indexOf("http://") == 0 || path.indexOf("https://") == 0);
    }

    return {
        dirname: dirname,
        join: join,
        isAbsolute: isAbsolute
    };
})