1 2 define(function() { 3 4 ////////////////////// exports /////////////////////// 5 6 /** 7 * Convert parameter-part of an URL to a "dictionary", containing 8 * the parameter keys and values 9 * <p> 10 * 11 * <code>?id=5&name=heinz&name=kunz</code> → 12 * <pre> 13 * { 14 * id: "5", 15 * name: ["heinz", "kunz"], 16 * 17 * //utility functions 18 * has: function(key) : Boolean, 19 * isMultiple: function(key) : Boolean,// is entry an Array of values 20 * getKeys: function() : String[], // get list of all keys 21 * } 22 * </pre> 23 * <p> 24 * 25 * The returnd "dictionary" has the following functions: 26 * <ul> 27 * <li>has(String key): returns <code>true</code> if the 28 * dictionary contains an entry for <code>key</code></li> 29 * <li>isMultiple(String key): returns <code>true</code> if the 30 * entry for <code>key</code> is an Array (i.e. the URL contained 31 * multiple values for this key)</li> 32 * <li>getKeys(): returns an Array with the keys (String) for all 33 * entries</li> 34 * </ul> 35 * 36 * @function 37 * @param {String} urlParamsPartStrings 38 * the parameter-part of the URL, i.e. <code>&...</code> 39 * @return {Object} a "dictionary" for the parameters 40 * @public 41 * @memberOf mmir.CommonUtils.prototype 42 */ 43 function parseParamsToDictionary(urlParamsPartStrings) { 44 var dict = new Object(); 45 46 dict.has = function(key) { 47 return typeof dict[key] !== 'undefined'; 48 }; 49 dict.isMultiple = function(key) { 50 // use not-allowed-as-part-of-parameter-name character '&' as 51 // prefix for meta-data 'isMultiple' on field 'key': 52 return dict['&' + key] === true; 53 }; 54 // use not-allowed-as-part-of-parameter-name char & as prefix 55 // for meta-data 'keys-list': 56 dict['&&keys'] = []; 57 dict.getKeys = function() { 58 return dict['&&keys']; 59 }; 60 61 if (urlParamsPartStrings) { 62 if (typeof urlParamsPartStrings !== 'string') { 63 urlParamsPartStrings = urlParamsPartStrings.toString(); 64 } 65 if (urlParamsPartStrings.length < 1) { 66 return dict;//////////////////////// EARLY EXIT ///////////////////////////// 67 } 68 if (urlParamsPartStrings.charAt(0) === '?') { 69 urlParamsPartStrings = urlParamsPartStrings.substring(1); 70 } 71 72 var params = urlParamsPartStrings.split('&'); 73 var cur = null; 74 var keyValue = null; 75 var theKey = null; 76 var theValue = null; 77 for (var i_params = 0, size_params = params.length; i_params < size_params; ++i_params) { 78 79 cur = params[i_params]; 80 81 // "parse" parameter into key & value: 82 keyValue = cur.split('='); 83 theKey = keyValue[0]; 84 if (keyValue.length > 1) { 85 theValue = keyValue[1]; 86 } 87 else { 88 theValue = null; 89 } 90 91 // create entry in dict for the parameter 92 if (dict.has(theKey)) { 93 94 if (dict.isMultiple(theKey)) { 95 dict[theKey].push(theValue); 96 } 97 else { 98 // entry already exist, but is not multiple (=Array) yet: 99 var arr = new Array(2); 100 arr[0] = dict[theKey]; 101 arr[1] = theValue; 102 dict[theKey] = arr; 103 dict['&' + theKey] = true; 104 } 105 } 106 else { 107 dict[theKey] = theValue; 108 dict['&&keys'].push(theKey); 109 } 110 } 111 } 112 return dict; 113 } 114 115 return parseParamsToDictionary; 116 117 }); 118