Bug 1250191 - Address rnewman review, r=rnewman draft
authorMathieu Leplatre <mathieu@mozilla.com>
Mon, 11 Apr 2016 16:11:27 +0200
changeset 349406 70f18c8eb9a85f6262baaf78697bad36fc4d72b1
parent 349405 c1287ec0a56d4cf4031359c2e3b79fa426dbba3c
child 352079 31c3377daf3a90ea48df6b74f89a3177b2da1889
push id15077
push usermleplatre@mozilla.com
push dateMon, 11 Apr 2016 14:11:50 +0000
reviewersrnewman
bugs1250191
milestone48.0a1
Bug 1250191 - Address rnewman review, r=rnewman MozReview-Commit-ID: GfWytXLRj9X
services/common/canonical-json.js
services/common/tests/unit/test_canonicaljson.js
--- a/services/common/canonical-json.js
+++ b/services/common/canonical-json.js
@@ -15,53 +15,45 @@ this.CanonicalJSON = {
    *
    * @param source
    *        The elements to be serialized.
    *
    * The output will have all unicode chars escaped with the unicode codepoint
    * as lowercase hexadecimal.
    *
    * @usage
-   *        getCanonicalJSON(listOfRecords);
+   *        CanonicalJSON.stringify(listOfRecords);
    **/
   stringify: function stringify(source) {
     if (Array.isArray(source)) {
       const jsonArray = source.map(x => typeof x === "undefined" ? null : x);
       return `[${jsonArray.map(stringify).join(",")}]`;
     }
 
     if (typeof source === "number") {
       if (source === 0) {
-        return (1/source) > 0 ?  "0" : "-0";
+        return (Object.is(source, -0)) ? "-0" : "0";
       }
     }
 
+    // Leverage jsesc library, mainly for unicode escaping.
+    const toJSON = (input) => jsesc(input, {lowercaseHex: true, json: true});
+
     if (typeof source !== "object" || source === null) {
       return toJSON(source);
     }
 
-
     // Dealing with objects, ordering keys.
     const sortedKeys = Object.keys(source).sort();
+    const lastIndex = sortedKeys.length - 1;
     return sortedKeys.reduce((serial, key, index) => {
       const value = source[key];
       // JSON.stringify drops keys with an undefined value.
       if (typeof value === "undefined") {
         return serial;
       }
       const jsonValue = value && value.toJSON ? value.toJSON() : value;
-      const suffix = index !== sortedKeys.length - 1 ? "," : "";
+      const suffix = index !== lastIndex ? "," : "";
       const escapedKey = toJSON(key);
       return serial + `${escapedKey}:${stringify(jsonValue)}${suffix}`;
     }, "{") + "}";
-  }
-}
-
-/**
- * Encode the given input to valid escaped JSON.
- * @private
- **/
-function toJSON(input) {
-  return jsesc(input, {
-    lowercaseHex: true,
-    json: true
-  });
-}
+  },
+};
--- a/services/common/tests/unit/test_canonicaljson.js
+++ b/services/common/tests/unit/test_canonicaljson.js
@@ -1,12 +1,12 @@
 /* Any copyright is dedicated to the Public Domain.
    http://creativecommons.org/publicdomain/zero/1.0/ */
 
-Cu.import("resource://services-common/canonical-json.js");
+const { CanonicalJSON } = Cu.import("resource://services-common/canonical-json.js");
 
 function stringRepresentation(obj) {
   const clone = JSON.parse(JSON.stringify(obj));
   return JSON.stringify(clone);
 }
 
 add_task(function* test_canonicalJSON_should_preserve_array_order() {
     const input = ['one', 'two', 'three'];