1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2025-12-23 05:57:58 +01:00

move array utility methods to util.Array

This commit is contained in:
Doug Kearns
2008-10-19 07:26:17 +00:00
parent ca28d52ed4
commit d14a32afa3
9 changed files with 52 additions and 63 deletions

View File

@@ -52,7 +52,7 @@ function Buffer() //{{{
120, 150, 200, 300, 500, 1000, 2000 ]; 120, 150, 200, 300, 500, 1000, 2000 ];
const util = modules.util; const util = modules.util;
const arrayIter = util.arrayIter; const arrayIter = util.Array.iterator;
function Styles(name, store, serial) function Styles(name, store, serial)
{ {
@@ -268,7 +268,7 @@ function Buffer() //{{{
} }
} }
Styles.prototype = { Styles.prototype = {
get sites() util.uniq(util.flatten([v.sites for ([k, v] in this.userSheets)])) get sites() util.Array.uniq(util.Array.flatten([v.sites for ([k, v] in this.userSheets)]))
}; };
let styles = storage.newObject("styles", Styles, false); let styles = storage.newObject("styles", Styles, false);

View File

@@ -234,9 +234,8 @@ function Commands() //{{{
__iterator__: function () __iterator__: function ()
{ {
var sorted = exCommands.sort(function (cmd1, cmd2) cmd1.name > cmd2.name); let sorted = exCommands.sort(function (a, b) a.name > b.name);
for (let i = 0; i < sorted.length; i++) return util.Array.iterator(sorted);
yield sorted[i];
}, },
add: function (names, description, action, extra) add: function (names, description, action, extra)

View File

@@ -1118,7 +1118,7 @@ function Completion() //{{{
var cpt = complete || options["complete"]; var cpt = complete || options["complete"];
var suggestEngineAlias = options["suggestengines"] || "google"; var suggestEngineAlias = options["suggestengines"] || "google";
// join all completion arrays together // join all completion arrays together
for (let c in util.arrayIter(cpt)) for (let c in util.Array.iterator(cpt))
{ {
if (c == "s") if (c == "s")
completions.push(this.search(filter)[1]); completions.push(this.search(filter)[1]);
@@ -1144,7 +1144,7 @@ function Completion() //{{{
} }
} }
completionCache = util.flatten(completions); completionCache = util.Array.flatten(completions);
return [start, completionCache.concat(historyCache)]; return [start, completionCache.concat(historyCache)];
}, },

View File

@@ -173,11 +173,7 @@ function AutoCommands() //{{{
return { return {
__iterator__: function () __iterator__: function () util.Array.iterator(store),
{
for (let i = 0; i < store.length; i++)
yield autoCmd[i];
},
add: function (events, regex, cmd) add: function (events, regex, cmd)
{ {

View File

@@ -278,16 +278,10 @@ function Mappings() //{{{
return { return {
// NOTE: just normal mode for now // NOTE: just normal mode for now
__iterator__: function () __iterator__: function () mappingsIterator([modes.NORMAL], main),
{
return mappingsIterator([modes.NORMAL], main);
},
// used by :mkvimperatorrc to save mappings // used by :mkvimperatorrc to save mappings
getUserIterator: function (mode) getUserIterator: function (mode) mappingsIterator(mode, user),
{
return mappingsIterator(mode, user);
},
add: function (modes, keys, description, action, extra) add: function (modes, keys, description, action, extra)
{ {

View File

@@ -183,7 +183,7 @@ const modes = (function () //{{{
RECORDING: 1 << 21, RECORDING: 1 << 21,
PROMPT: 1 << 22, PROMPT: 1 << 22,
__iterator__: function () util.arrayIter(this.all), __iterator__: function () util.Array.iterator(this.all),
get all() [this.NONE, this.NORMAL, this.INSERT, this.VISUAL, get all() [this.NONE, this.NORMAL, this.INSERT, this.VISUAL,
this.HINTS, this.COMMAND_LINE, this.CARET, this.HINTS, this.COMMAND_LINE, this.CARET,

View File

@@ -624,11 +624,11 @@ function Options() //{{{
switch (opt.operator) switch (opt.operator)
{ {
case "+": case "+":
newValue = util.uniq(Array.concat(opt.optionHas, opt.valueHas), true); newValue = util.Array.uniq(Array.concat(opt.optionHas, opt.valueHas), true);
break; break;
case "^": case "^":
// NOTE: Vim doesn't prepend if there's a match in the current value // NOTE: Vim doesn't prepend if there's a match in the current value
newValue = util.uniq(Array.concat(opt.valueHas, opt.optionHas), true); newValue = util.Array.uniq(Array.concat(opt.valueHas, opt.optionHas), true);
break; break;
case "-": case "-":
newValue = opt.optionHas.filter(function (item) opt.valueHas.indexOf(item) == -1); newValue = opt.optionHas.filter(function (item) opt.valueHas.indexOf(item) == -1);
@@ -735,8 +735,7 @@ function Options() //{{{
optionCompletions.push([[prefix + name, option.description] optionCompletions.push([[prefix + name, option.description]
for each (name in option.names) for each (name in option.names)
if (name.indexOf(filter) == 0)]); if (name.indexOf(filter) == 0)]);
// Flatten array. optionCompletions = util.Array.flatten(optionCompletions);
optionCompletions = Array.concat.apply(Array, optionCompletions);
return [0, completion.filter(optionCompletions, prefix + filter, true)]; return [0, completion.filter(optionCompletions, prefix + filter, true)];
} }

View File

@@ -5,7 +5,7 @@ const template = {
map: function (iter, fn, sep) map: function (iter, fn, sep)
{ {
if (iter.length) /* Kludge? */ if (iter.length) /* Kludge? */
iter = util.arrayIter(iter); iter = util.Array.iterator(iter);
let ret = <></>; let ret = <></>;
let n = 0; let n = 0;
for each (let i in Iterator(iter)) for each (let i in Iterator(iter))

View File

@@ -28,6 +28,44 @@ the terms of any one of the MPL, the GPL or the LGPL.
const util = { //{{{ 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) Timer: function Timer(minInterval, maxInterval, callback)
{ {
let timer = Components.classes["@mozilla.org/timer;1"] 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) ciCompare: function (a, b)
{ {
return String.localeCompare(a.toLowerCase(), b.toLowerCase()); 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; 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) formatBytes: function (num, decimalPlaces, humanReadable)
{ {
const unitVal = ["Bytes", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"]; const unitVal = ["Bytes", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"];
@@ -399,27 +421,6 @@ const util = { //{{{
return urls; 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) xmlToDom: function (node, doc)
{ {
XML.prettyPrinting = false; XML.prettyPrinting = false;