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

Use builtin String.startsWith, String.endsWith, and String.contains methods where appropriate.

This commit is contained in:
Kris Maglione
2014-02-22 15:17:08 -08:00
parent 6790c62c41
commit 51eb03c376
19 changed files with 40 additions and 39 deletions

View File

@@ -267,7 +267,7 @@ var Abbreviations = Module("abbreviations", {
let hives = (hives || this.userHives).filter(h => !h.empty); let hives = (hives || this.userHives).filter(h => !h.empty);
function abbrevs(hive) function abbrevs(hive)
hive.merged.filter(ab => (ab.inModes(modes) && ab.lhs.indexOf(lhs) == 0)); hive.merged.filter(ab => (ab.inModes(modes) && ab.lhs.startsWith(lhs)));
let list = ["table", {}, let list = ["table", {},
["tr", { highlight: "Title" }, ["tr", { highlight: "Title" },

View File

@@ -656,7 +656,7 @@ var Bookmarks = Module("bookmarks", {
keyword, true); keyword, true);
let item = keywords[keyword]; let item = keywords[keyword];
if (item && item.url.indexOf("%s") > -1) if (item && item.url.contains("%s"))
context.fork("keyword/" + keyword, keyword.length + space.length, null, function (context) { context.fork("keyword/" + keyword, keyword.length + space.length, null, function (context) {
context.format = history.format; context.format = history.format;
context.title = [/*L*/keyword + " Quick Search"]; context.title = [/*L*/keyword + " Quick Search"];
@@ -669,7 +669,7 @@ var Bookmarks = Module("bookmarks", {
return history.get({ uri: util.newURI(begin), uriIsPrefix: true }).map(function (item) { return history.get({ uri: util.newURI(begin), uriIsPrefix: true }).map(function (item) {
let rest = item.url.length - end.length; let rest = item.url.length - end.length;
let query = item.url.substring(begin.length, rest); let query = item.url.substring(begin.length, rest);
if (item.url.substr(rest) == end && query.indexOf("&") == -1) if (item.url.substr(rest) == end && query.contains("&"))
try { try {
item.url = decodeURIComponent(query.replace(/#.*/, "").replace(/\+/g, " ")); item.url = decodeURIComponent(query.replace(/#.*/, "").replace(/\+/g, " "));
return item; return item;
@@ -718,14 +718,14 @@ var Bookmarks = Module("bookmarks", {
return; return;
let words = ctxt.filter.toLowerCase().split(/\s+/g); let words = ctxt.filter.toLowerCase().split(/\s+/g);
ctxt.completions = ctxt.completions.filter(i => words.every(w => i.toLowerCase().indexOf(w) >= 0)); ctxt.completions = ctxt.completions.filter(i => words.every(w => i.toLowerCase().contains(w)));
ctxt.hasItems = ctxt.completions.length; ctxt.hasItems = ctxt.completions.length;
ctxt.incomplete = true; ctxt.incomplete = true;
ctxt.cache.request = bookmarks.getSuggestions(name, ctxt.filter); ctxt.cache.request = bookmarks.getSuggestions(name, ctxt.filter);
ctxt.cache.request.then(function (compl) { ctxt.cache.request.then(function (compl) {
ctxt.incomplete = false; ctxt.incomplete = false;
ctxt.completions = array.uniq(ctxt.completions.filter(c => compl.indexOf(c) >= 0) ctxt.completions = array.uniq(ctxt.completions.filter(c => compl.contains(c))
.concat(compl), true); .concat(compl), true);
}, Cu.reportError); }, Cu.reportError);
}); });

View File

@@ -136,14 +136,14 @@ var Dactyl = Module("dactyl", XPCOM(Ci.nsISupportsWeakReference, ModuleBase), {
&& !item.hidden && !item.hidden
&& !/rdf:http:/.test(item.getAttribute("label"))) { // FIXME && !/rdf:http:/.test(item.getAttribute("label"))) { // FIXME
item.dactylPath = parent + item.getAttribute("label"); item.dactylPath = parent + item.getAttribute("label");
if (!targetPath || targetPath.indexOf(item.dactylPath) == 0) if (!targetPath || targetPath.startsWith(item.dactylPath))
items.push(item); items.push(item);
} }
else { else {
let path = parent; let path = parent;
if (item.localName == "menu") if (item.localName == "menu")
path += item.getAttribute("label") + "."; path += item.getAttribute("label") + ".";
if (!targetPath || targetPath.indexOf(path) == 0) if (!targetPath || targetPath.startsWith(path))
addChildren(item, path); addChildren(item, path);
} }
} }
@@ -459,7 +459,7 @@ var Dactyl = Module("dactyl", XPCOM(Ci.nsISupportsWeakReference, ModuleBase), {
*/ */
loadScript: function loadScript(uri, context) { loadScript: function loadScript(uri, context) {
let prefix = "literal:" + uri + ":"; let prefix = "literal:" + uri + ":";
cache.flush(s => s.indexOf(prefix) == 0); cache.flush(s => s.startsWith(prefix));
delete literal.files[uri]; delete literal.files[uri];
JSMLoader.loadSubScript(uri, context, File.defaultEncoding); JSMLoader.loadSubScript(uri, context, File.defaultEncoding);
}, },
@@ -1147,7 +1147,7 @@ var Dactyl = Module("dactyl", XPCOM(Ci.nsISupportsWeakReference, ModuleBase), {
if (error instanceof FailedAssertion && error.noTrace || error.message === "Interrupted") { if (error instanceof FailedAssertion && error.noTrace || error.message === "Interrupted") {
let context = contexts.context; let context = contexts.context;
let prefix = context ? context.file + ":" + context.line + ": " : ""; let prefix = context ? context.file + ":" + context.line + ": " : "";
if (error.message && error.message.indexOf(prefix) !== 0 && if (error.message && !error.message.startsWith(prefix) &&
prefix != "[Command Line]:1: ") prefix != "[Command Line]:1: ")
error.message = prefix + error.message; error.message = prefix + error.message;

View File

@@ -764,7 +764,7 @@ var Editor = Module("editor", XPCOM(Ci.nsIEditActionListener, ModuleBase), {
context = context.fork("registers"); context = context.fork("registers");
context.keys = { text: util.identity, description: editor.closure.getRegister }; context.keys = { text: util.identity, description: editor.closure.getRegister };
context.match = function (r) !this.filter || ~this.filter.indexOf(r); context.match = function (r) !this.filter || this.filter.contains(r);
context.fork("clipboard", 0, this, function (ctxt) { context.fork("clipboard", 0, this, function (ctxt) {
ctxt.match = context.match; ctxt.match = context.match;

View File

@@ -695,7 +695,7 @@ var HintSession = Class("HintSession", CommandMode, {
updateValidNumbers: function updateValidNumbers(always) { updateValidNumbers: function updateValidNumbers(always) {
let string = this.getHintString(this.hintNumber); let string = this.getHintString(this.hintNumber);
for (let hint in values(this.validHints)) for (let hint in values(this.validHints))
hint.valid = always || hint.span.getAttribute("number").indexOf(string) == 0; hint.valid = always || hint.span.getAttribute("number").startsWith(string);
}, },
tab: function tab(previous) { tab: function tab(previous) {
@@ -1071,7 +1071,7 @@ var Hints = Module("hints", {
mappings.popCommand(); mappings.popCommand();
}, },
onChange: function (arg) { onChange: function (arg) {
if (Object.keys(hints.modes).some(m => m != arg && m.indexOf(arg) == 0)) if (Object.keys(hints.modes).some(m => m != arg && m.startsWith(arg)))
return; return;
this.accepted = true; this.accepted = true;

View File

@@ -714,6 +714,7 @@ var Mappings = Module("mappings", {
function uniqueModes(modes) { function uniqueModes(modes) {
let chars = [k for ([k, v] in Iterator(modules.modes.modeChars)) let chars = [k for ([k, v] in Iterator(modules.modes.modeChars))
if (v.every(mode => modes.indexOf(mode) >= 0))]; if (v.every(mode => modes.indexOf(mode) >= 0))];
return array.uniq(modes.filter(m => chars.indexOf(m.char) < 0) return array.uniq(modes.filter(m => chars.indexOf(m.char) < 0)
.map(m => m.name.toLowerCase()) .map(m => m.name.toLowerCase())
.concat(chars)); .concat(chars));

View File

@@ -1152,7 +1152,7 @@ var Buffer = Module("Buffer", {
else { else {
let url = loc || doc.location.href; let url = loc || doc.location.href;
const PREFIX = "view-source:"; const PREFIX = "view-source:";
if (url.indexOf(PREFIX) == 0) if (url.startsWith(PREFIX))
url = url.substr(PREFIX.length); url = url.substr(PREFIX.length);
else else
url = PREFIX + url; url = PREFIX + url;

View File

@@ -62,7 +62,7 @@ var Cache = Module("Cache", XPCOM(Ci.nsIRequestObserver), {
}), }),
parse: function parse(str) { parse: function parse(str) {
if (~'{['.indexOf(str[0])) if ('{['.contains(str[0]))
return JSON.parse(str); return JSON.parse(str);
return str; return str;
}, },

View File

@@ -764,7 +764,7 @@ var Commands = Module("commands", {
return ""; return "";
} }
// TODO: allow matching of aliases? // TODO: allow matching of aliases?
function cmds(hive) hive._list.filter(cmd => cmd.name.indexOf(filter || "") == 0) function cmds(hive) hive._list.filter(cmd => cmd.name.startsWith(filter || ""))
let hives = (hives || this.userHives).map(h => [h, cmds(h)]) let hives = (hives || this.userHives).map(h => [h, cmds(h)])
.filter(([h, c]) => c.length); .filter(([h, c]) => c.length);
@@ -1067,7 +1067,7 @@ var Commands = Module("commands", {
if (!onlyArgumentsRemaining) { if (!onlyArgumentsRemaining) {
for (let [, opt] in Iterator(options)) { for (let [, opt] in Iterator(options)) {
for (let [, optname] in Iterator(opt.names)) { for (let [, optname] in Iterator(opt.names)) {
if (sub.indexOf(optname) == 0) { if (sub.startsWith(optname)) {
let count = 0; let count = 0;
let invalid = false; let invalid = false;
let arg, quote, quoted; let arg, quote, quoted;

View File

@@ -512,7 +512,7 @@ var CompletionContext = Class("CompletionContext", {
filtered.sort(this.compare); filtered.sort(this.compare);
if (!this.anchored) { if (!this.anchored) {
let filter = this.filter; let filter = this.filter;
filtered.sort(function s(a, b) (b.text.indexOf(filter) == 0) - (a.text.indexOf(filter) == 0)); filtered.sort(function s(a, b) b.text.startsWith(filter) - a.text.startsWith(filter));
} }
} }
@@ -549,7 +549,7 @@ var CompletionContext = Class("CompletionContext", {
var substrings = [text]; var substrings = [text];
} }
else { else {
var compare = function compare(text, s) text.indexOf(s) >= 0; var compare = function compare(text, s) text.contains(s);
var substrings = []; var substrings = [];
let start = 0; let start = 0;
let idx; let idx;
@@ -970,7 +970,7 @@ var Completion = Module("completion", {
context.generate = function generate_() { context.generate = function generate_() {
return [[k.substr(services.ABOUT.length), ""] return [[k.substr(services.ABOUT.length), ""]
for (k in Cc) for (k in Cc)
if (k.indexOf(services.ABOUT) == 0)]; if (k.startsWith(services.ABOUT))];
}; };
}); });
@@ -1056,7 +1056,7 @@ var Completion = Module("completion", {
let contains = String.indexOf; let contains = String.indexOf;
if (context.ignoreCase) { if (context.ignoreCase) {
compare = util.compareIgnoreCase; compare = util.compareIgnoreCase;
contains = function contains_(a, b) a && a.toLowerCase().indexOf(b.toLowerCase()) > -1; contains = function contains_(a, b) a && a.toLowerCase().contains(b.toLowerCase());
} }
if (tags) if (tags)
@@ -1180,7 +1180,7 @@ var Completion = Module("completion", {
.concat([let (name = k.substr(services.AUTOCOMPLETE.length)) .concat([let (name = k.substr(services.AUTOCOMPLETE.length))
["native:" + name, _("autocomplete.description", name)] ["native:" + name, _("autocomplete.description", name)]
for (k in Cc) for (k in Cc)
if (k.indexOf(services.AUTOCOMPLETE) == 0)]), if (k.startsWith(services.AUTOCOMPLETE))]),
setter: function setter(values) { setter: function setter(values) {
if (values.length == 1 && !hasOwnProperty(values[0], this.values) if (values.length == 1 && !hasOwnProperty(values[0], this.values)

View File

@@ -20,7 +20,7 @@ var MAX_LOAD_TIME = 10 * 1000;
let prefix = "DOWNLOAD_"; let prefix = "DOWNLOAD_";
var states = iter([v, k.slice(prefix.length).toLowerCase()] var states = iter([v, k.slice(prefix.length).toLowerCase()]
for ([k, v] in Iterator(Ci.nsIDownloadManager)) for ([k, v] in Iterator(Ci.nsIDownloadManager))
if (k.indexOf(prefix) == 0)) if (k.startsWith(prefix)))
.toObject(); .toObject();
var Download = Class("Download", { var Download = Class("Download", {
@@ -288,7 +288,7 @@ var DownloadList = Class("DownloadList",
addDownload: function addDownload(download) { addDownload: function addDownload(download) {
if (!this.downloads.has(download)) { if (!this.downloads.has(download)) {
download = Download(download, this); download = Download(download, this);
if (this.filter && download.displayName.indexOf(this.filter) === -1) if (this.filter && !download.displayName.contains(this.filter))
return; return;
this.downloads.set(download.download, download); this.downloads.set(download.download, download);

View File

@@ -1,4 +1,4 @@
// Copyright (c) 2008-2013 Kris Maglione <maglione.k@gmail.com> // Copyright (c) 2008-2014 Kris Maglione <maglione.k@gmail.com>
// //
// This work is licensed for reuse under an MIT license. Details are // This work is licensed for reuse under an MIT license. Details are
// given in the LICENSE.txt file included with this file. // given in the LICENSE.txt file included with this file.
@@ -626,7 +626,7 @@ var RangeFind = Class("RangeFind", {
if (!this.matchCase) if (!this.matchCase)
pattern = pattern.toLowerCase(); pattern = pattern.toLowerCase();
if (!again && (pattern === "" || pattern.indexOf(this.lastString) !== 0 || this.backward)) { if (!again && (pattern === "" || !pattern.startsWith(this.lastString) || this.backward)) {
if (!private_) if (!private_)
this.range.deselect(); this.range.deselect();
if (pattern === "") if (pattern === "")

View File

@@ -325,7 +325,7 @@ var Help = Module("Help", {
} }
if (name == "href") { if (name == "href") {
value = node.href || value; value = node.href || value;
if (value.indexOf("dactyl://help-tag/") == 0) { if (value.startsWith("dactyl://help-tag/")) {
try { try {
let uri = services.io.newChannel(value, null, null).originalURI; let uri = services.io.newChannel(value, null, null).originalURI;
value = uri.spec == value ? "javascript:;" : uri.path.substr(1); value = uri.spec == value ? "javascript:;" : uri.path.substr(1);

View File

@@ -348,7 +348,7 @@ var JavaScript = Module("javascript", {
else { else {
base.quote = [last, text => util.escapeString(text, ""), last]; base.quote = [last, text => util.escapeString(text, ""), last];
if (prefix) if (prefix)
base.filters.push(item => item.item.indexOf(prefix) === 0); base.filters.push(item => item.item.startsWith(prefix));
} }
if (!compl) { if (!compl) {

View File

@@ -358,7 +358,7 @@ var Overlay = Module("Overlay", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReferen
delete desc.writable; delete desc.writable;
desc.get = function get() value; desc.get = function get() value;
desc.set = function set(val) { desc.set = function set(val) {
if (!callable(val) || Function.prototype.toString(val).indexOf(sentinel) < 0) if (!callable(val) || !Function.prototype.toString(val).contains(sentinel))
Class.replaceProperty(this, k, val); Class.replaceProperty(this, k, val);
else { else {
let package_ = util.newURI(Components.stack.caller.filename).host; let package_ = util.newURI(Components.stack.caller.filename).host;

View File

@@ -1,6 +1,6 @@
// Copyright (c) 2006-2008 by Martin Stubenschrott <stubenschrott@vimperator.org> // Copyright (c) 2006-2008 by Martin Stubenschrott <stubenschrott@vimperator.org>
// Copyright (c) 2007-2011 by Doug Kearns <dougkearns@gmail.com> // Copyright (c) 2007-2011 by Doug Kearns <dougkearns@gmail.com>
// Copyright (c) 2008-2013 Kris Maglione <maglione.k@gmail.com> // Copyright (c) 2008-2014 Kris Maglione <maglione.k@gmail.com>
// //
// This work is licensed for reuse under an MIT license. Details are // This work is licensed for reuse under an MIT license. Details are
// given in the LICENSE.txt file included with this file. // given in the LICENSE.txt file included with this file.
@@ -398,7 +398,7 @@ var Prefs = Module("prefs", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference])
function prefs() { function prefs() {
for (let [, pref] in Iterator(prefArray)) { for (let [, pref] in Iterator(prefArray)) {
let userValue = services.pref.prefHasUserValue(pref); let userValue = services.pref.prefHasUserValue(pref);
if (onlyNonDefault && !userValue || pref.indexOf(filter) == -1) if (onlyNonDefault && !userValue || !pref.contains(filter))
continue; continue;
let value = this.get(pref); let value = this.get(pref);

View File

@@ -399,7 +399,7 @@ var Sanitizer = Module("sanitizer", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakRef
for (let c in iter(services.cookies, Ci.nsICookie2)) for (let c in iter(services.cookies, Ci.nsICookie2))
if (!host || util.isSubdomain(c.rawHost, host) || if (!host || util.isSubdomain(c.rawHost, host) ||
c.host[0] == "." && c.host.length < host.length c.host[0] == "." && c.host.length < host.length
&& host.indexOf(c.host) == host.length - c.host.length) && host.endsWith(c.host))
yield c; yield c;
}, },
@@ -597,7 +597,7 @@ var Sanitizer = Module("sanitizer", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakRef
completion: function initCompletion(dactyl, modules, window) { completion: function initCompletion(dactyl, modules, window) {
modules.completion.visibleHosts = function completeHosts(context) { modules.completion.visibleHosts = function completeHosts(context) {
let res = util.visibleHosts(window.content); let res = util.visibleHosts(window.content);
if (context.filter && !res.some(host => host.indexOf(context.filter) >= 0)) if (context.filter && !res.some(host => host.contains(context.filter)))
res.push(context.filter); res.push(context.filter);
context.title = ["Domain"]; context.title = ["Domain"];

View File

@@ -212,7 +212,7 @@ var Hive = Class("Hive", {
name = null; name = null;
} }
if (filter && filter.indexOf(",") > -1) if (filter && filter.contains(","))
return filter.split(",").reduce( return filter.split(",").reduce(
(n, f) => n + this.removeSheet(name, f, index), 0); (n, f) => n + this.removeSheet(name, f, index), 0);

View File

@@ -482,7 +482,7 @@ var Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]),
return obj.res; return obj.res;
} }
if (pattern.indexOf("{") == -1) if (!pattern.contains("{"))
return [pattern]; return [pattern];
let res = []; let res = [];
@@ -516,7 +516,7 @@ var Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]),
rec([]); rec([]);
return res; return res;
} }
catch (e if e.message && ~e.message.indexOf("res is undefined")) { catch (e if e.message && e.message.contains("res is undefined")) {
// prefs.safeSet() would be reset on :rehash // prefs.safeSet() would be reset on :rehash
prefs.set("javascript.options.methodjit.chrome", false); prefs.set("javascript.options.methodjit.chrome", false);
util.dactyl.warn(_(UTF8("error.damnYouJägermonkey"))); util.dactyl.warn(_(UTF8("error.damnYouJägermonkey")));
@@ -544,7 +544,7 @@ var Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]),
* @returns {string} * @returns {string}
*/ */
dequote: function dequote(pattern, chars) dequote: function dequote(pattern, chars)
pattern.replace(/\\(.)/, (m0, m1) => chars.indexOf(m1) >= 0 ? m1 : m0), pattern.replace(/\\(.)/, (m0, m1) => chars.contains(m1) ? m1 : m0),
/** /**
* Returns the nsIDocShell for the given window. * Returns the nsIDocShell for the given window.
@@ -1110,9 +1110,9 @@ var Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]),
function rec(data, level, seen) { function rec(data, level, seen) {
if (isObject(data)) { if (isObject(data)) {
if (~seen.indexOf(data)) seen = RealSet(seen);
if (seen.add(data))
throw Error("Recursive object passed"); throw Error("Recursive object passed");
seen = seen.concat([data]);
} }
let prefix = level + INDENT; let prefix = level + INDENT;
@@ -1160,7 +1160,7 @@ var Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]),
} }
let res = []; let res = [];
rec(data, "", []); rec(data, "", RealSet());
return res.join(""); return res.join("");
}, },