|
| 1 | +// Query String Utilities |
| 2 | + |
| 3 | +var QueryString = exports; |
| 4 | + |
| 5 | +QueryString.unescape = function (str, decodeSpaces) { |
| 6 | + return decodeURIComponent(decodeSpaces ? str.replace(/\+/g, " ") : str); |
| 7 | +}; |
| 8 | + |
| 9 | +QueryString.escape = function (str) { |
| 10 | + return encodeURIComponent(str); |
| 11 | +}; |
| 12 | + |
| 13 | + |
| 14 | +var stack = []; |
| 15 | +/** |
| 16 | + * <p>Converts an arbitrary value to a Query String representation.</p> |
| 17 | + * |
| 18 | + * <p>Objects with cyclical references will trigger an exception.</p> |
| 19 | + * |
| 20 | + * @method stringify |
| 21 | + * @param obj {Variant} any arbitrary value to convert to query string |
| 22 | + * @param sep {String} (optional) Character that should join param k=v pairs together. Default: "&" |
| 23 | + * @param eq {String} (optional) Character that should join keys to their values. Default: "=" |
| 24 | + * @param name {String} (optional) Name of the current key, for handling children recursively. |
| 25 | + * @static |
| 26 | + */ |
| 27 | +QueryString.stringify = function (obj, sep, eq, name) { |
| 28 | + sep = sep || "&"; |
| 29 | + eq = eq || "="; |
| 30 | + if (isA(obj, null) || isA(obj, undefined) || typeof(obj) === 'function') { |
| 31 | + return name ? encodeURIComponent(name) + eq : ''; |
| 32 | + } |
| 33 | + |
| 34 | + if (isBool(obj)) obj = +obj; |
| 35 | + if (isNumber(obj) || isString(obj)) { |
| 36 | + return encodeURIComponent(name) + eq + encodeURIComponent(obj); |
| 37 | + } |
| 38 | + if (isA(obj, [])) { |
| 39 | + var s = []; |
| 40 | + name = name+'[]'; |
| 41 | + for (var i = 0, l = obj.length; i < l; i ++) { |
| 42 | + s.push( QueryString.stringify(obj[i], sep, eq, name) ); |
| 43 | + } |
| 44 | + return s.join(sep); |
| 45 | + } |
| 46 | + // now we know it's an object. |
| 47 | + |
| 48 | + // Check for cyclical references in nested objects |
| 49 | + for (var i = stack.length - 1; i >= 0; --i) if (stack[i] === obj) { |
| 50 | + throw new Error("querystring.stringify. Cyclical reference"); |
| 51 | + } |
| 52 | + |
| 53 | + stack.push(obj); |
| 54 | + |
| 55 | + var s = []; |
| 56 | + var begin = name ? name + '[' : ''; |
| 57 | + var end = name ? ']' : ''; |
| 58 | + for (var i in obj) if (obj.hasOwnProperty(i)) { |
| 59 | + var n = begin + i + end; |
| 60 | + s.push(QueryString.stringify(obj[i], sep, eq, n)); |
| 61 | + } |
| 62 | + |
| 63 | + stack.pop(); |
| 64 | + |
| 65 | + s = s.join(sep); |
| 66 | + if (!s && name) return name + "="; |
| 67 | + return s; |
| 68 | +}; |
| 69 | + |
| 70 | +QueryString.parseQuery = QueryString.parse = function (qs, sep, eq) { |
| 71 | + return qs |
| 72 | + .split(sep||"&") |
| 73 | + .map(pieceParser(eq||"=")) |
| 74 | + .reduce(mergeParams); |
| 75 | +}; |
| 76 | + |
| 77 | +// Parse a key=val string. |
| 78 | +// These can get pretty hairy |
| 79 | +// example flow: |
| 80 | +// parse(foo[bar][][bla]=baz) |
| 81 | +// return parse(foo[bar][][bla],"baz") |
| 82 | +// return parse(foo[bar][], {bla : "baz"}) |
| 83 | +// return parse(foo[bar], [{bla:"baz"}]) |
| 84 | +// return parse(foo, {bar:[{bla:"baz"}]}) |
| 85 | +// return {foo:{bar:[{bla:"baz"}]}} |
| 86 | +var trimmerPattern = /^\s+|\s+$/g, |
| 87 | + slicerPattern = /(.*)\[([^\]]*)\]$/; |
| 88 | +var pieceParser = function (eq) { |
| 89 | + return function parsePiece (key, val) { |
| 90 | + if (arguments.length !== 2) { |
| 91 | + // key=val, called from the map/reduce |
| 92 | + key = key.split(eq); |
| 93 | + return parsePiece( |
| 94 | + QueryString.unescape(key.shift(), true), |
| 95 | + QueryString.unescape(key.join(eq), true) |
| 96 | + ); |
| 97 | + } |
| 98 | + key = key.replace(trimmerPattern, ''); |
| 99 | + if (isString(val)) { |
| 100 | + val = val.replace(trimmerPattern, ''); |
| 101 | + // convert numerals to numbers |
| 102 | + if (!isNaN(val)) { |
| 103 | + var numVal = +val; |
| 104 | + if (val === numVal.toString(10)) val = numVal; |
| 105 | + } |
| 106 | + } |
| 107 | + var sliced = slicerPattern.exec(key); |
| 108 | + if (!sliced) { |
| 109 | + var ret = {}; |
| 110 | + if (key) ret[key] = val; |
| 111 | + return ret; |
| 112 | + } |
| 113 | + // ["foo[][bar][][baz]", "foo[][bar][]", "baz"] |
| 114 | + var tail = sliced[2], head = sliced[1]; |
| 115 | + |
| 116 | + // array: key[]=val |
| 117 | + if (!tail) return parsePiece(head, [val]); |
| 118 | + |
| 119 | + // obj: key[subkey]=val |
| 120 | + var ret = {}; |
| 121 | + ret[tail] = val; |
| 122 | + return parsePiece(head, ret); |
| 123 | + }; |
| 124 | +}; |
| 125 | + |
| 126 | +// the reducer function that merges each query piece together into one set of params |
| 127 | +function mergeParams (params, addition) { |
| 128 | + return ( |
| 129 | + // if it's uncontested, then just return the addition. |
| 130 | + (!params) ? addition |
| 131 | + // if the existing value is an array, then concat it. |
| 132 | + : (isA(params, [])) ? params.concat(addition) |
| 133 | + // if the existing value is not an array, and either are not objects, arrayify it. |
| 134 | + : (!isA(params, {}) || !isA(addition, {})) ? [params].concat(addition) |
| 135 | + // else merge them as objects, which is a little more complex |
| 136 | + : mergeObjects(params, addition) |
| 137 | + ); |
| 138 | +}; |
| 139 | + |
| 140 | +// Merge two *objects* together. If this is called, we've already ruled |
| 141 | +// out the simple cases, and need to do the for-in business. |
| 142 | +function mergeObjects (params, addition) { |
| 143 | + for (var i in addition) if (i && addition.hasOwnProperty(i)) { |
| 144 | + params[i] = mergeParams(params[i], addition[i]); |
| 145 | + } |
| 146 | + return params; |
| 147 | +}; |
| 148 | + |
| 149 | +// duck typing |
| 150 | +function isA (thing, canon) { |
| 151 | + return ( |
| 152 | + // truthiness. you can feel it in your gut. |
| 153 | + (!thing === !canon) |
| 154 | + // typeof is usually "object" |
| 155 | + && typeof(thing) === typeof(canon) |
| 156 | + // check the constructor |
| 157 | + && Object.prototype.toString.call(thing) === Object.prototype.toString.call(canon) |
| 158 | + ); |
| 159 | +}; |
| 160 | +function isBool (thing) { |
| 161 | + return ( |
| 162 | + typeof(thing) === "boolean" |
| 163 | + || isA(thing, new Boolean(thing)) |
| 164 | + ); |
| 165 | +}; |
| 166 | +function isNumber (thing) { |
| 167 | + return ( |
| 168 | + typeof(thing) === "number" |
| 169 | + || isA(thing, new Number(thing)) |
| 170 | + ) && isFinite(thing); |
| 171 | +}; |
| 172 | +function isString (thing) { |
| 173 | + return ( |
| 174 | + typeof(thing) === "string" |
| 175 | + || isA(thing, new String(thing)) |
| 176 | + ); |
| 177 | +}; |
0 commit comments