diff --git a/common/content/abbreviations.js b/common/content/abbreviations.js index c1c8751c..1c81b1d5 100644 --- a/common/content/abbreviations.js +++ b/common/content/abbreviations.js @@ -105,7 +105,7 @@ var AbbrevHive = Class("AbbrevHive", Contexts.Hive, { }, /** @property {boolean} True if there are no abbreviations. */ - get empty() !values(this._store).nth(util.identity, 0), + get empty() !values(this._store).find(util.identity), /** * Adds a new abbreviation. @@ -251,7 +251,7 @@ var Abbreviations = Module("abbreviations", { let match = this._match.exec(text); if (match) return this.hives.map(h => h.get(mode, match[2] || match[4] || match[6])) - .nth(util.identity, 0); + .find(util.identity); return null; }, diff --git a/common/content/commandline.js b/common/content/commandline.js index dc9e6586..afbca3f9 100644 --- a/common/content/commandline.js +++ b/common/content/commandline.js @@ -2018,7 +2018,7 @@ var ItemList = Class("ItemList", { if (start < 0 || start >= this.itemCount) return null; - group = array.nth(groups, g => let (i = start - g.offsets.start) i >= 0 && i < g.itemCount, 0); + group = groups.find(g => let (i = start - g.offsets.start) i >= 0 && i < g.itemCount); return [group.context, start - group.offsets.start]; }, diff --git a/common/content/hints.js b/common/content/hints.js index 79a6fd62..30d72724 100644 --- a/common/content/hints.js +++ b/common/content/hints.js @@ -1299,16 +1299,21 @@ var Hints = Module("hints", { }, { keepQuotes: true, + getKey: function (val, default_) - let (res = array.nth(this.value, re => let (match = re.exec(val)) match && match[0] == val, 0)) - res ? res.matcher : default_, + let (res = this.value.find(re => let (match = re.exec(val)) match && match[0] == val)) + res ? res.matcher + : default_, + parse: function parse(val) { let vals = parse.supercall(this, val); for (let value in values(vals)) value.matcher = DOM.compileMatcher(Option.splitList(value.result)); return vals; }, + testValues: function testValues(vals, validator) vals.every(re => Option.splitList(re).every(validator)), + validator: DOM.validateMatcher }); diff --git a/common/content/modes.js b/common/content/modes.js index a6183522..1e35c4a9 100644 --- a/common/content/modes.js +++ b/common/content/modes.js @@ -274,7 +274,8 @@ var Modes = Module("modes", { remove: function remove(mode, covert) { if (covert && this.topOfStack.main != mode) { util.assert(mode != this.NORMAL); - for (let m; m = array.nth(this.modeStack, m => m.main == mode, 0);) + + for (let m; m = this.modeStack.find(m => m.main == mode);) this._modeStack.splice(this._modeStack.indexOf(m)); } else if (this.stack.some(m => m.main == mode)) { @@ -604,7 +605,7 @@ var Modes = Module("modes", { getKey: function getKey(val, default_) { if (isArray(val)) - return (array.nth(this.value, v => val.some(m => m.name === v.mode), 0) + return (this.value.find(v => val.some(m => m.name === v.mode)) || { result: default_ }).result; return hasOwnProperty(this.valueMap, val) ? this.valueMap[val] : default_; diff --git a/common/content/tabs.js b/common/content/tabs.js index fa31e415..47a525f1 100644 --- a/common/content/tabs.js +++ b/common/content/tabs.js @@ -550,7 +550,7 @@ var Tabs = Module("tabs", { if (matches) return tabs.select(this.allTabs[parseInt(matches[1], 10) - 1], false); - matches = array.nth(tabs.allTabs, t => (t.linkedBrowser.lastURI || {}).spec === buffer, 0); + matches = tabs.allTabs.find(t => (t.linkedBrowser.lastURI || {}).spec === buffer); if (matches) return tabs.select(matches, false); diff --git a/common/modules/base.jsm b/common/modules/base.jsm index b405b66c..39844289 100644 --- a/common/modules/base.jsm +++ b/common/modules/base.jsm @@ -1508,21 +1508,21 @@ update(iter, { every: function every(iter, pred, self) { pred = pred || util.identity; - for (let elem in iter) + for (let elem of iter) if (!pred.call(self, elem)) return false; return true; }, some: function every(iter, pred, self) { pred = pred || util.identity; - for (let elem in iter) + for (let elem of iter) if (pred.call(self, elem)) return true; return false; }, filter: function filter(iter, pred, self) { - for (let elem in iter) + for (let elem of iter) if (pred.call(self, elem)) yield elem; }, @@ -1536,13 +1536,13 @@ update(iter, { * @param {object} self The this object for *fn*. */ forEach: function forEach(iter, func, self) { - for (let val in iter) + for (let val of iter) func.call(self, val); }, indexOf: function indexOf(iter, elem) { let i = 0; - for (let item in iter) { + for (let item of iter) { if (item == elem) return i; i++; @@ -1558,7 +1558,7 @@ update(iter, { * @returns {Array} */ map: function map(iter, func, self) { - for (let i in iter) + for (let i of iter) yield func.call(self, i); }, @@ -1570,18 +1570,29 @@ update(iter, { if (typeof pred === "number") [pred, n] = [() => true, pred]; // Hack. - for (let elem in iter) + for (let elem of iter) if (pred.call(self, elem) && n-- === 0) return elem; return undefined; }, + /** + * Analog of Array.find method. Returns the first item in the + * iterator for which `pred` returns true. + */ + find: function find(iter, pred, self) { + for (let elem of iter) + if (pred.call(self, elem)) + return elem; + return undefined; + }, + sort: function sort(iter, fn, self) array(this.toArray(iter).sort(fn, self)), uniq: function uniq(iter) { let seen = RealSet(); - for (let item in iter) + for (let item of iter) if (!seen.add(item)) yield item; }, diff --git a/common/modules/buffer.jsm b/common/modules/buffer.jsm index 5507cfd5..d8f0490a 100644 --- a/common/modules/buffer.jsm +++ b/common/modules/buffer.jsm @@ -2476,10 +2476,9 @@ var Buffer = Module("Buffer", { if (/^func:/.test(filter.result)) var res = dactyl.userEval("(" + Option.dequote(filter.result.substr(5)) + ")")(doc, line); else - res = iter.nth(filter.matcher(doc), - elem => ((elem.nodeValue || elem.textContent).trim() == line && - DOM(elem).display != "none"), - 0) + res = iter.find(filter.matcher(doc), + elem => ((elem.nodeValue || elem.textContent).trim() == line && + DOM(elem).display != "none")) || iter.nth(filter.matcher(doc), util.identity, line - 1); if (res) break; diff --git a/common/modules/commands.jsm b/common/modules/commands.jsm index 0855fa2a..46a93d6d 100644 --- a/common/modules/commands.jsm +++ b/common/modules/commands.jsm @@ -622,11 +622,12 @@ var CommandHive = Class("CommandHive", Contexts.Hive, { */ get: function get(name, full) { let cmd = this._map[name] - || !full && array.nth(this._list, cmd => cmd.hasName(name), 0) + || !full && this._list.find(cmd => cmd.hasName(name)) || null; if (!cmd && full) { - let name = array.nth(this.specs, spec => Command.hasName(spec, name), 0); + // Hrm. This is wrong. -Kris + let name = this._specs.find(spec => Command.hasName(spec, name)); return name && this.get(name); } @@ -883,7 +884,7 @@ var Commands = Module("commands", { * @returns {Command} */ get: function get(name, full) iter(this.hives).map(([i, hive]) => hive.get(name, full)) - .nth(util.identity, 0), + .find(util.identity), /** * Returns true if a command invocation contains a URL referring to the diff --git a/common/modules/config.jsm b/common/modules/config.jsm index bd5d37be..5b252a62 100644 --- a/common/modules/config.jsm +++ b/common/modules/config.jsm @@ -243,7 +243,7 @@ var ConfigBase = Class("ConfigBase", { bestLocale: function (list) { return values([this.appLocale, this.appLocale.replace(/-.*/, ""), "en", "en-US", list[0]]) - .nth((function (l) this.has(l)).bind(RealSet(list)), 0); + .find(bind("has", RealSet(list))); }, /** diff --git a/common/modules/contexts.jsm b/common/modules/contexts.jsm index f04968b3..20a7e5b9 100644 --- a/common/modules/contexts.jsm +++ b/common/modules/contexts.jsm @@ -67,8 +67,9 @@ var Group = Class("Group", { }, { compileFilter: function (patterns, default_=false) { function siteFilter(uri) - let (match = array.nth(siteFilter.filters, f => f(uri), 0)) - match ? match.result : default_; + let (match = siteFilter.filters.find(f => f(uri))) + match ? match.result + : default_; return update(siteFilter, { toString: function () this.filters.join(","), @@ -205,12 +206,10 @@ var Contexts = Module("contexts", { Context: function Context(file, group, args) { const { contexts, io, newContext, plugins, userContext } = this.modules; - let isPlugin = array.nth(io.getRuntimeDirectories("plugins"), - dir => dir.contains(file, true), - 0); - let isRuntime = array.nth(io.getRuntimeDirectories(""), - dir => dir.contains(file, true), - 0); + let isPlugin = io.getRuntimeDirectories("plugins") + .find(dir => dir.contains(file, true)); + let isRuntime = io.getRuntimeDirectories("") + .find(dir => dir.contains(file, true)); let name = isPlugin ? file.getRelativeDescriptor(isPlugin).replace(File.PATH_SEP, "-") : file.leafName; @@ -308,9 +307,8 @@ var Contexts = Module("contexts", { if (uri instanceof Ci.nsIFileURL) var file = File(uri.file); - let isPlugin = array.nth(io.getRuntimeDirectories("plugins"), - dir => dir.contains(file, true), - 0); + let isPlugin = io.getRuntimeDirectories("plugins") + .find(dir => dir.contains(file, true)); let name = isPlugin && file && file.getRelativeDescriptor(isPlugin) .replace(File.PATH_SEP, "-"); diff --git a/common/modules/downloads.jsm b/common/modules/downloads.jsm index 262adbba..9341e5b1 100644 --- a/common/modules/downloads.jsm +++ b/common/modules/downloads.jsm @@ -157,7 +157,7 @@ var Download = Class("Download", { let val = this._compare[order.substr(1)](this, other); return (order[0] == "-") ? -val : val; - }, this).nth(util.identity, 0) || 0, + }, this).find(util.identity) || 0, timeRemaining: Infinity, diff --git a/common/modules/options.jsm b/common/modules/options.jsm index 54bb1138..c9626813 100644 --- a/common/modules/options.jsm +++ b/common/modules/options.jsm @@ -1504,8 +1504,7 @@ var Options = Module("options", { function val(obj) { if (isArray(opt.defaultValue)) { - let val = array.nth(obj, re => (re.key == extra.key), - 0); + let val = Array.find(obj, re => (re.key == extra.key)); return val && val.result; } if (hasOwnProperty(opt.defaultValue, extra.key)) diff --git a/common/modules/sanitizer.jsm b/common/modules/sanitizer.jsm index 8d966d36..dbb27894 100644 --- a/common/modules/sanitizer.jsm +++ b/common/modules/sanitizer.jsm @@ -474,7 +474,10 @@ var Sanitizer = Module("sanitizer", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakRef sanitizer.sanitize(items, range); } - if (array.nth(opt.value, i => i == "all" || /^!/.test(i), 0) == "all" && !args["-host"]) + if ("all" == opt.value.find(i => (i == "all" || + /^!/.test(i))) + && !args["-host"]) + modules.commandline.input(_("sanitize.prompt.deleteAll") + " ", function (resp) { if (resp.match(/^y(es)?$/i)) { @@ -620,8 +623,7 @@ var Sanitizer = Module("sanitizer", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakRef }, has: function has(val) - let (res = array.nth(this.value, v => (v == "all" || v.replace(/^!/, "") == val), - 0)) + let (res = this.value.find(v => (v == "all" || v.replace(/^!/, "") == val))) res && !/^!/.test(res), validator: function (values) values.length && diff --git a/common/modules/styles.jsm b/common/modules/styles.jsm index 12cf60fa..29801088 100644 --- a/common/modules/styles.jsm +++ b/common/modules/styles.jsm @@ -277,8 +277,7 @@ var Styles = Module("Styles", { }, addHive: function addHive(name, ref, persist) { - let hive = array.nth(this.hives, h => h.name === name, - 0); + let hive = this.hives.find(h => h.name === name); if (!hive) { hive = Hive(name, persist); this.hives.push(hive);