mirror of
https://github.com/gryf/pentadactyl-pm.git
synced 2026-02-14 23:25:45 +01:00
move array utility methods to util.Array
This commit is contained in:
@@ -28,6 +28,44 @@ the terms of any one of the MPL, the GPL or the LGPL.
|
||||
|
||||
const util = { //{{{
|
||||
|
||||
Array: {
|
||||
// flatten an array: [["foo", "bar"], ["baz"]] -> ["foo", "bar", "baz"]
|
||||
flatten: function (ary)
|
||||
{
|
||||
if (ary.length == 0)
|
||||
return [];
|
||||
return Array.concat.apply(Array, ary);
|
||||
},
|
||||
|
||||
iterator: function (ary)
|
||||
{
|
||||
let length = ary.length;
|
||||
for (let i = 0; i < length; i++)
|
||||
yield ary[i];
|
||||
},
|
||||
|
||||
uniq: function (ary, unsorted)
|
||||
{
|
||||
let ret = [];
|
||||
if (unsorted)
|
||||
{
|
||||
for (let [, item] in Iterator(ary))
|
||||
if (ret.indexOf(item) == -1)
|
||||
ret.push(item);
|
||||
}
|
||||
else
|
||||
{
|
||||
for (let [,item] in Iterator(ary.sort()))
|
||||
{
|
||||
if (item != last || !ret.length)
|
||||
ret.push(item);
|
||||
var last = item;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
},
|
||||
|
||||
Timer: function Timer(minInterval, maxInterval, callback)
|
||||
{
|
||||
let timer = Components.classes["@mozilla.org/timer;1"]
|
||||
@@ -80,13 +118,6 @@ const util = { //{{{
|
||||
}
|
||||
},
|
||||
|
||||
arrayIter: function (ary)
|
||||
{
|
||||
let length = ary.length;
|
||||
for (let i = 0; i < length; i++)
|
||||
yield ary[i];
|
||||
},
|
||||
|
||||
ciCompare: function (a, b)
|
||||
{
|
||||
return String.localeCompare(a.toLowerCase(), b.toLowerCase());
|
||||
@@ -137,15 +168,6 @@ const util = { //{{{
|
||||
return delimiter + str.replace(/([\\'"])/g, "\\$1").replace("\n", "\\n").replace("\t", "\\t") + delimiter;
|
||||
},
|
||||
|
||||
// Flatten an array:
|
||||
// [["foo", "bar"], ["baz"]] -> ["foo", "bar", "baz"]
|
||||
flatten: function (ary)
|
||||
{
|
||||
if (ary.length == 0)
|
||||
return [];
|
||||
return Array.concat.apply(Array, ary);
|
||||
},
|
||||
|
||||
formatBytes: function (num, decimalPlaces, humanReadable)
|
||||
{
|
||||
const unitVal = ["Bytes", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"];
|
||||
@@ -399,27 +421,6 @@ const util = { //{{{
|
||||
return urls;
|
||||
},
|
||||
|
||||
uniq: function (ary, unsorted)
|
||||
{
|
||||
let ret = [];
|
||||
if (unsorted)
|
||||
{
|
||||
for (let [, item] in Iterator(ary))
|
||||
if (ret.indexOf(item) == -1)
|
||||
ret.push(item);
|
||||
}
|
||||
else
|
||||
{
|
||||
for (let [,item] in Iterator(ary.sort()))
|
||||
{
|
||||
if (item != last || !ret.length)
|
||||
ret.push(item);
|
||||
var last = item;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
},
|
||||
|
||||
xmlToDom: function (node, doc)
|
||||
{
|
||||
XML.prettyPrinting = false;
|
||||
|
||||
Reference in New Issue
Block a user