Bug 1343122 - Support multiple params with the same name. r=Honza, a=lizzard
authorFred Lin <gasolin@mozilla.com>
Thu, 02 Mar 2017 10:35:20 +0800
changeset 376605 7c156e6954655962756727949406eb3b0acaa23f
parent 376604 21bb36dab77ee30c9d6050aec2bc4b5f72d120b2
child 376606 5fd05d2d980957d2657b68de28e0242f2deb12d7
push id6996
push userjlorenzo@mozilla.com
push dateMon, 06 Mar 2017 20:48:21 +0000
treeherdermozilla-beta@d89512dab048 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersHonza, lizzard
bugs1343122
milestone53.0a2
Bug 1343122 - Support multiple params with the same name. r=Honza, a=lizzard MozReview-Commit-ID: 7Xj3fq5apTc
devtools/client/netmonitor/shared/components/params-panel.js
--- a/devtools/client/netmonitor/shared/components/params-panel.js
+++ b/devtools/client/netmonitor/shared/components/params-panel.js
@@ -48,41 +48,23 @@ function ParamsPanel({
     );
   }
 
   let object = {};
   let json;
 
   // Query String section
   if (query) {
-    object[PARAMS_QUERY_STRING] =
-      parseQueryString(query)
-        .reduce((acc, { name, value }) =>
-          name ? Object.assign(acc, { [name]: value }) : acc
-        , {});
+    object[PARAMS_QUERY_STRING] = getProperties(parseQueryString(query));
   }
 
   // Form Data section
   if (formDataSections && formDataSections.length > 0) {
     let sections = formDataSections.filter((str) => /\S/.test(str)).join("&");
-    object[PARAMS_FORM_DATA] =
-      parseQueryString(sections)
-        .reduce((map, obj) => {
-          let value = map[obj.name];
-          // Deal with duplicate key case (ex: multiple selection)
-          if (value) {
-            if (typeof value !== "object") {
-              map[obj.name] = [value];
-            }
-            map[obj.name].push(obj.value);
-          } else {
-            map[obj.name] = obj.value;
-          }
-          return map;
-        }, {});
+    object[PARAMS_FORM_DATA] = getProperties(parseQueryString(sections));
   }
 
   // Request payload section
   if (formDataSections && formDataSections.length === 0 && postData) {
     try {
       json = JSON.parse(postData);
     } catch (error) {
       // Continue regardless of parsing error
@@ -116,16 +98,40 @@ function ParamsPanel({
 ParamsPanel.displayName = "ParamsPanel";
 
 ParamsPanel.propTypes = {
   formDataSections: PropTypes.array,
   postData: PropTypes.string,
   query: PropTypes.string,
 };
 
+/**
+ * Mapping array to dict for TreeView usage.
+ * Since TreeView only support Object(dict) format.
+ * This function also deal with duplicate key case
+ * (for multiple selection and query params with same keys)
+ *
+ * @param {Object[]} arr - key-value pair array like query or form params
+ * @returns {Object} Rep compatible object
+ */
+function getProperties(arr) {
+  return arr.reduce((map, obj) => {
+    let value = map[obj.name];
+    if (value) {
+      if (typeof value !== "object") {
+        map[obj.name] = [value];
+      }
+      map[obj.name].push(obj.value);
+    } else {
+      map[obj.name] = obj.value;
+    }
+    return map;
+  }, {});
+}
+
 module.exports = connect(
   (state) => {
     const selectedRequest = getSelectedRequest(state);
 
     if (selectedRequest) {
       const { formDataSections, mimeType, requestPostData, url } = selectedRequest;
 
       return {