Bug 1338458 - Use the hyphen style for fieldName instead of camel case style in Profile Storage.; r=MattN
authorSean Lee <selee@mozilla.com>
Fri, 10 Feb 2017 12:03:59 +0800
changeset 342233 f73fdf5965ef2061d60bb6fb440755264ad3ea68
parent 342232 73b628209894c110320ffd1941981e55f6a0e30e
child 342234 434ed9575f9afa3933df2a9f1745c874317c9820
push id31346
push userkwierso@gmail.com
push dateFri, 10 Feb 2017 22:33:24 +0000
treeherdermozilla-central@7b9d9e4a82a6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersMattN
bugs1338458
milestone54.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1338458 - Use the hyphen style for fieldName instead of camel case style in Profile Storage.; r=MattN MozReview-Commit-ID: 9wpeYVxWZ1L
browser/extensions/formautofill/FormAutofillParent.jsm
browser/extensions/formautofill/ProfileStorage.jsm
browser/extensions/formautofill/test/unit/test_populateFieldValues.js
browser/extensions/formautofill/test/unit/test_profileStorage.js
--- a/browser/extensions/formautofill/FormAutofillParent.jsm
+++ b/browser/extensions/formautofill/FormAutofillParent.jsm
@@ -142,48 +142,33 @@ let FormAutofillParent = {
     } else {
       profiles = this._profileStore.getAll();
     }
 
     target.messageManager.sendAsyncMessage("FormAutofill:Profiles", profiles);
   },
 
   /**
-   * Transforms a word with hyphen into camel case.
-   * (e.g. transforms "address-type" into "addressType".)
-   *
-   * @private
-   * @param   {string} str The original string with hyphen.
-   * @returns {string} The camel-cased output string.
-   */
-  _camelCase(str) {
-    return str.toLowerCase().replace(/-([a-z])/g, s => s[1].toUpperCase());
-  },
-
-  /**
    * Get the corresponding value from the specified profile according to a valid
    * @autocomplete field name.
    *
    * Note that the field name doesn't need to match the property name defined in
    * Profile object. This method can transform the raw data to fulfill it. (e.g.
    * inputting "country-name" as "fieldName" will get a full name transformed
    * from the country code that is recorded in "country" field.)
    *
    * @private
    * @param   {Profile} profile   The specified profile.
    * @param   {string}  fieldName A valid @autocomplete field name.
    * @returns {string}  The corresponding value. Returns "undefined" if there's
    *                    no matching field.
    */
   _getDataByFieldName(profile, fieldName) {
-    let key = this._camelCase(fieldName);
-
     // TODO: Transform the raw profile data to fulfill "fieldName" here.
-
-    return profile[key];
+    return profile[fieldName];
   },
 
   /**
    * Fills in the "fields" array by the specified profile.
    *
    * @private
    * @param   {Profile} profile The specified profile to fill in.
    * @param   {Fields}  fields  The "fields" array collected from content.
--- a/browser/extensions/formautofill/ProfileStorage.jsm
+++ b/browser/extensions/formautofill/ProfileStorage.jsm
@@ -11,20 +11,20 @@
  * {
  *   version: 1,
  *   profiles: [
  *     {
  *       guid,             // 12 character...
  *
  *       // profile
  *       organization,     // Company
- *       streetAddress,    // (Multiline)
- *       addressLevel2,    // City/Town
- *       addressLevel1,    // Province (Standardized code if possible)
- *       postalCode,
+ *       street-address,    // (Multiline)
+ *       address-level2,    // City/Town
+ *       address-level1,    // Province (Standardized code if possible)
+ *       postal-code,
  *       country,          // ISO 3166
  *       tel,
  *       email,
  *
  *       // metadata
  *       timeCreated,      // in ms
  *       timeLastUsed,     // in ms
  *       timeLastModified, // in ms
@@ -52,36 +52,36 @@ XPCOMUtils.defineLazyServiceGetter(this,
                                    "@mozilla.org/uuid-generator;1",
                                    "nsIUUIDGenerator");
 
 const SCHEMA_VERSION = 1;
 
 // Name-related fields will be handled in follow-up bugs due to the complexity.
 const VALID_FIELDS = [
   "organization",
-  "streetAddress",
-  "addressLevel2",
-  "addressLevel1",
-  "postalCode",
+  "street-address",
+  "address-level2",
+  "address-level1",
+  "postal-code",
   "country",
   "tel",
   "email",
 ];
 
 // TODO: Remove this once we can add profile from preference.
 const MOCK_MODE = false;
 const MOCK_STORAGE = [{
   guid: "test-guid-1",
   organization: "Sesame Street",
-  streetAddress: "123 Sesame Street.",
+  "street-address": "123 Sesame Street.",
   tel: "1-345-345-3456",
 }, {
   guid: "test-guid-2",
   organization: "Mozilla",
-  streetAddress: "331 E. Evelyn Avenue",
+  "street-address": "331 E. Evelyn Avenue",
   tel: "1-650-903-0800",
 }];
 
 function ProfileStorage(path) {
   this._path = path;
 }
 
 ProfileStorage.prototype = {
--- a/browser/extensions/formautofill/test/unit/test_populateFieldValues.js
+++ b/browser/extensions/formautofill/test/unit/test_populateFieldValues.js
@@ -21,29 +21,25 @@ const TEST_FIELDS = [
   {"section": "", "addressType": "shipping", "contactType": "", "fieldName": "email"},
 ];
 
 const TEST_GUID = "test-guid";
 
 const TEST_PROFILE = {
   guid: TEST_GUID,
   organization: "World Wide Web Consortium",
-  streetAddress: "32 Vassar Street\nMIT Room 32-G524",
-  addressLevel2: "Cambridge",
-  addressLevel1: "MA",
+  "street-address": "32 Vassar Street\nMIT Room 32-G524",
+  "address-level2": "Cambridge",
+  "address-level1": "MA",
   postalCode: "02139",
   country: "US",
   tel: "+1 617 253 5702",
   email: "timbl@w3.org",
 };
 
-function camelCase(str) {
-  return str.toLowerCase().replace(/-([a-z])/g, s => s[1].toUpperCase());
-}
-
 add_task(function* test_populateFieldValues() {
   FormAutofillParent.init();
 
   let store = FormAutofillParent.getProfileStore();
   do_check_neq(store, null);
 
   store.get = function(guid) {
     do_check_eq(guid, TEST_GUID);
@@ -68,17 +64,17 @@ add_task(function* test_populateFieldVal
           do_check_eq(name, "FormAutofill:fillForm");
 
           let fields = data.fields;
           do_check_eq(fields.length, TEST_FIELDS.length);
 
           for (let i = 0; i < fields.length; i++) {
             do_check_eq(fields[i].fieldName, TEST_FIELDS[i].fieldName);
             do_check_eq(fields[i].value,
-              TEST_PROFILE[camelCase(fields[i].fieldName)]);
+              TEST_PROFILE[fields[i].fieldName]);
           }
 
           resolve();
         },
       },
     });
   });
 
--- a/browser/extensions/formautofill/test/unit/test_profileStorage.js
+++ b/browser/extensions/formautofill/test/unit/test_profileStorage.js
@@ -8,37 +8,37 @@
 
 Cu.import("resource://gre/modules/Task.jsm");
 Cu.import("resource://formautofill/ProfileStorage.jsm");
 
 const TEST_STORE_FILE_NAME = "test-profile.json";
 
 const TEST_PROFILE_1 = {
   organization: "World Wide Web Consortium",
-  streetAddress: "32 Vassar Street\nMIT Room 32-G524",
-  addressLevel2: "Cambridge",
-  addressLevel1: "MA",
-  postalCode: "02139",
+  "street-address": "32 Vassar Street\nMIT Room 32-G524",
+  "address-level2": "Cambridge",
+  "address-level1": "MA",
+  "postal-code": "02139",
   country: "US",
   tel: "+1 617 253 5702",
   email: "timbl@w3.org",
 };
 
 const TEST_PROFILE_2 = {
-  streetAddress: "Some Address",
+  "street-address": "Some Address",
   country: "US",
 };
 
 const TEST_PROFILE_3 = {
-  streetAddress: "Other Address",
-  postalCode: "12345",
+  "street-address": "Other Address",
+  "postal-code": "12345",
 };
 
 const TEST_PROFILE_WITH_INVALID_FIELD = {
-  streetAddress: "Another Address",
+  "street-address": "Another Address",
   invalidField: "INVALID",
 };
 
 let prepareTestProfiles = Task.async(function* (path) {
   let profileStorage = new ProfileStorage(path);
   yield profileStorage.initialize();
 
   profileStorage.add(TEST_PROFILE_1);
@@ -111,37 +111,37 @@ add_task(function* test_get() {
 
 add_task(function* test_getByFilter() {
   let path = getTempFile(TEST_STORE_FILE_NAME).path;
   yield prepareTestProfiles(path);
 
   let profileStorage = new ProfileStorage(path);
   yield profileStorage.initialize();
 
-  let filter = {info: {fieldName: "streetAddress"}, searchString: "Some"};
+  let filter = {info: {fieldName: "street-address"}, searchString: "Some"};
   let profiles = profileStorage.getByFilter(filter);
   do_check_eq(profiles.length, 1);
   do_check_profile_matches(profiles[0], TEST_PROFILE_2);
 
   filter = {info: {fieldName: "country"}, searchString: "u"};
   profiles = profileStorage.getByFilter(filter);
   do_check_eq(profiles.length, 2);
   do_check_profile_matches(profiles[0], TEST_PROFILE_1);
   do_check_profile_matches(profiles[1], TEST_PROFILE_2);
 
-  filter = {info: {fieldName: "streetAddress"}, searchString: "test"};
+  filter = {info: {fieldName: "street-address"}, searchString: "test"};
   profiles = profileStorage.getByFilter(filter);
   do_check_eq(profiles.length, 0);
 
-  filter = {info: {fieldName: "streetAddress"}, searchString: ""};
+  filter = {info: {fieldName: "street-address"}, searchString: ""};
   profiles = profileStorage.getByFilter(filter);
   do_check_eq(profiles.length, 2);
 
   // Check if the filtering logic is free from searching special chars.
-  filter = {info: {fieldName: "streetAddress"}, searchString: ".*"};
+  filter = {info: {fieldName: "street-address"}, searchString: ".*"};
   profiles = profileStorage.getByFilter(filter);
   do_check_eq(profiles.length, 0);
 });
 
 add_task(function* test_add() {
   let path = getTempFile(TEST_STORE_FILE_NAME).path;
   yield prepareTestProfiles(path);