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

--HG--
extra : histedit_source : eb6e4f8c54f536c84b08da22ef60cfa56a5075c8
This commit is contained in:
Nick Fitzgerald 2016-03-31 16:18:44 -07:00
Родитель ff953effdf
Коммит 546829d13e
3 изменённых файлов: 36 добавлений и 0 удалений

Просмотреть файл

@ -243,3 +243,14 @@ exports.isPromise = function (p) {
exports.isSavedFrame = function (thing) { exports.isSavedFrame = function (thing) {
return Object.prototype.toString.call(thing) === "[object SavedFrame]"; 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);
};

Просмотреть файл

@ -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");
}

Просмотреть файл

@ -12,6 +12,7 @@ support-files =
[test_fetch-file.js] [test_fetch-file.js]
[test_fetch-http.js] [test_fetch-http.js]
[test_fetch-resource.js] [test_fetch-resource.js]
[test_flatten.js]
[test_indentation.js] [test_indentation.js]
[test_independent_loaders.js] [test_independent_loaders.js]
[test_invisible_loader.js] [test_invisible_loader.js]