all files / src/util/ getUrlParams.ts

18.52% Statements 5/27
0% Branches 0/10
0% Functions 0/3
18.52% Lines 5/27
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                                                                                                                 
/**
 * Created by gavorhes on 6/23/2016.
 */
import provide from './provide';
let nm = provide('util');
 
 
function isNumber(checkVal: any):  boolean{
    let returnVal = parseFloat(checkVal);
 
    return !isNaN(returnVal);
}
 
/**
 *
 * @returns {object} object representation of url params
 */
export default function getUrlParams() {
    "use strict";
 
    let match;
    let pl = /\+/g;  // Regex for replacing addition symbol with a space
    let search = /([^&=]+)=?([^&]*)/g;
    let decode = function (s) {
        return decodeURIComponent(s.replace(pl, " "));
    };
    let query = window.location.search.substring(1);
 
    let urlParams = {};
    while (match = search.exec(query)) {
        /**
         * @type {string}
         */
        let val =  decode(match[2]).trim();
 
        let typedVal = null;
        if (val.length == 0){
            // pass
        }
        else if (isNumber(val)){
            if (val.indexOf('.') > -1){
                typedVal = parseFloat(val);
            } else {
                typedVal = parseInt(val);
            }
        }
        else if (val.toLowerCase() == 'false' || val.toLowerCase() == 'true'){
            typedVal = val.toLowerCase() == 'true';
        }
        else {
            typedVal = val;
        }
        urlParams[decode(match[1])] = typedVal;
    }
 
    return urlParams;
}
 
nm.getUrlParams = getUrlParams;