From 416f45d1e3a394712e2c1d30ff90ff48803fff8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Astori?= Date: Wed, 22 Nov 2017 01:39:32 -0500 Subject: [PATCH] Use some ES6/Node v4-only syntax when possible --- client/js/webpush.js | 4 ++-- scripts/generate-emoji.js | 2 +- src/client.js | 6 +----- src/command-line/users/add.js | 2 +- src/command-line/users/edit.js | 2 +- src/command-line/users/reset.js | 2 +- src/log.js | 35 ++++++++++++++----------------- src/plugins/inputs/action.js | 5 ++--- src/plugins/inputs/ban.js | 8 +++---- src/plugins/inputs/connect.js | 8 +++---- src/plugins/inputs/ctcp.js | 3 +-- src/plugins/inputs/disconnect.js | 4 ++-- src/plugins/inputs/invite.js | 4 +--- src/plugins/inputs/kick.js | 3 +-- src/plugins/inputs/list.js | 2 +- src/plugins/inputs/mode.js | 10 ++++----- src/plugins/inputs/query.js | 2 +- src/plugins/inputs/raw.js | 3 +-- src/plugins/inputs/rejoin.js | 6 +++--- src/plugins/inputs/topic.js | 4 ++-- src/plugins/inputs/whois.js | 6 +++--- src/plugins/irc-events/message.js | 2 +- src/plugins/irc-events/mode.js | 2 +- src/plugins/webpush.js | 12 +++++------ test/commands/mode.js | 4 ++-- test/util.js | 14 ++++++------- webpack.config.js | 2 +- 27 files changed, 70 insertions(+), 87 deletions(-) diff --git a/client/js/webpush.js b/client/js/webpush.js index 791b079a..6cac73f8 100644 --- a/client/js/webpush.js +++ b/client/js/webpush.js @@ -89,9 +89,9 @@ function onPushButton() { userVisibleOnly: true, }).then((subscription) => { const rawKey = subscription.getKey ? subscription.getKey("p256dh") : ""; - const key = rawKey ? window.btoa(String.fromCharCode.apply(null, new Uint8Array(rawKey))) : ""; + const key = rawKey ? window.btoa(String.fromCharCode(...new Uint8Array(rawKey))) : ""; const rawAuthSecret = subscription.getKey ? subscription.getKey("auth") : ""; - const authSecret = rawAuthSecret ? window.btoa(String.fromCharCode.apply(null, new Uint8Array(rawAuthSecret))) : ""; + const authSecret = rawAuthSecret ? window.btoa(String.fromCharCode(...new Uint8Array(rawAuthSecret))) : ""; socket.emit("push:register", { token: storage.get("token"), diff --git a/scripts/generate-emoji.js b/scripts/generate-emoji.js index 87f7690b..9f864d91 100644 --- a/scripts/generate-emoji.js +++ b/scripts/generate-emoji.js @@ -16,7 +16,7 @@ request.get({ const shortname = prepareShortName(emojiStrategy[key].shortname); // Skip tones, at least for now - if (shortname.indexOf("tone") > -1) { + if (shortname.includes("tone")) { continue; } diff --git a/src/client.js b/src/client.js index 98f7a3ae..72fcd258 100644 --- a/src/client.js +++ b/src/client.js @@ -65,11 +65,7 @@ var inputs = [ return plugins; }, {}); -function Client(manager, name, config) { - if (typeof config !== "object") { - config = {}; - } - +function Client(manager, name, config = {}) { _.merge(this, { awayMessage: config.awayMessage || "", lastActiveChannel: -1, diff --git a/src/command-line/users/add.js b/src/command-line/users/add.js index 21148557..0a5b530f 100644 --- a/src/command-line/users/add.js +++ b/src/command-line/users/add.js @@ -24,7 +24,7 @@ program return; } - if (users.indexOf(name) !== -1) { + if (users.includes(name)) { log.error(`User ${colors.bold(name)} already exists.`); return; } diff --git a/src/command-line/users/edit.js b/src/command-line/users/edit.js index cafc91e9..468cbc4e 100644 --- a/src/command-line/users/edit.js +++ b/src/command-line/users/edit.js @@ -24,7 +24,7 @@ program return; } - if (users.indexOf(name) === -1) { + if (!users.includes(name)) { log.error(`User ${colors.bold(name)} does not exist.`); return; } diff --git a/src/command-line/users/reset.js b/src/command-line/users/reset.js index a3e47fbe..a0318c67 100644 --- a/src/command-line/users/reset.js +++ b/src/command-line/users/reset.js @@ -23,7 +23,7 @@ program return; } - if (users.indexOf(name) === -1) { + if (!users.includes(name)) { log.error(`User ${colors.bold(name)} does not exist.`); return; } diff --git a/src/log.js b/src/log.js index 1efbdfe5..6a54a80b 100644 --- a/src/log.js +++ b/src/log.js @@ -5,41 +5,38 @@ var moment = require("moment"); const read = require("read"); var Helper = require("./helper"); -function timestamp(type, messageArgs) { - var format = Helper.config.logs.format || "YYYY-MM-DD HH:mm:ss"; - var tz = Helper.config.logs.timezone || "UTC+00:00"; +function timestamp() { + const format = Helper.config.logs.format || "YYYY-MM-DD HH:mm:ss"; + const tz = Helper.config.logs.timezone || "UTC+00:00"; + const time = moment().utcOffset(tz).format(format); - var time = moment().utcOffset(tz).format(format); - - Array.prototype.unshift.call(messageArgs, colors.dim(time), type); - - return messageArgs; + return colors.dim(time); } /* eslint-disable no-console */ -exports.error = function() { - console.error.apply(console, timestamp(colors.red("[ERROR]"), arguments)); +exports.error = function(...args) { + console.error(timestamp(), colors.red("[ERROR]"), ...args); }; -exports.warn = function() { - console.error.apply(console, timestamp(colors.yellow("[WARN]"), arguments)); +exports.warn = function(...args) { + console.error(timestamp(), colors.yellow("[WARN]"), ...args); }; -exports.info = function() { - console.log.apply(console, timestamp(colors.blue("[INFO]"), arguments)); +exports.info = function(...args) { + console.log(timestamp(), colors.blue("[INFO]"), ...args); }; -exports.debug = function() { - console.log.apply(console, timestamp(colors.green("[DEBUG]"), arguments)); +exports.debug = function(...args) { + console.log(timestamp(), colors.green("[DEBUG]"), ...args); }; -exports.raw = function() { - console.log.apply(console, arguments); +exports.raw = function(...args) { + console.log(...args); }; /* eslint-enable no-console */ exports.prompt = (options, callback) => { - options.prompt = timestamp(colors.cyan("[PROMPT]"), [options.text]).join(" "); + options.prompt = [timestamp(), colors.cyan("[PROMPT]"), options.text].join(" "); read(options, callback); }; diff --git a/src/plugins/inputs/action.js b/src/plugins/inputs/action.js index 70107026..8c6726b6 100644 --- a/src/plugins/inputs/action.js +++ b/src/plugins/inputs/action.js @@ -5,7 +5,7 @@ var Msg = require("../../models/msg"); exports.commands = ["slap", "me"]; -exports.input = function(network, chan, cmd, args) { +exports.input = function({irc}, chan, cmd, args) { if (chan.type !== Chan.Type.CHANNEL && chan.type !== Chan.Type.QUERY) { chan.pushMessage(this, new Msg({ type: Msg.Type.ERROR, @@ -15,7 +15,6 @@ exports.input = function(network, chan, cmd, args) { return; } - var irc = network.irc; var text; switch (cmd) { @@ -31,7 +30,7 @@ exports.input = function(network, chan, cmd, args) { irc.action(chan.name, text); - if (!network.irc.network.cap.isEnabled("echo-message")) { + if (!irc.network.cap.isEnabled("echo-message")) { irc.emit("action", { nick: irc.user.nick, target: chan.name, diff --git a/src/plugins/inputs/ban.js b/src/plugins/inputs/ban.js index 510e773c..4649a71f 100644 --- a/src/plugins/inputs/ban.js +++ b/src/plugins/inputs/ban.js @@ -9,7 +9,7 @@ exports.commands = [ "banlist", ]; -exports.input = function(network, chan, cmd, args) { +exports.input = function({irc}, chan, cmd, args) { if (chan.type !== Chan.Type.CHANNEL) { chan.pushMessage(this, new Msg({ type: Msg.Type.ERROR, @@ -32,13 +32,13 @@ exports.input = function(network, chan, cmd, args) { switch (cmd) { case "ban": - network.irc.ban(chan.name, args[0]); + irc.ban(chan.name, args[0]); break; case "unban": - network.irc.unban(chan.name, args[0]); + irc.unban(chan.name, args[0]); break; case "banlist": - network.irc.banlist(chan.name); + irc.banlist(chan.name); break; } }; diff --git a/src/plugins/inputs/connect.js b/src/plugins/inputs/connect.js index 8852c430..550f918a 100644 --- a/src/plugins/inputs/connect.js +++ b/src/plugins/inputs/connect.js @@ -5,13 +5,13 @@ var Msg = require("../../models/msg"); exports.commands = ["connect", "server"]; exports.allowDisconnected = true; -exports.input = function(network, chan, cmd, args) { +exports.input = function({irc}, chan, cmd, args) { if (args.length === 0) { - if (!network.irc || !network.irc.connection) { + if (!irc || !irc.connection) { return; } - if (network.irc.connection.connected) { + if (irc.connection.connected) { chan.pushMessage(this, new Msg({ type: Msg.Type.ERROR, text: "You are already connected.", @@ -19,7 +19,7 @@ exports.input = function(network, chan, cmd, args) { return; } - network.irc.connection.connect(); + irc.connection.connect(); return; } diff --git a/src/plugins/inputs/ctcp.js b/src/plugins/inputs/ctcp.js index 0acc6aec..06fb9566 100644 --- a/src/plugins/inputs/ctcp.js +++ b/src/plugins/inputs/ctcp.js @@ -2,9 +2,8 @@ exports.commands = ["ctcp"]; -exports.input = function(network, chan, cmd, args) { +exports.input = function({irc}, chan, cmd, args) { if (args.length > 1) { - var irc = network.irc; irc.ctcpRequest(args[0], args.slice(1).join(" ")); } }; diff --git a/src/plugins/inputs/disconnect.js b/src/plugins/inputs/disconnect.js index 5c414884..7737d60c 100644 --- a/src/plugins/inputs/disconnect.js +++ b/src/plugins/inputs/disconnect.js @@ -4,8 +4,8 @@ const Helper = require("../../helper"); exports.commands = ["disconnect"]; -exports.input = function(network, chan, cmd, args) { +exports.input = function({irc}, chan, cmd, args) { var quitMessage = args[0] ? args.join(" ") : Helper.config.leaveMessage; - network.irc.quit(quitMessage); + irc.quit(quitMessage); }; diff --git a/src/plugins/inputs/invite.js b/src/plugins/inputs/invite.js index 3532eba6..727df6b9 100644 --- a/src/plugins/inputs/invite.js +++ b/src/plugins/inputs/invite.js @@ -5,9 +5,7 @@ var Msg = require("../../models/msg"); exports.commands = ["invite"]; -exports.input = function(network, chan, cmd, args) { - var irc = network.irc; - +exports.input = function({irc}, chan, cmd, args) { if (args.length === 2) { irc.raw("INVITE", args[0], args[1]); // Channel provided in the command } else if (args.length === 1 && chan.type === Chan.Type.CHANNEL) { diff --git a/src/plugins/inputs/kick.js b/src/plugins/inputs/kick.js index 98e013c7..7482642a 100644 --- a/src/plugins/inputs/kick.js +++ b/src/plugins/inputs/kick.js @@ -5,7 +5,7 @@ var Msg = require("../../models/msg"); exports.commands = ["kick"]; -exports.input = function(network, chan, cmd, args) { +exports.input = function({irc}, chan, cmd, args) { if (chan.type !== Chan.Type.CHANNEL) { chan.pushMessage(this, new Msg({ type: Msg.Type.ERROR, @@ -16,7 +16,6 @@ exports.input = function(network, chan, cmd, args) { } if (args.length !== 0) { - var irc = network.irc; irc.raw("KICK", chan.name, args[0], args.slice(1).join(" ")); } diff --git a/src/plugins/inputs/list.js b/src/plugins/inputs/list.js index 4ef63b4a..7d1e6fcc 100644 --- a/src/plugins/inputs/list.js +++ b/src/plugins/inputs/list.js @@ -4,6 +4,6 @@ exports.commands = ["list"]; exports.input = function(network, chan, cmd, args) { network.chanCache = []; - network.irc.list.apply(network.irc, args); + network.irc.list(...args); return true; }; diff --git a/src/plugins/inputs/mode.js b/src/plugins/inputs/mode.js index 4ebd4a84..35f51106 100644 --- a/src/plugins/inputs/mode.js +++ b/src/plugins/inputs/mode.js @@ -13,7 +13,7 @@ exports.commands = [ "devoice", ]; -exports.input = function(network, chan, cmd, args) { +exports.input = function({irc, nick}, chan, cmd, args) { if (cmd !== "mode") { if (chan.type !== Chan.Type.CHANNEL) { chan.pushMessage(this, new Msg({ @@ -43,17 +43,15 @@ exports.input = function(network, chan, cmd, args) { }[cmd]; args.forEach(function(target) { - network.irc.raw("MODE", chan.name, mode, target); + irc.raw("MODE", chan.name, mode, target); }); return; } if (args.length === 0 || args[0][0] === "+" || args[0][0] === "-") { - args.unshift(chan.type === Chan.Type.CHANNEL || chan.type === Chan.Type.QUERY ? chan.name : network.nick); + args.unshift(chan.type === Chan.Type.CHANNEL || chan.type === Chan.Type.QUERY ? chan.name : nick); } - args.unshift("MODE"); - - network.irc.raw.apply(network.irc, args); + irc.raw("MODE", ...args); }; diff --git a/src/plugins/inputs/query.js b/src/plugins/inputs/query.js index 85a88310..9882bebd 100644 --- a/src/plugins/inputs/query.js +++ b/src/plugins/inputs/query.js @@ -22,7 +22,7 @@ exports.input = function(network, chan, cmd, args) { } var char = target[0]; - if (network.irc.network.options.CHANTYPES && network.irc.network.options.CHANTYPES.indexOf(char) !== -1) { + if (network.irc.network.options.CHANTYPES && network.irc.network.options.CHANTYPES.includes(char)) { chan.pushMessage(this, new Msg({ type: Msg.Type.ERROR, text: "You can not open query windows for channels, use /join instead.", diff --git a/src/plugins/inputs/raw.js b/src/plugins/inputs/raw.js index b18fbc4d..dc00b73f 100644 --- a/src/plugins/inputs/raw.js +++ b/src/plugins/inputs/raw.js @@ -2,9 +2,8 @@ exports.commands = ["raw", "send", "quote"]; -exports.input = function(network, chan, cmd, args) { +exports.input = function({irc}, chan, cmd, args) { if (args.length !== 0) { - var irc = network.irc; irc.raw(args); } diff --git a/src/plugins/inputs/rejoin.js b/src/plugins/inputs/rejoin.js index b67cdc5a..1ff01b58 100644 --- a/src/plugins/inputs/rejoin.js +++ b/src/plugins/inputs/rejoin.js @@ -5,7 +5,7 @@ var Chan = require("../../models/chan"); exports.commands = ["cycle", "rejoin"]; -exports.input = function(network, chan) { +exports.input = function({irc}, chan) { if (chan.type !== Chan.Type.CHANNEL) { chan.pushMessage(this, new Msg({ type: Msg.Type.ERROR, @@ -14,8 +14,8 @@ exports.input = function(network, chan) { return; } - network.irc.part(chan.name, "Rejoining"); - network.irc.join(chan.name); + irc.part(chan.name, "Rejoining"); + irc.join(chan.name); return true; }; diff --git a/src/plugins/inputs/topic.js b/src/plugins/inputs/topic.js index 1493df24..36e22deb 100644 --- a/src/plugins/inputs/topic.js +++ b/src/plugins/inputs/topic.js @@ -5,7 +5,7 @@ var Msg = require("../../models/msg"); exports.commands = ["topic"]; -exports.input = function(network, chan, cmd, args) { +exports.input = function({irc}, chan, cmd, args) { if (chan.type !== Chan.Type.CHANNEL) { chan.pushMessage(this, new Msg({ type: Msg.Type.ERROR, @@ -14,6 +14,6 @@ exports.input = function(network, chan, cmd, args) { return; } - network.irc.setTopic(chan.name, args.join(" ")); + irc.setTopic(chan.name, args.join(" ")); return true; }; diff --git a/src/plugins/inputs/whois.js b/src/plugins/inputs/whois.js index 03573109..805b9bf1 100644 --- a/src/plugins/inputs/whois.js +++ b/src/plugins/inputs/whois.js @@ -2,14 +2,14 @@ exports.commands = ["whois"]; -exports.input = function(network, chan, cmd, args) { +exports.input = function({irc}, chan, cmd, args) { if (args.length === 1) { // This queries server of the other user and not of the current user, which // does not know idle time. // See http://superuser.com/a/272069/208074. - network.irc.raw("WHOIS", args[0], args[0]); + irc.raw("WHOIS", args[0], args[0]); } else { // Re-assembling the command parsed in client.js - network.irc.raw(`${cmd} ${args.join(" ")}`); + irc.raw(`${cmd} ${args.join(" ")}`); } }; diff --git a/src/plugins/irc-events/message.js b/src/plugins/irc-events/message.js index 65fb3897..9b24974e 100644 --- a/src/plugins/irc-events/message.js +++ b/src/plugins/irc-events/message.js @@ -114,7 +114,7 @@ module.exports = function(irc, network) { } // No prefetch URLs unless are simple MESSAGE or ACTION types - if ([Msg.Type.MESSAGE, Msg.Type.ACTION].indexOf(data.type) !== -1) { + if ([Msg.Type.MESSAGE, Msg.Type.ACTION].includes(data.type)) { LinkPrefetch(client, chan, msg); } diff --git a/src/plugins/irc-events/mode.js b/src/plugins/irc-events/mode.js index d2d79d65..5b416666 100644 --- a/src/plugins/irc-events/mode.js +++ b/src/plugins/irc-events/mode.js @@ -94,7 +94,7 @@ module.exports = function(irc, network) { if (!add) { _.pull(user.modes, changedMode); - } else if (user.modes.indexOf(changedMode) === -1) { + } else if (!user.modes.includes(changedMode)) { user.modes.push(changedMode); user.modes.sort(function(a, b) { return userModeSortPriority[a] - userModeSortPriority[b]; diff --git a/src/plugins/webpush.js b/src/plugins/webpush.js index 0777483b..f8b5ecac 100644 --- a/src/plugins/webpush.js +++ b/src/plugins/webpush.js @@ -38,13 +38,13 @@ class WebPush { } push(client, payload, onlyToOffline) { - _.forOwn(client.config.sessions, (session, token) => { - if (session.pushSubscription) { - if (onlyToOffline && _.find(client.attachedClients, {token: token}) !== undefined) { + _.forOwn(client.config.sessions, ({pushSubscription}, token) => { + if (pushSubscription) { + if (onlyToOffline && _.find(client.attachedClients, {token}) !== undefined) { return; } - this.pushSingle(client, session.pushSubscription, payload); + this.pushSingle(client, pushSubscription, payload); } }); } @@ -56,8 +56,8 @@ class WebPush { if (error.statusCode >= 400 && error.statusCode < 500) { log.warn(`WebPush subscription for ${client.name} returned an error (${error.statusCode}), removing subscription`); - _.forOwn(client.config.sessions, (session, token) => { - if (session.pushSubscription && session.pushSubscription.endpoint === subscription.endpoint) { + _.forOwn(client.config.sessions, ({pushSubscription}, token) => { + if (pushSubscription && pushSubscription.endpoint === subscription.endpoint) { client.unregisterPushSubscription(token); } }); diff --git a/test/commands/mode.js b/test/commands/mode.js index e69d3e50..ac16b290 100644 --- a/test/commands/mode.js +++ b/test/commands/mode.js @@ -20,8 +20,8 @@ describe("Commands", function() { lastCommand: null, nick: "xPaw", irc: { - raw: function() { - testableNetwork.lastCommand = Array.prototype.join.call(arguments, " "); + raw: function(...args) { + testableNetwork.lastCommand = args.join(" "); }, }, }; diff --git a/test/util.js b/test/util.js index 66e9987e..22027fd8 100644 --- a/test/util.js +++ b/test/util.js @@ -24,14 +24,12 @@ MockClient.prototype.createMessage = function(opts) { }; function mockLogger(callback) { - return function() { - // TODO: Use ...args with The Lounge v3: add `...args` as function argument - // and replaced the next line with `args.join(", ")` - const stdout = Array.prototype.slice.call(arguments).join(" ") - .replace( // Removes ANSI colors. See https://stackoverflow.com/a/29497680 - /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, - "" - ); + return function(...args) { + // Concats and removes ANSI colors. See https://stackoverflow.com/a/29497680 + const stdout = args.join(" ").replace( + /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, + "" + ); callback(stdout + "\n"); }; diff --git a/webpack.config.js b/webpack.config.js index 2b0948f9..449e4313 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -101,7 +101,7 @@ const config = { // automatically split all vendor dependencies into a separate bundle new webpack.optimize.CommonsChunkPlugin({ name: "js/bundle.vendor.js", - minChunks: (module) => module.context && module.context.indexOf("node_modules") !== -1, + minChunks: (module) => module.context && module.context.includes("node_modules"), }), ], };