diff --git a/changelog.d/1097.misc b/changelog.d/1097.misc new file mode 100644 index 000000000..8591129d8 --- /dev/null +++ b/changelog.d/1097.misc @@ -0,0 +1 @@ +Replace .indexOf with more specific methods \ No newline at end of file diff --git a/spec/integ/provisioning.spec.js b/spec/integ/provisioning.spec.js index 17bb031ea..a3ed26ff7 100644 --- a/spec/integ/provisioning.spec.js +++ b/spec/integ/provisioning.spec.js @@ -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(); } } @@ -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(); } } diff --git a/spec/util/irc-client-mock.js b/spec/util/irc-client-mock.js index 1c0ae9ba9..a4a63bc1d 100644 --- a/spec/util/irc-client-mock.js +++ b/spec/util/irc-client-mock.js @@ -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); } @@ -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); } }); diff --git a/src/bridge/AdminRoomHandler.ts b/src/bridge/AdminRoomHandler.ts index 04d34c50a..17f5e2ec3 100644 --- a/src/bridge/AdminRoomHandler.ts +++ b/src/bridge/AdminRoomHandler.ts @@ -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)) { diff --git a/src/bridge/IrcBridge.ts b/src/bridge/IrcBridge.ts index 4bc0c35c2..28f776484 100644 --- a/src/bridge/IrcBridge.ts +++ b/src/bridge/IrcBridge.ts @@ -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) diff --git a/src/bridge/MemberListSyncer.ts b/src/bridge/MemberListSyncer.ts index dcd3f3b4c..5f6da45b0 100644 --- a/src/bridge/MemberListSyncer.ts +++ b/src/bridge/MemberListSyncer.ts @@ -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( @@ -312,7 +312,7 @@ export class MemberListSyncer { d.resolve(); return; } - if (entry.userId.indexOf("@-") === 0) { + if (entry.userId.startsWith("@-")) { joinNextUser(); return; } @@ -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 { diff --git a/src/bridge/PublicitySyncer.ts b/src/bridge/PublicitySyncer.ts index aaa592f03..54ecee34c 100644 --- a/src/bridge/PublicitySyncer.ts +++ b/src/bridge/PublicitySyncer.ts @@ -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]; diff --git a/src/datastore/NedbDataStore.ts b/src/datastore/NedbDataStore.ts index bc5865395..44fe49736 100644 --- a/src/datastore/NedbDataStore.ts +++ b/src/datastore/NedbDataStore.ts @@ -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); }); }); } diff --git a/src/irc/BridgedClient.ts b/src/irc/BridgedClient.ts index ba53cbe26..24114590b 100644 --- a/src/irc/BridgedClient.ts +++ b/src/irc/BridgedClient.ts @@ -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) { @@ -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)) { @@ -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 } @@ -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)); } @@ -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); diff --git a/src/irc/ConnectionInstance.ts b/src/irc/ConnectionInstance.ts index e9bd7cfb5..2ce6ab993 100644 --- a/src/irc/ConnectionInstance.ts +++ b/src/irc/ConnectionInstance.ts @@ -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. } } @@ -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); diff --git a/src/irc/IdentGenerator.ts b/src/irc/IdentGenerator.ts index 65a429f95..5198edf03 100644 --- a/src/irc/IdentGenerator.ts +++ b/src/irc/IdentGenerator.ts @@ -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; } @@ -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 diff --git a/src/irc/IrcEventBroker.ts b/src/irc/IrcEventBroker.ts index 288c0eb26..9ee33f537 100644 --- a/src/irc/IrcEventBroker.ts +++ b/src/irc/IrcEventBroker.ts @@ -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, @@ -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, @@ -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, @@ -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( @@ -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, @@ -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; } @@ -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\[\]\\`^{}|-]*)(!([^@]+)@(.*))?$/ diff --git a/src/irc/IrcServer.ts b/src/irc/IrcServer.ts index 6db6a5b97..d2251c14b 100644 --- a/src/irc/IrcServer.ts +++ b/src/irc/IrcServer.ts @@ -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() { @@ -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) { @@ -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) { diff --git a/src/irc/formatting.ts b/src/irc/formatting.ts index c04a107f6..a15c93a0a 100644 --- a/src/irc/formatting.ts +++ b/src/irc/formatting.ts @@ -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) { @@ -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 === "<") { diff --git a/src/models/IrcAction.ts b/src/models/IrcAction.ts index e90bab449..05872a6e6 100644 --- a/src/models/IrcAction.ts +++ b/src/models/IrcAction.ts @@ -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); } } diff --git a/src/models/IrcRoom.ts b/src/models/IrcRoom.ts index 52539091a..128729864 100644 --- a/src/models/IrcRoom.ts +++ b/src/models/IrcRoom.ts @@ -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."); diff --git a/src/models/MatrixAction.ts b/src/models/MatrixAction.ts index f214cd409..769271296 100644 --- a/src/models/MatrixAction.ts +++ b/src/models/MatrixAction.ts @@ -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); } } diff --git a/src/provisioning/Provisioner.ts b/src/provisioning/Provisioner.ts index fdb8e0d84..536166330 100644 --- a/src/provisioning/Provisioner.ts +++ b/src/provisioning/Provisioner.ts @@ -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}.`); }