Bug 1264696 - Part 1: Move string utility functions to rep-utils. r=Honza draft
authorLin Clark <lclark@mozilla.com>
Thu, 23 Jun 2016 17:14:41 -0400
changeset 384244 a4ba9cb5ea0ec033f29073e5218785e25c3c743c
parent 383145 fdcee57b4e4f66a82831ab01e61500da98a858e8
child 384245 d49ac8fb4dc4239186a48c1d6626835f47b29020
child 384512 5ca75430e7a9bc06b50b6f1759c9575338bcbb4a
push id22226
push userbmo:lclark@mozilla.com
push dateTue, 05 Jul 2016 22:17:56 +0000
reviewersHonza
bugs1264696
milestone50.0a1
Bug 1264696 - Part 1: Move string utility functions to rep-utils. r=Honza
devtools/client/shared/components/reps/function.js
devtools/client/shared/components/reps/rep-utils.js
devtools/client/shared/components/reps/string.js
devtools/client/shared/components/reps/text-node.js
devtools/client/shared/components/reps/window.js
--- a/devtools/client/shared/components/reps/function.js
+++ b/devtools/client/shared/components/reps/function.js
@@ -6,19 +6,18 @@
 "use strict";
 
 // Make this available to both AMD and CJS environments
 define(function (require, exports, module) {
   // ReactJS
   const React = require("devtools/client/shared/vendor/react");
 
   // Reps
-  const { createFactories, isGrip } = require("./rep-utils");
+  const { createFactories, isGrip, cropString } = require("./rep-utils");
   const { ObjectLink } = createFactories(require("./object-link"));
-  const { cropString } = require("./string");
 
   /**
    * This component represents a template for Function objects.
    */
   let Func = React.createClass({
     displayName: "Func",
 
     propTypes: {
--- a/devtools/client/shared/components/reps/rep-utils.js
+++ b/devtools/client/shared/components/reps/rep-utils.js
@@ -26,12 +26,57 @@ define(function (require, exports, modul
 
   /**
    * Returns true if the given object is a grip (see RDP protocol)
    */
   function isGrip(object) {
     return object && object.actor;
   }
 
+
+  function escapeNewLines(value) {
+    return value.replace(/\r/gm, "\\r").replace(/\n/gm, "\\n");
+  }
+
+  function cropMultipleLines(text, limit) {
+    return escapeNewLines(cropString(text, limit));
+  }
+
+  function cropString(text, limit, alternativeText) {
+    if (!alternativeText) {
+      alternativeText = "\u2026";
+    }
+
+    // Make sure it's a string.
+    text = text + "";
+
+    // Use default limit if necessary.
+    if (!limit) {
+      limit = 50;
+    }
+
+    // Crop the string only if a limit is actually specified.
+    if (limit <= 0) {
+      return text;
+    }
+
+    // Set the limit at least to the length of the alternative text
+    // plus one character of the original text.
+    if (limit <= alternativeText.length) {
+      limit = alternativeText.length + 1;
+    }
+
+    let halfLimit = (limit - alternativeText.length) / 2;
+
+    if (text.length > limit) {
+      return text.substr(0, Math.ceil(halfLimit)) + alternativeText +
+        text.substr(text.length - Math.floor(halfLimit));
+    }
+
+    return text;
+  }
+
   // Exports from this module
   exports.createFactories = createFactories;
   exports.isGrip = isGrip;
+  exports.cropString = cropString;
+  exports.cropMultipleLines = cropMultipleLines;
 });
--- a/devtools/client/shared/components/reps/string.js
+++ b/devtools/client/shared/components/reps/string.js
@@ -5,17 +5,17 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 "use strict";
 
 // Make this available to both AMD and CJS environments
 define(function (require, exports, module) {
   // Dependencies
   const React = require("devtools/client/shared/vendor/react");
-  const { createFactories } = require("./rep-utils");
+  const { createFactories, cropMultipleLines } = require("./rep-utils");
   const { ObjectBox } = createFactories(require("./object-box"));
 
   /**
    * Renders a string. String value is enclosed within quotes.
    */
   const StringRep = React.createClass({
     displayName: "StringRep",
 
@@ -35,70 +35,19 @@ define(function (require, exports, modul
           "\"" + cropMultipleLines(text) + "\""
         )
       );
     },
   });
 
   // Helpers
 
-  function escapeNewLines(value) {
-    return value.replace(/\r/gm, "\\r").replace(/\n/gm, "\\n");
-  }
-
-  function cropMultipleLines(text, limit) {
-    return escapeNewLines(cropString(text, limit));
-  }
-
-  function cropString(text, limit, alternativeText) {
-    if (!alternativeText) {
-      alternativeText = "\u2026";
-    }
-
-    // Make sure it's a string.
-    text = text + "";
-
-    // Use default limit if necessary.
-    if (!limit) {
-      limit = 50;
-    }
-
-    // Crop the string only if a limit is actually specified.
-    if (limit <= 0) {
-      return text;
-    }
-
-    // Set the limit at least to the length of the alternative text
-    // plus one character of the original text.
-    if (limit <= alternativeText.length) {
-      limit = alternativeText.length + 1;
-    }
-
-    let halfLimit = (limit - alternativeText.length) / 2;
-
-    if (text.length > limit) {
-      return text.substr(0, Math.ceil(halfLimit)) + alternativeText +
-        text.substr(text.length - Math.floor(halfLimit));
-    }
-
-    return text;
-  }
-
-  function isCropped(value) {
-    let cropLength = 50;
-    return typeof value == "string" && value.length > cropLength;
-  }
-
   function supportsObject(object, type) {
     return (type == "string");
   }
 
   // Exports from this module
 
   exports.StringRep = {
     rep: StringRep,
     supportsObject: supportsObject,
   };
-
-  exports.isCropped = isCropped;
-  exports.cropString = cropString;
-  exports.cropMultipleLines = cropMultipleLines;
 });
--- a/devtools/client/shared/components/reps/text-node.js
+++ b/devtools/client/shared/components/reps/text-node.js
@@ -6,19 +6,18 @@
 "use strict";
 
 // Make this available to both AMD and CJS environments
 define(function (require, exports, module) {
   // ReactJS
   const React = require("devtools/client/shared/vendor/react");
 
   // Reps
-  const { createFactories, isGrip } = require("./rep-utils");
+  const { createFactories, isGrip, cropMultipleLines } = require("./rep-utils");
   const { ObjectLink } = createFactories(require("./object-link"));
-  const { cropMultipleLines } = require("./string");
 
   // Shortcuts
   const DOM = React.DOM;
 
   /**
    * Renders DOM #text node.
    */
   let TextNode = React.createClass({
--- a/devtools/client/shared/components/reps/window.js
+++ b/devtools/client/shared/components/reps/window.js
@@ -6,19 +6,18 @@
 "use strict";
 
 // Make this available to both AMD and CJS environments
 define(function (require, exports, module) {
   // ReactJS
   const React = require("devtools/client/shared/vendor/react");
 
   // Reps
-  const { createFactories, isGrip } = require("./rep-utils");
+  const { createFactories, isGrip, cropString } = require("./rep-utils");
   const { ObjectBox } = createFactories(require("./object-box"));
-  const { cropString } = require("./string");
 
   // Shortcuts
   const DOM = React.DOM;
 
   /**
    * Renders a grip representing a window.
    */
   let Window = React.createClass({