diff --git a/content/buffer.js b/content/buffer.js index a72bd78d..41b63e95 100644 --- a/content/buffer.js +++ b/content/buffer.js @@ -52,7 +52,7 @@ function Buffer() //{{{ 120, 150, 200, 300, 500, 1000, 2000 ]; const util = modules.util; - const arrayIter = util.arrayIter; + const arrayIter = util.Array.iterator; function Styles(name, store, serial) { @@ -268,7 +268,7 @@ function Buffer() //{{{ } } 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); diff --git a/content/commands.js b/content/commands.js index 69bf805e..e4193509 100644 --- a/content/commands.js +++ b/content/commands.js @@ -234,9 +234,8 @@ function Commands() //{{{ __iterator__: function () { - var sorted = exCommands.sort(function (cmd1, cmd2) cmd1.name > cmd2.name); - for (let i = 0; i < sorted.length; i++) - yield sorted[i]; + let sorted = exCommands.sort(function (a, b) a.name > b.name); + return util.Array.iterator(sorted); }, add: function (names, description, action, extra) diff --git a/content/completion.js b/content/completion.js index 9c34cc12..8b2ff7ac 100644 --- a/content/completion.js +++ b/content/completion.js @@ -1118,7 +1118,7 @@ function Completion() //{{{ var cpt = complete || options["complete"]; var suggestEngineAlias = options["suggestengines"] || "google"; // join all completion arrays together - for (let c in util.arrayIter(cpt)) + for (let c in util.Array.iterator(cpt)) { if (c == "s") 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)]; }, diff --git a/content/events.js b/content/events.js index 89a2b524..5e29e75d 100644 --- a/content/events.js +++ b/content/events.js @@ -173,11 +173,7 @@ function AutoCommands() //{{{ return { - __iterator__: function () - { - for (let i = 0; i < store.length; i++) - yield autoCmd[i]; - }, + __iterator__: function () util.Array.iterator(store), add: function (events, regex, cmd) { diff --git a/content/mappings.js b/content/mappings.js index ad711984..4072a931 100644 --- a/content/mappings.js +++ b/content/mappings.js @@ -278,16 +278,10 @@ function Mappings() //{{{ return { // NOTE: just normal mode for now - __iterator__: function () - { - return mappingsIterator([modes.NORMAL], main); - }, + __iterator__: function () mappingsIterator([modes.NORMAL], main), // used by :mkvimperatorrc to save mappings - getUserIterator: function (mode) - { - return mappingsIterator(mode, user); - }, + getUserIterator: function (mode) mappingsIterator(mode, user), add: function (modes, keys, description, action, extra) { diff --git a/content/modes.js b/content/modes.js index 6d07e472..50fddaa5 100644 --- a/content/modes.js +++ b/content/modes.js @@ -183,7 +183,7 @@ const modes = (function () //{{{ RECORDING: 1 << 21, 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, this.HINTS, this.COMMAND_LINE, this.CARET, diff --git a/content/options.js b/content/options.js index fb2ffb26..072dae14 100644 --- a/content/options.js +++ b/content/options.js @@ -624,11 +624,11 @@ function Options() //{{{ switch (opt.operator) { case "+": - newValue = util.uniq(Array.concat(opt.optionHas, opt.valueHas), true); + newValue = util.Array.uniq(Array.concat(opt.optionHas, opt.valueHas), true); break; case "^": // 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; case "-": newValue = opt.optionHas.filter(function (item) opt.valueHas.indexOf(item) == -1); @@ -735,8 +735,7 @@ function Options() //{{{ optionCompletions.push([[prefix + name, option.description] for each (name in option.names) if (name.indexOf(filter) == 0)]); - // Flatten array. - optionCompletions = Array.concat.apply(Array, optionCompletions); + optionCompletions = util.Array.flatten(optionCompletions); return [0, completion.filter(optionCompletions, prefix + filter, true)]; } diff --git a/content/template.js b/content/template.js index 295d0fd7..a59d7053 100644 --- a/content/template.js +++ b/content/template.js @@ -5,7 +5,7 @@ const template = { map: function (iter, fn, sep) { if (iter.length) /* Kludge? */ - iter = util.arrayIter(iter); + iter = util.Array.iterator(iter); let ret = <>; let n = 0; for each (let i in Iterator(iter)) diff --git a/content/util.js b/content/util.js index 26255779..77faafe6 100644 --- a/content/util.js +++ b/content/util.js @@ -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;