1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2025-12-22 17:27:57 +01:00

Use builtin Array.find rather than array.nth where applicable.

This commit is contained in:
Kris Maglione
2014-02-22 14:57:23 -08:00
parent 414a165e9c
commit 6790c62c41
14 changed files with 58 additions and 43 deletions

View File

@@ -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;
},

View File

@@ -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];
},

View File

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

View File

@@ -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_;

View File

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

View File

@@ -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;
},

View File

@@ -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;

View File

@@ -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

View File

@@ -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)));
},
/**

View File

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

View File

@@ -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,

View File

@@ -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))

View File

@@ -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 &&

View File

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