* Merges two query parameter objects. Merges to array * if the same key is encountered. * * @public * @function mergeQs * @param {Object} obj1 - first qs object * @param {Object} obj2 - second qs object * @returns {Object} the merged object
(obj1, obj2)
| 32 | * @returns {Object} the merged object |
| 33 | */ |
| 34 | function mergeQs(obj1, obj2) { |
| 35 | var merged = shallowCopy(obj1) || {}; |
| 36 | |
| 37 | // defend against null cause null is an object. yay js. |
| 38 | if (obj2 && typeof obj2 === 'object') { |
| 39 | Object.keys(obj2).forEach(function forEach(key) { |
| 40 | // if we already have this key and it isn't an array, |
| 41 | // make it one array of the same element. |
| 42 | if (merged.hasOwnProperty(key) && !(merged[key] instanceof Array)) { |
| 43 | merged[key] = [merged[key]]; |
| 44 | |
| 45 | // push the new value down |
| 46 | merged[key].push(obj2[key]); |
| 47 | } else { |
| 48 | // otherwise just set it |
| 49 | merged[key] = obj2[key]; |
| 50 | } |
| 51 | }); |
| 52 | } |
| 53 | |
| 54 | return merged; |
| 55 | } |
| 56 | |
| 57 | ///--- Exports |
| 58 |
no test coverage detected