Bug 1260589 - Add the ThreadSafeDevToolsUtils.flatten utility; r=jimb a=kwierso
authorNick Fitzgerald <fitzgen@gmail.com>
Thu, 31 Mar 2016 16:18:44 -0700
changeset 291299 1ac6445b00251dd50863942dd6901eff76ebfaa6
parent 291298 8a8cd8d35d0141f95c2b0b00cba1449d3cccf204
child 291300 a5e17a9cb4a02db80b1ba543a1158966c470078a
push id19656
push usergwagner@mozilla.com
push dateMon, 04 Apr 2016 13:43:23 +0000
treeherderb2g-inbound@e99061fde28a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjimb, kwierso
bugs1260589
milestone48.0a1
Bug 1260589 - Add the ThreadSafeDevToolsUtils.flatten utility; r=jimb a=kwierso The `flatten` function takes an array of arrays and flattens them to a single array, removing one level of nesting. It does not recursively flatten multiple levels of nesting. MozReview-Commit-ID: ErDMHbT2IX
devtools/shared/ThreadSafeDevToolsUtils.js
devtools/shared/tests/unit/test_flatten.js
devtools/shared/tests/unit/xpcshell.ini
--- a/devtools/shared/ThreadSafeDevToolsUtils.js
+++ b/devtools/shared/ThreadSafeDevToolsUtils.js
@@ -238,8 +238,19 @@ exports.isPromise = function (p) {
 };
 
 /**
  * Return true if `thing` is a SavedFrame, false otherwise.
  */
 exports.isSavedFrame = function (thing) {
   return Object.prototype.toString.call(thing) === "[object SavedFrame]";
 };
+
+/**
+ * Given a list of lists, flatten it. Only flattens one level; does not
+ * recursively flatten all levels.
+ *
+ * @param {Array<Array<Any>>} lists
+ * @return {Array<Any>}
+ */
+exports.flatten = function(lists) {
+  return Array.prototype.concat.apply([], lists);
+};
new file mode 100644
--- /dev/null
+++ b/devtools/shared/tests/unit/test_flatten.js
@@ -0,0 +1,24 @@
+/* -*- js-indent-level: 2; indent-tabs-mode: nil -*- */
+/* Any copyright is dedicated to the Public Domain.
+   http://creativecommons.org/publicdomain/zero/1.0/ */
+
+// Test ThreadSafeDevToolsUtils.flatten
+
+function run_test() {
+  const { flatten } = DevToolsUtils;
+
+  const flat = flatten([["a", "b", "c"],
+                        ["d", "e", "f"],
+                        ["g", "h", "i"]]);
+
+  equal(flat.length, 9);
+  equal(flat[0], "a");
+  equal(flat[1], "b");
+  equal(flat[2], "c");
+  equal(flat[3], "d");
+  equal(flat[4], "e");
+  equal(flat[5], "f");
+  equal(flat[6], "g");
+  equal(flat[7], "h");
+  equal(flat[8], "i");
+}
--- a/devtools/shared/tests/unit/xpcshell.ini
+++ b/devtools/shared/tests/unit/xpcshell.ini
@@ -7,16 +7,17 @@ skip-if = toolkit == 'android' || toolki
 support-files =
   exposeLoader.js
 
 [test_assert.js]
 [test_fetch-chrome.js]
 [test_fetch-file.js]
 [test_fetch-http.js]
 [test_fetch-resource.js]
+[test_flatten.js]
 [test_indentation.js]
 [test_independent_loaders.js]
 [test_invisible_loader.js]
 [test_safeErrorString.js]
 [test_defineLazyPrototypeGetter.js]
 [test_async-utils.js]
 [test_consoleID.js]
 [test_cssAngle.js]