1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2026-02-13 18:05:46 +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

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