all files / blackbird/modules/ Location.js

96.72% Statements 59/61
94.44% Branches 51/54
93.33% Functions 28/30
96.72% Lines 59/61
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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182     186×                           27×           186× 1860× 822×           185× 185× 185×                                       16× 16× 16×   16×             71×       94×       29×       93×       24× 24× 24× 24×   24×       90× 90× 90×       47×       184×       33×       184×       118×       184×       18×       90×   90× 38× 38× 38×   52× 52×         53×       277×       25×       98×       23×       97×                      
const mergeQuery = require("./utils/mergeQuery");
const stringifyQuery = require("./utils/stringifyQuery");
const parseQuery = require("qs").parse;
const parseURL = require("url").parse;
const R = require("ramda");
 
module.exports = class Location {
    static get PROPERTY_NAMES() {
        return [
            "protocol",
            "auth",
            "hostname",
            "port",
            "host",
            "pathname",
            "search",
            "queryString",
            "query",
            "path"
        ];
    }
    static get STANDARD_PORTS() {
        return {
            "http:": "80",
            "https:": "443"
        };
    }
    static setProperties(location, properties) {
        R.forEach(function (propertyName) {
            if (properties.hasOwnProperty(propertyName)) {
                location[propertyName] = properties[propertyName];
            }
        }, Location.PROPERTY_NAMES);
    }
 
    constructor(options) {
        this.properties = {};
        const transform = R.is(String, options) ? parseURL : R.identity;
        Location.setProperties(this, transform(options || {}));
    }
 
    concat(location) {
        Eif (!R.is(Location, location)) {
            location = new Location(location);
        }
 
        let pathname = this.pathname;
        const extraPathname = location.pathname;
 
        Eif (extraPathname !== "/") {
            pathname = pathname.replace(/\/*$/, "/") + extraPathname.replace(/^\/*/, "");
        }
 
        const query = mergeQuery(this.query, location.query);
 
        return new Location({
            protocol: location.protocol || this.protocol,
            auth: location.auth || this.auth,
            hostname: location.hostname || this.hostname,
            port: location.port || this.port,
            pathname,
            query
        });
    }
 
    get href() {
        const auth = this.auth;
        const host = this.host;
        const path = this.path;
 
        return host ? `${this.protocol}//${(auth ? `${auth}@` : "") + host + path}` : path;
    }
 
    set href(value) {
        Location.setProperties(this, parseURL(value));
    }
 
    get protocol() {
        return this.properties.protocol;
    }
 
    set protocol(p) {
        this.properties.protocol = p;
    }
 
    get auth() {
        return this.properties.auth || "";
    }
 
    set auth(a) {
        this.properties.auth = a;
    }
 
    get host() {
        const protocol = this.protocol || "";
        const port = this.port;
        const portAddition = !R.isNil(port) && port !== Location.STANDARD_PORTS[protocol] ? `:${port}` : "";
        const host = this.hostname || "";
 
        return host + portAddition;
    }
 
    set host(value) {
        const [hostname, port] = (value || "").split(":");
        this.hostname = hostname || null;
        this.port = port || null;
    }
 
    get hostname() {
        return this.properties.hostname;
    }
 
    set hostname(h) {
        this.properties.hostname = h;
    }
 
    get port() {
        return this.properties.port || (this.protocol ? Location.STANDARD_PORTS[this.protocol] : null);
    }
 
    set port(value) {
        this.properties.port = value ? String(value) : null;
    }
 
    get pathname() {
        return this.properties.pathname || "/";
    }
 
    set pathname(p) {
        this.properties.pathname = p;
    }
 
    get path() {
        return this.pathname + this.search;
    }
 
    set path(value) {
        let index;
 
        if (typeof value === "string" && R.contains("?", value)) {
            index = R.findIndex(R.equals("?"), value);
            this.pathname = value.substring(0, index);
            this.search = value.substring(index);
        } else {
            this.pathname = value;
            this.search = null;
        }
    }
 
    get search() {
        return this.properties.search || "";
    }
 
    set search(s) {
        this.properties.search = s;
    }
 
    get queryString() {
        return this.search.substring(1);
    }
 
    set queryString(value) {
        this.search = value && `?${value}`;
    }
 
    get query() {
        return parseQuery(this.queryString);
    }
 
    set query(value) {
        this.queryString = stringifyQuery(value);
    }
 
    toJSON() {
        return this.href;
    }
 
    toString() {
        return this.href;
    }
};