Use some ES6/Node v4-only syntax when possible

This commit is contained in:
Jérémie Astori 2017-11-22 01:39:32 -05:00 committed by Pavel Djundik
parent 6013a02fc2
commit 416f45d1e3
27 changed files with 70 additions and 87 deletions

View File

@ -89,9 +89,9 @@ function onPushButton() {
userVisibleOnly: true, userVisibleOnly: true,
}).then((subscription) => { }).then((subscription) => {
const rawKey = subscription.getKey ? subscription.getKey("p256dh") : ""; 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 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", { socket.emit("push:register", {
token: storage.get("token"), token: storage.get("token"),

View File

@ -16,7 +16,7 @@ request.get({
const shortname = prepareShortName(emojiStrategy[key].shortname); const shortname = prepareShortName(emojiStrategy[key].shortname);
// Skip tones, at least for now // Skip tones, at least for now
if (shortname.indexOf("tone") > -1) { if (shortname.includes("tone")) {
continue; continue;
} }

View File

@ -65,11 +65,7 @@ var inputs = [
return plugins; return plugins;
}, {}); }, {});
function Client(manager, name, config) { function Client(manager, name, config = {}) {
if (typeof config !== "object") {
config = {};
}
_.merge(this, { _.merge(this, {
awayMessage: config.awayMessage || "", awayMessage: config.awayMessage || "",
lastActiveChannel: -1, lastActiveChannel: -1,

View File

@ -24,7 +24,7 @@ program
return; return;
} }
if (users.indexOf(name) !== -1) { if (users.includes(name)) {
log.error(`User ${colors.bold(name)} already exists.`); log.error(`User ${colors.bold(name)} already exists.`);
return; return;
} }

View File

@ -24,7 +24,7 @@ program
return; return;
} }
if (users.indexOf(name) === -1) { if (!users.includes(name)) {
log.error(`User ${colors.bold(name)} does not exist.`); log.error(`User ${colors.bold(name)} does not exist.`);
return; return;
} }

View File

@ -23,7 +23,7 @@ program
return; return;
} }
if (users.indexOf(name) === -1) { if (!users.includes(name)) {
log.error(`User ${colors.bold(name)} does not exist.`); log.error(`User ${colors.bold(name)} does not exist.`);
return; return;
} }

View File

@ -5,41 +5,38 @@ var moment = require("moment");
const read = require("read"); const read = require("read");
var Helper = require("./helper"); var Helper = require("./helper");
function timestamp(type, messageArgs) { function timestamp() {
var format = Helper.config.logs.format || "YYYY-MM-DD HH:mm:ss"; const format = Helper.config.logs.format || "YYYY-MM-DD HH:mm:ss";
var tz = Helper.config.logs.timezone || "UTC+00:00"; const tz = Helper.config.logs.timezone || "UTC+00:00";
const time = moment().utcOffset(tz).format(format);
var time = moment().utcOffset(tz).format(format); return colors.dim(time);
Array.prototype.unshift.call(messageArgs, colors.dim(time), type);
return messageArgs;
} }
/* eslint-disable no-console */ /* eslint-disable no-console */
exports.error = function() { exports.error = function(...args) {
console.error.apply(console, timestamp(colors.red("[ERROR]"), arguments)); console.error(timestamp(), colors.red("[ERROR]"), ...args);
}; };
exports.warn = function() { exports.warn = function(...args) {
console.error.apply(console, timestamp(colors.yellow("[WARN]"), arguments)); console.error(timestamp(), colors.yellow("[WARN]"), ...args);
}; };
exports.info = function() { exports.info = function(...args) {
console.log.apply(console, timestamp(colors.blue("[INFO]"), arguments)); console.log(timestamp(), colors.blue("[INFO]"), ...args);
}; };
exports.debug = function() { exports.debug = function(...args) {
console.log.apply(console, timestamp(colors.green("[DEBUG]"), arguments)); console.log(timestamp(), colors.green("[DEBUG]"), ...args);
}; };
exports.raw = function() { exports.raw = function(...args) {
console.log.apply(console, arguments); console.log(...args);
}; };
/* eslint-enable no-console */ /* eslint-enable no-console */
exports.prompt = (options, callback) => { 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); read(options, callback);
}; };

View File

