Browse Source

[refactor] Abstracted getText and extractCommand

master
Muthu Kumar 6 years ago
parent
commit
f919dd52c7
  1. 14
      app.js
  2. 6
      util/index.js

14
app.js

@ -10,7 +10,7 @@ const Telegraf = require('telegraf');
const config = require('./config.js');
// Utils
const { path } = require('./util/index.js');
const { path, getText, extractCommand } = require('./util/index.js');
// Lib
const validator = require('./lib/validator.js');
@ -65,8 +65,8 @@ bot.command('ls',
bot.command('attach',
ctx => {
const text = path(['update', 'message', 'text'], ctx);
const sessionIndex = parseInt(text.replace('/attach ', '').trim());
const text = getText(ctx);
const sessionIndex = parseInt(extractCommand('attach')(text));
if(Number.isNaN(sessionIndex) || !sessions[sessionIndex])
return responder.fail('Session not found. /ls for list of sessions')(ctx);
sessions.currentSession = sessions[sessionIndex];
@ -76,8 +76,8 @@ bot.command('attach',
bot.command('detach',
ctx => {
const text = path(['update', 'message', 'text'], ctx);
const sessionIndex = parseInt(text.replace('/detach ', '').trim());
const text = getText(ctx);
const sessionIndex = parseInt(extractCommand('detach')(text));
const currentSession =
text.trim() === '/detach'
? sessions.currentSession : sessions[sessionIndex];
@ -90,8 +90,8 @@ bot.command('detach',
bot.command('kill',
ctx => {
const text = path(['update', 'message', 'text'], ctx);
const sessionIndex = parseInt(text.replace('/kill ', '').trim());
const text = getText(ctx);
const sessionIndex = parseInt(extractCommand('kill')(text));
if(Number.isNaN(sessionIndex) || !sessions[sessionIndex])
return responder.fail('Session not found. /ls for list of sessions.')(ctx);
const disconnect = sessions[sessionIndex];

6
util/index.js

@ -1,5 +1,11 @@
const path = (path, obj) => path.reduce((result, segment) => result && result[segment], obj);
const compose = (...fns) => fns.reduce((f, g) => (...args) => f(g(...args)));
const getText = ctx => path(['update', 'message', 'text'], ctx) || '';
const extractCommand = cmd => text => text.replace(`/${cmd}`, '').trim();
module.exports = {
path,
compose,
getText,
extractCommand,
};

Loading…
Cancel
Save