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

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

View File

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

View File

@@ -764,7 +764,7 @@ var Commands = Module("commands", {
return "";
}
// 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)])
.filter(([h, c]) => c.length);
@@ -1067,7 +1067,7 @@ var Commands = Module("commands", {
if (!onlyArgumentsRemaining) {
for (let [, opt] in Iterator(options)) {
for (let [, optname] in Iterator(opt.names)) {
if (sub.indexOf(optname) == 0) {
if (sub.startsWith(optname)) {
let count = 0;
let invalid = false;
let arg, quote, quoted;

View File

@@ -512,7 +512,7 @@ var CompletionContext = Class("CompletionContext", {
filtered.sort(this.compare);
if (!this.anchored) {
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];
}
else {
var compare = function compare(text, s) text.indexOf(s) >= 0;
var compare = function compare(text, s) text.contains(s);
var substrings = [];
let start = 0;
let idx;
@@ -970,7 +970,7 @@ var Completion = Module("completion", {
context.generate = function generate_() {
return [[k.substr(services.ABOUT.length), ""]
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;
if (context.ignoreCase) {
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)
@@ -1180,7 +1180,7 @@ var Completion = Module("completion", {
.concat([let (name = k.substr(services.AUTOCOMPLETE.length))
["native:" + name, _("autocomplete.description", name)]
for (k in Cc)
if (k.indexOf(services.AUTOCOMPLETE) == 0)]),
if (k.startsWith(services.AUTOCOMPLETE))]),
setter: function setter(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_";
var states = iter([v, k.slice(prefix.length).toLowerCase()]
for ([k, v] in Iterator(Ci.nsIDownloadManager))
if (k.indexOf(prefix) == 0))
if (k.startsWith(prefix)))
.toObject();
var Download = Class("Download", {
@@ -288,7 +288,7 @@ var DownloadList = Class("DownloadList",
addDownload: function addDownload(download) {
if (!this.downloads.has(download)) {
download = Download(download, this);
if (this.filter && download.displayName.indexOf(this.filter) === -1)
if (this.filter && !download.displayName.contains(this.filter))
return;
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
// given in the LICENSE.txt file included with this file.
@@ -626,7 +626,7 @@ var RangeFind = Class("RangeFind", {
if (!this.matchCase)
pattern = pattern.toLowerCase();
if (!again && (pattern === "" || pattern.indexOf(this.lastString) !== 0 || this.backward)) {
if (!again && (pattern === "" || !pattern.startsWith(this.lastString) || this.backward)) {
if (!private_)
this.range.deselect();
if (pattern === "")

View File

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

View File

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

View File

@@ -358,7 +358,7 @@ var Overlay = Module("Overlay", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReferen
delete desc.writable;
desc.get = function get() value;
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);
else {
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) 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
// 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() {
for (let [, pref] in Iterator(prefArray)) {
let userValue = services.pref.prefHasUserValue(pref);
if (onlyNonDefault && !userValue || pref.indexOf(filter) == -1)
if (onlyNonDefault && !userValue || !pref.contains(filter))
continue;
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))
if (!host || util.isSubdomain(c.rawHost, host) ||
c.host[0] == "." && c.host.length < host.length
&& host.indexOf(c.host) == host.length - c.host.length)
&& host.endsWith(c.host))
yield c;
},
@@ -597,7 +597,7 @@ var Sanitizer = Module("sanitizer", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakRef
completion: function initCompletion(dactyl, modules, window) {
modules.completion.visibleHosts = function completeHosts(context) {
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);
context.title = ["Domain"];

View File

@@ -212,7 +212,7 @@ var Hive = Class("Hive", {
name = null;
}
if (filter && filter.indexOf(",") > -1)
if (filter && filter.contains(","))
return filter.split(",").reduce(
(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;
}
if (pattern.indexOf("{") == -1)
if (!pattern.contains("{"))
return [pattern];
let res = [];
@@ -516,7 +516,7 @@ var Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]),
rec([]);
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.set("javascript.options.methodjit.chrome", false);
util.dactyl.warn(_(UTF8("error.damnYouJägermonkey")));
@@ -544,7 +544,7 @@ var Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]),
* @returns {string}
*/
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.
@@ -1110,9 +1110,9 @@ var Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]),
function rec(data, level, seen) {
if (isObject(data)) {
if (~seen.indexOf(data))
seen = RealSet(seen);
if (seen.add(data))
throw Error("Recursive object passed");
seen = seen.concat([data]);
}
let prefix = level + INDENT;
@@ -1160,7 +1160,7 @@ var Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]),
}
let res = [];
rec(data, "", []);
rec(data, "", RealSet());
return res.join("");
},