@ -5,7 +5,7 @@ var Msg = require("../../models/msg");
exports.commands = ["slap", "me"]; 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) { if (chan.type !== Chan.Type.CHANNEL && chan.type !== Chan.Type.QUERY) {
chan.pushMessage(this, new Msg({ chan.pushMessage(this, new Msg({
type: Msg.Type.ERROR, type: Msg.Type.ERROR,
@ -15,7 +15,6 @@ exports.input = function(network, chan, cmd, args) {
return; return;
} }
var irc = network.irc;
var text; var text;
switch (cmd) { switch (cmd) {
@ -31,7 +30,7 @@ exports.input = function(network, chan, cmd, args) {
irc.action(chan.name, text); irc.action(chan.name, text);
if (!network.irc.network.cap.isEnabled("echo-message")) { if (!irc.network.cap.isEnabled("echo-message")) {
irc.emit("action", { irc.emit("action", {
nick: irc.user.nick, nick: irc.user.nick,
target: chan.name, target: chan.name,

View File

@ -9,7 +9,7 @@ exports.commands = [
"banlist", "banlist",
]; ];
exports.input = function(network, chan, cmd, args) { exports.input = function({irc}, chan, cmd, args) {
if (chan.type !== Chan.Type.CHANNEL) { if (chan.type !== Chan.Type.CHANNEL) {
chan.pushMessage(this, new Msg({ chan.pushMessage(this, new Msg({
type: Msg.Type.ERROR, type: Msg.Type.ERROR,
@ -32,13 +32,13 @@ exports.input = function(network, chan, cmd, args) {
switch (cmd) { switch (cmd) {
case "ban": case "ban":
network.irc.ban(chan.name, args[0]); irc.ban(chan.name, args[0]);
break; break;
case "unban": case "unban":
network.irc.unban(chan.name, args[0]); irc.unban(chan.name, args[0]);
break; break;
case "banlist": case "banlist":
network.irc.banlist(chan.name); irc.banlist(chan.name);
break; break;
} }
}; };

View File

@ -5,13 +5,13 @@ var Msg = require("../../models/msg");
exports.commands = ["connect", "server"]; exports.commands = ["connect", "server"];
exports.allowDisconnected = true; exports.allowDisconnected = true;
exports.input = function(network, chan, cmd, args) { exports.input = function({irc}, chan, cmd, args) {
if (args.length === 0) { if (args.length === 0) {
if (!network.irc || !network.irc.connection) { if (!irc || !irc.connection) {
return; return;
} }
if (network.irc.connection.connected) { if (irc.connection.connected) {
chan.pushMessage(this, new Msg({ chan.pushMessage(this, new Msg({
type: Msg.Type.ERROR, type: Msg.Type.ERROR,
text: "You are already connected.", text: "You are already connected.",
@ -19,7 +19,7 @@ exports.input = function(network, chan, cmd, args) {
return; return;
} }
network.irc.connection.connect(); irc.connection.connect();
return; return;
} }

View File

@ -2,9 +2,8 @@
exports.commands = ["ctcp"]; exports.commands = ["ctcp"];
exports.input = function(network, chan, cmd, args) { exports.input = function({irc}, chan, cmd, args) {
if (args.length > 1) { if (args.length > 1) {
var irc = network.irc;
irc.ctcpRequest(args[0], args.slice(1).join(" ")); irc.ctcpRequest(args[0], args.slice(1).join(" "));
} }
}; };

View File

@ -4,8 +4,8 @@ const Helper = require("../../helper");
exports.commands = ["disconnect"]; 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; var quitMessage = args[0] ? args.join(" ") : Helper.config.leaveMessage;
network.irc.quit(quitMessage); irc.quit(quitMessage);
}; };

View File

@ -5,9 +5,7 @@ var Msg = require("../../models/msg");
exports.commands = ["invite"]; exports.commands = ["invite"];
exports.input = function(network, chan, cmd, args) { exports.input = function({irc}, chan, cmd, args) {
var irc = network.irc;
if (args.length === 2) { if (args.length === 2) {
irc.raw("INVITE", args[0], args[1]); // Channel provided in the command irc.raw("INVITE", args[0], args[1]); // Channel provided in the command
} else if (args.length === 1 && chan.type === Chan.Type.CHANNEL) { } else if (args.length === 1 && chan.type === Chan.Type.CHANNEL) {

View File

@ -5,7 +5,7 @@ var Msg = require("../../models/msg");
exports.commands = ["kick"]; exports.commands = ["kick"];
exports.input = function(network, chan, cmd, args) { exports.input = function({irc}, chan, cmd, args) {
if (chan.type !== Chan.Type.CHANNEL) { if (chan.type !== Chan.Type.CHANNEL) {
chan.pushMessage(this, new Msg({ chan.pushMessage(this, new Msg({
type: Msg.Type.ERROR, type: Msg.Type.ERROR,
@ -16,7 +16,6 @@ exports.input = function(network, chan, cmd, args) {
} }
if (args.length !== 0) { if (args.length !== 0) {
var irc = network.irc;
irc.raw("KICK", chan.name, args[0], args.slice(1).join(" ")); irc.raw("KICK", chan.name, args[0], args.slice(1).join(" "));
} }

View File

@ -4,6 +4,6 @@ exports.commands = ["list"];
exports.input = function(network, chan, cmd, args) { exports.input = function(network, chan, cmd, args) {
network.chanCache = []; network.chanCache = [];
network.irc.list.apply(network.irc, args); network.irc.list(...args);
return true; return true;
}; };

View File

@ -13,7 +13,7 @@ exports.commands = [
"devoice", "devoice",
]; ];
exports.input = function(network, chan, cmd, args) { exports.input = function({irc, nick}, chan, cmd, args) {
if (cmd !== "mode") { if (cmd !== "mode") {
if (chan.type !== Chan.Type.CHANNEL) { if (chan.type !== Chan.Type.CHANNEL) {
chan.pushMessage(this, new Msg({ chan.pushMessage(this, new Msg({
@ -43,17 +43,15 @@ exports.input = function(network, chan, cmd, args) {
}[cmd]; }[cmd];
args.forEach(function(target) { args.forEach(function(target) {
network.irc.raw("MODE", chan.name, mode, target); irc.raw("MODE", chan.name, mode, target);
}); });
return; return;
} }
if (args.length === 0 || args[0][0] === "+" || args[0][0] === "-") { 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"); irc.raw("MODE", ...args);
network.irc.raw.apply(network.irc, args);
}; };

View File

@ -22,7 +22,7 @@ exports.input = function(network, chan, cmd, args) {
} }
var char = target[0]; 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({ chan.pushMessage(this, new Msg({
type: Msg.Type.ERROR, type: Msg.Type.ERROR,
text: "You can not open query windows for channels, use /join instead.", text: "You can not open query windows for channels, use /join instead.",

View File

@ -2,9 +2,8 @@
exports.commands = ["raw", "send", "quote"]; exports.commands = ["raw", "send", "quote"];
exports.input = function(network, chan, cmd, args) { exports.input = function({irc}, chan, cmd, args) {
if (args.length !== 0) { if (args.length !== 0) {
var irc = network.irc;
irc.raw(args); irc.raw(args);
} }

View File

@ -5,7 +5,7 @@ var Chan = require("../../models/chan");
exports.commands = ["cycle", "rejoin"]; exports.commands = ["cycle", "rejoin"];
exports.input = function(network, chan) { exports.input = function({irc}, chan) {
if (chan.type !== Chan.Type.CHANNEL) { if (chan.type !== Chan.Type.CHANNEL) {
chan.pushMessage(this, new Msg({ chan.pushMessage(this, new Msg({
type: Msg.Type.ERROR, type: Msg.Type.ERROR,
@ -14,8 +14,8 @@ exports.input = function(network, chan) {
return; return;
} }
network.irc.part(chan.name, "Rejoining"); irc.part(chan.name, "Rejoining");
network.irc.join(chan.name); irc.join(chan.name);
return true; return true;
}; };

View File

@ -5,7 +5,7 @@ var Msg = require("../../models/msg");
exports.commands = ["topic"]; exports.commands = ["topic"];
exports.input = function(network, chan, cmd, args) { exports.input = function({irc}, chan, cmd, args) {
if (chan.type !== Chan.Type.CHANNEL) { if (chan.type !== Chan.Type.CHANNEL) {
chan.pushMessage(this, new Msg({ chan.pushMessage(this, new Msg({
type: Msg.Type.ERROR, type: Msg.Type.ERROR,
@ -14,6 +14,6 @@ exports.input = function(network, chan, cmd, args) {
return; return;
} }
network.irc.setTopic(chan.name, args.join(" ")); irc.setTopic(chan.name, args.join(" "));
return true; return true;
}; };

View File

@ -2,14 +2,14 @@
exports.commands = ["whois"]; exports.commands = ["whois"];
exports.input = function(network, chan, cmd, args) { exports.input = function({irc}, chan, cmd, args) {
if (args.length === 1) { if (args.length === 1) {
// This queries server of the other user and not of the current user, which // This queries server of the other user and not of the current user, which
// does not know idle time. // does not know idle time.
// See http://superuser.com/a/272069/208074. // See http://superuser.com/a/272069/208074.
network.irc.raw("WHOIS", args[0], args[0]); irc.raw("WHOIS", args[0], args[0]);
} else { } else {
// Re-assembling the command parsed in client.js // Re-assembling the command parsed in client.js
network.irc.raw(`${cmd} ${args.join(" ")}`); irc.raw(`${cmd} ${args.join(" ")}`);
} }
}; };

View File

@ -114,7 +114,7 @@ module.exports = function(irc, network) {
} }
// No prefetch URLs unless are simple MESSAGE or ACTION types // 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); LinkPrefetch(client, chan, msg);
} }

View File

@ -94,7 +94,7 @@ module.exports = function(irc, network) {
if (!add) { if (!add) {
_.pull(user.modes, changedMode); _.pull(user.modes, changedMode);
} else if (user.modes.indexOf(changedMode) === -1) { } else if (!user.modes.includes(changedMode)) {
user.modes.push(changedMode); user.modes.push(changedMode);
user.modes.sort(function(a, b) { user.modes.sort(function(a, b) {
return userModeSortPriority[a] - userModeSortPriority[b]; return userModeSortPriority[a] - userModeSortPriority[b];

View File

@ -38,13 +38,13 @@ class WebPush {
} }
push(client, payload, onlyToOffline) { push(client, payload, onlyToOffline) {
_.forOwn(client.config.sessions, (session, token) => { _.forOwn(client.config.sessions, ({pushSubscription}, token) => {
if (session.pushSubscription) { if (pushSubscription) {
if (onlyToOffline && _.find(client.attachedClients, {token: token}) !== undefined) { if (onlyToOffline && _.find(client.attachedClients, {token}) !== undefined) {
return; 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) { if (error.statusCode >= 400 && error.statusCode < 500) {
log.warn(`WebPush subscription for ${client.name} returned an error (${error.statusCode}), removing subscription`); log.warn(`WebPush subscription for ${client.name} returned an error (${error.statusCode}), removing subscription`);
_.forOwn(client.config.sessions, (session, token) => { _.forOwn(client.config.sessions, ({pushSubscription}, token) => {
if (session.pushSubscription && session.pushSubscription.endpoint === subscription.endpoint) { if (pushSubscription && pushSubscription.endpoint === subscription.endpoint) {
client.unregisterPushSubscription(token); client.unregisterPushSubscription(token);
} }
}); });

View File

@ -20,8 +20,8 @@ describe("Commands", function() {
lastCommand: null, lastCommand: null,
nick: "xPaw", nick: "xPaw",
irc: { irc: {
raw: function() { raw: function(...args) {
testableNetwork.lastCommand = Array.prototype.join.call(arguments, " "); testableNetwork.lastCommand = args.join(" ");
}, },
}, },
}; };

View File

@ -24,11 +24,9 @@ MockClient.prototype.createMessage = function(opts) {
}; };
function mockLogger(callback) { function mockLogger(callback) {
return function() { return function(...args) {
// TODO: Use ...args with The Lounge v3: add `...args` as function argument // Concats and removes ANSI colors. See https://stackoverflow.com/a/29497680
// and replaced the next line with `args.join(", ")` const stdout = args.join(" ").replace(
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, /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g,
"" ""
); );

View File

@ -101,7 +101,7 @@ const config = {
// automatically split all vendor dependencies into a separate bundle // automatically split all vendor dependencies into a separate bundle
new webpack.optimize.CommonsChunkPlugin({ new webpack.optimize.CommonsChunkPlugin({
name: "js/bundle.vendor.js", name: "js/bundle.vendor.js",
minChunks: (module) => module.context && module.context.indexOf("node_modules") !== -1, minChunks: (module) => module.context && module.context.includes("node_modules"),
}), }),
], ],
}; };