Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace .indexOf with more specific methods #1097

Merged
merged 4 commits into from
Aug 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/1097.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Replace .indexOf with more specific methods
4 changes: 2 additions & 2 deletions spec/integ/provisioning.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ describe("Provisioning API", function() {
if (content.status === "failure") {
env.isFailed.resolve();
}
else if (content.status == "success") {
else if (content.status === "success") {
env.isSuccess.resolve();
}
}
Expand Down Expand Up @@ -472,7 +472,7 @@ describe("Provisioning API", function() {
if (content.status === "failure") {
env.isFailed.resolve();
}
else if (content.status == "success") {
else if (content.status === "success") {
env.isSuccess.resolve();
}
}
Expand Down
4 changes: 2 additions & 2 deletions spec/util/irc-client-mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ module.exports._autoJoinChannels = function(addr, nick, channels) {
channels = [channels];
}
module.exports._whenClient(addr, nick, "join", function(client, chan, cb) {
if (channels.indexOf(chan) != -1) {
if (channels.includes(chan)) {
client.chans[chan] = {};
client._invokeCallback(cb);
}
Expand All @@ -244,7 +244,7 @@ module.exports._autoConnectNetworks = function(addr, nick, networks) {
networks = [networks];
}
module.exports._whenClient(addr, nick, "connect", function(client, cb) {
if (networks.indexOf(client.addr) != -1) {
if (networks.includes(client.addr)) {
client._invokeCallback(cb);
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/bridge/AdminRoomHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export class AdminRoomHandler {
const ircChannel = args[0];
const key = args[1]; // keys can't have spaces in them, so we can just do this.
let errText = null;
if (!ircChannel || ircChannel.indexOf("#") !== 0) {
if (!ircChannel || !ircChannel.startsWith("#")) {
errText = "Format: '!join irc.example.com #channel [key]'";
}
else if (!server.canJoinRooms(sender)) {
Expand Down
2 changes: 1 addition & 1 deletion src/bridge/IrcBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ export class IrcBridge {
else if (event.content.membership === "join") {
await this.matrixHandler.onJoin(request, event, target);
}
else if (["ban", "leave"].indexOf(event.content.membership) !== -1) {
else if (["ban", "leave"].includes(event.content.membership)) {
// Given a "self-kick" is a leave, and you can't ban yourself,
// if the 2 IDs are different then we know it is either a kick
// or a ban (or a rescinded invite)
Expand Down
6 changes: 3 additions & 3 deletions src/bridge/MemberListSyncer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export class MemberListSyncer {
// map irc channel to a list of room IDs. If all of those
// room IDs have no real users in them, then part the bridge bot too.
public async checkBotPartRoom(ircRoom: IrcRoom, req: BridgeRequest) {
if (ircRoom.channel.indexOf("#") !== 0) {
if (!ircRoom.channel.startsWith("#")) {
return; // don't leave PM rooms
}
const matrixRooms = await this.ircBridge.getStore().getMatrixRoomsForChannel(
Expand Down Expand Up @@ -312,7 +312,7 @@ export class MemberListSyncer {
d.resolve();
return;
}
if (entry.userId.indexOf("@-") === 0) {
if (entry.userId.startsWith("@-")) {
joinNextUser();
return;
}
Expand Down Expand Up @@ -474,7 +474,7 @@ export class MemberListSyncer {
if (server.claimsUserId(userId)) {
data.virtuals.push(userId);
}
else if (userId.indexOf("@-") === 0) {
else if (userId.startsWith("@-")) {
// Ignore guest user IDs -- TODO: Do this properly by passing them through
}
else {
Expand Down
2 changes: 1 addition & 1 deletion src/bridge/PublicitySyncer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export class PublicitySyncer {
}

// Filter out already checked channels
channels = channels.filter((c) => checkedChannels.indexOf(c) === -1);
channels = channels.filter((c) => !checkedChannels.includes(c));

const anyAreSecret = channels.some((channel) => {
let channelIsSecret = this.visibilityMap.channelIsSecret[channel];
Expand Down
2 changes: 1 addition & 1 deletion src/datastore/NedbDataStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ export class NeDBDataStore implements DataStore {
return true;
}
}
return e.data && origin.indexOf(e.data.origin) !== -1;
return e.data && origin.includes(e.data.origin);
});
});
}
Expand Down
14 changes: 7 additions & 7 deletions src/irc/BridgedClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ export class BridgedClient extends EventEmitter {
"err_erroneusnickname", "err_nonicknamegiven", "err_eventnickchange",
"err_nicktoofast", "err_unavailresource"
];
if (failCodes.indexOf(err.command) !== -1) {
if (failCodes.includes(err.command)) {
this.log.error("Nick change error : %s", err.command);
clearTimeout(timeoutId);
if (nickListener) {
Expand All @@ -404,7 +404,7 @@ export class BridgedClient extends EventEmitter {
if (!this.inst || this.inst.dead || !this.unsafeClient) {
return Promise.resolve(); // we were never connected to the network.
}
if (channel.indexOf("#") !== 0) {
if (!channel.startsWith("#")) {
return Promise.resolve(); // PM room
}
if (!this.inChannel(channel)) {
Expand All @@ -430,11 +430,11 @@ export class BridgedClient extends EventEmitter {
if (!this.inst || this.inst.dead || !this.unsafeClient) {
return Promise.resolve(); // we were never connected to the network.
}
if (Object.keys(this.unsafeClient.chans).indexOf(channel) === -1) {
if (!Object.keys(this.unsafeClient.chans).includes(channel)) {
// we were never joined to it. We need to be joined to it to kick people.
return Promise.resolve();
}
if (channel.indexOf("#") !== 0) {
if (!channel.startsWith("#")) {
return Promise.resolve(); // PM room
}

Expand Down Expand Up @@ -832,10 +832,10 @@ export class BridgedClient extends EventEmitter {
}
return Bluebird.reject(new Error("No client"));
}
if (Object.keys(this.unsafeClient.chans).indexOf(channel) !== -1) {
if (Object.keys(this.unsafeClient.chans).includes(channel)) {
return Bluebird.resolve(new IrcRoom(this.server, channel));
}
if (channel.indexOf("#") !== 0) {
if (!channel.startsWith("#")) {
// PM room
return Bluebird.resolve(new IrcRoom(this.server, channel));
}
Expand Down Expand Up @@ -878,7 +878,7 @@ export class BridgedClient extends EventEmitter {
// promise isn't resolved yet and we still want to join this channel
if (defer.promise.isPending() && this._chanList.has(channel)) {
// we may have joined but didn't get the callback so check the client
if (Object.keys(this.unsafeClient.chans).indexOf(channel) !== -1) {
if (Object.keys(this.unsafeClient.chans).includes(channel)) {
// we're joined
this.log.debug("Timed out joining %s - didn't get callback but " +
"are now joined. Resolving.", channel);
Expand Down
4 changes: 2 additions & 2 deletions src/irc/ConnectionInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ export class ConnectionInstance {
"err_umodeunknownflag", "err_nononreg"
];
if (err && err.command) {
if (failCodes.indexOf(err.command) !== -1) {
if (failCodes.includes(err.command)) {
return; // don't disconnect for these error codes.
}
}
Expand Down Expand Up @@ -262,7 +262,7 @@ export class ConnectionInstance {

let errText = ("" + msg.args[0]) || "";
errText = errText.toLowerCase();
wasThrottled = errText.indexOf("throttl") !== -1;
wasThrottled = errText.includes("throttl");

if (wasThrottled) {
this.disconnect("throttled").catch(logError);
Expand Down
4 changes: 2 additions & 2 deletions src/irc/IdentGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export class IdentGenerator {
*/
const delim = "_";
const modifyUsername = () => {
if (uname.indexOf(delim) === -1) {
if (!uname.includes(delim)) {
uname = uname.substring(0, uname.length - 2) + delim + "1";
return true;
}
Expand All @@ -174,7 +174,7 @@ export class IdentGenerator {
else {
uname = segments[0] + delim + num;
}
return uname.indexOf(delim) !== 0; // break out if '~10000'
return !uname.startsWith(delim); // break out if '~10000'
}

// TODO: This isn't efficient currently; since this will be called worst
Expand Down
20 changes: 10 additions & 10 deletions src/irc/IrcEventBroker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ export class IrcEventBroker {
// listen for PMs for clients. If you listen for rooms, you'll get
// duplicates since the bot will also invoke the callback fn!
connInst.addListener("message", (from: string, to: string, text: string) => {
if (to.indexOf("#") === 0) { return; }
if (to.startsWith("#")) { return; }
const req = createRequest();
complete(req, ircHandler.onPrivateMessage(
req,
Expand All @@ -253,7 +253,7 @@ export class IrcEventBroker {
));
});
connInst.addListener("notice", (from: string, to: string, text: string) => {
if (!from || to.indexOf("#") === 0) { return; }
if (!from || to.startsWith("#")) { return; }
const req = createRequest();
complete(req, ircHandler.onPrivateMessage(
req,
Expand All @@ -262,8 +262,8 @@ export class IrcEventBroker {
));
});
connInst.addListener("ctcp-privmsg", (from: string, to: string, text: string) => {
if (to.indexOf("#") === 0) { return; }
if (text.indexOf("ACTION ") === 0) {
if (to.startsWith("#")) { return; }
if (text.startsWith("ACTION ")) {
const req = createRequest();
complete(req, ircHandler.onPrivateMessage(
req,
Expand Down Expand Up @@ -430,7 +430,7 @@ export class IrcEventBroker {
));
});
this.hookIfClaimed(client, connInst, "message", (from: string, to: string, text: string) => {
if (to.indexOf("#") !== 0) { return; }
if (!to.startsWith("#")) { return; }
const req = createRequest();
this.bufferRequestToChannel(to, () => {
return complete(req, ircHandler.onMessage(
Expand All @@ -440,8 +440,8 @@ export class IrcEventBroker {
}, req);
});
this.hookIfClaimed(client, connInst, "ctcp-privmsg", function(from: string, to: string, text: string) {
if (to.indexOf("#") !== 0) { return; }
if (text.indexOf("ACTION ") === 0) {
if (!to.startsWith("#")) { return; }
if (text.startsWith("ACTION ")) {
const req = createRequest();
complete(req, ircHandler.onMessage(
req, server, createUser(from), to,
Expand All @@ -450,7 +450,7 @@ export class IrcEventBroker {
}
});
this.hookIfClaimed(client, connInst, "notice", (from: string, to: string, text: string) => {
if (to.indexOf("#") !== 0) { return; }
if (!to.startsWith("#")) { return; }
if (!from) { // ignore server notices
return;
}
Expand All @@ -462,9 +462,9 @@ export class IrcEventBroker {
}, req);
});
this.hookIfClaimed(client, connInst, "topic", function(channel: string, topic: string, nick: string) {
if (channel.indexOf("#") !== 0) { return; }
if (!channel.startsWith("#")) { return; }

if (nick && nick.indexOf("@") !== -1) {
if (nick && nick.includes("@")) {
const match = nick.match(
// https://github.com/martynsmith/node-irc/blob/master/lib/parse_message.js#L26
/^([_a-zA-Z0-9\[\]\\`^{}|-]*)(!([^@]+)@(.*))?$/
Expand Down
6 changes: 3 additions & 3 deletions src/irc/IrcServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ export class IrcServer {
}

public isInWhitelist(userId: string) {
return this.config.dynamicChannels.whitelist.indexOf(userId) !== -1;
return this.config.dynamicChannels.whitelist.includes(userId);
}

public getCA() {
Expand Down Expand Up @@ -297,7 +297,7 @@ export class IrcServer {
}

public isExcludedChannel(channel: string) {
return this.config.dynamicChannels.exclude.indexOf(channel) !== -1;
return this.config.dynamicChannels.exclude.includes(channel);
}

public isExcludedUser(userId: string) {
Expand Down Expand Up @@ -342,7 +342,7 @@ export class IrcServer {
}

private shouldSyncMembership(kind: MembershipSyncKind, identifier: string|undefined, toIrc: boolean) {
if (["incremental", "initial"].indexOf(kind) === -1) {
if (!["incremental", "initial"].includes(kind)) {
throw new Error("Bad kind: " + kind);
}
if (!this.config.membershipLists.enabled) {
Expand Down
4 changes: 2 additions & 2 deletions src/irc/formatting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export function htmlTag(state: StyleState, name: string, open?: boolean): string
let text = '';

if (typeof open === 'undefined') {
open = (state.history.indexOf(name) === -1);
open = !state.history.includes(name);
}

if (open) {
Expand Down Expand Up @@ -205,7 +205,7 @@ export function htmlToIrc(html?: string): string|null {
let replacement;
for (let i = 0; i < cleanHtml.length; i++) {
const ch = cleanHtml[i];
if (STYLE_CODES.indexOf(ch) >= 0) {
if (STYLE_CODES.includes(ch)) {
openStyleCodes.push(ch);
}
else if (ch === "<") {
Expand Down
2 changes: 1 addition & 1 deletion src/models/IrcAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class IrcAction {
public readonly type: IrcActionType,
public text: string,
public readonly ts: number = 0 ) {
if (ACTION_TYPES.indexOf(type) === -1) {
if (!ACTION_TYPES.includes(type)) {
throw new Error("Unknown IrcAction type: " + type);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/models/IrcRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class IrcRoom extends RemoteRoom {
super(IrcRoom.createId(server, toIrcLowerCase(channel)), {
domain: server.domain,
channel: toIrcLowerCase(channel),
type: channel.indexOf("#") === 0 ? "channel" : "pm"
type: channel.startsWith("#") ? "channel" : "pm"
});
if (!server || !channel) {
throw new Error("Server and channel are required.");
Expand Down
2 changes: 1 addition & 1 deletion src/models/MatrixAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export class MatrixAction {
public htmlText: string|null = null,
public readonly ts: number = 0
) {
if (ACTION_TYPES.indexOf(type) === -1) {
if (!ACTION_TYPES.includes(type)) {
throw new Error("Unknown MatrixAction type: " + type);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/provisioning/Provisioner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,11 +347,11 @@ export class Provisioner {

const info = await botClient.getOperators(ircChannel, {key : key});

if (info.nicks.indexOf(opNick) === -1) {
if (!info.nicks.includes(opNick)) {
throw new Error(`Provided user is not in channel ${ircChannel}.`);
}

if (info.operatorNicks.indexOf(opNick) === -1) {
if (!info.operatorNicks.includes(opNick)) {
throw new Error(`Provided user is not an op of ${ircChannel}.`);
}

Expand Down