Skip to content
This repository has been archived by the owner on Dec 4, 2020. It is now read-only.

Commit

Permalink
Merge pull request #21 from localytics/support_aliasing_functions
Browse files Browse the repository at this point in the history
Support aliasing functions
  • Loading branch information
kddnewton committed Mar 22, 2016
2 parents fa86690 + 94ee425 commit 0df17b3
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
32 changes: 32 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var buildResponse = function (response_type, response) {
function SlackBot(config) {
this.config = config;
this.commands = {};
this.aliases = {};
}

// add a command
Expand All @@ -37,6 +38,22 @@ SlackBot.prototype.addCommand = function (command, args, desc, callback) {
this.commands[command] = { args: realArgs, desc: realDesc };
};

// alias a command so that it can be called by multiple names
SlackBot.prototype.aliasCommand = function (commandName) {
var argIndex;

if (!this.commands.hasOwnProperty(commandName)) {
throw new Error(commandName + ' is not a configured command');
}

for (argIndex = 1; argIndex < arguments.length; argIndex++) {
if (this.aliases.hasOwnProperty(arguments[argIndex])) {
throw new Error(arguments[argIndex] + ' is already aliased or is an invalid alias name');
}
this.aliases[arguments[argIndex]] = commandName;
}
};

// call a stored command
SlackBot.prototype.callCommand = function (commandName, options, callback) {
var args;
Expand Down Expand Up @@ -65,11 +82,14 @@ SlackBot.prototype.ephemeralResponse = function (response) {

// respond with a usage message
SlackBot.prototype.help = function (options, callback) {
var _this = this;
var helpText = '';
var aliasText;

Object.keys(this.commands).forEach(function (command) {
helpText += command;

// add argument description
if (this.commands[command].args.length) {
helpText += ' ' + this.commands[command].args.map(function (arg) {
var optionalArgName;
Expand All @@ -81,6 +101,15 @@ SlackBot.prototype.help = function (options, callback) {
}).join(' ');
}

// add alias description
aliasText = Object.keys(this.aliases).filter(function (alias) {
return _this.aliases[alias] === command;
});
if (aliasText.length) {
helpText += ' (' + aliasText.join(', ') + ')';
}

// add command description
helpText += ': ' + this.commands[command].desc + '\n';
}.bind(this));
helpText += 'help: display this help message';
Expand All @@ -104,6 +133,9 @@ SlackBot.prototype.findCommand = function (payload) {

for (commandNameLength = payload.length - 1; commandNameLength > 0; commandNameLength--) {
commandName = splitPayload.slice(0, commandNameLength).join(' ');
if (this.aliases.hasOwnProperty(commandName)) {
return { commandName: this.aliases[commandName], args: splitPayload.slice(commandNameLength) };
}
if (this.commands.hasOwnProperty(commandName)) {
return { commandName: commandName, args: splitPayload.slice(commandNameLength) };
}
Expand Down
27 changes: 27 additions & 0 deletions test/find-command-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,31 @@ describe('finding the right commands', function () {
var foundCommand = slackbot.findCommand('one four three arg1 arg2');
expect(foundCommand).to.deep.equal({ commandName: 'one', args: ['four', 'three', 'arg1', 'arg2'] });
});

it('correctly aliases a command', function () {
slackbot.aliasCommand('one two', 'alias');
expect(slackbot.aliases).to.deep.equal({ alias: 'one two' });
});

it('supports aliasing multiple times', function () {
slackbot.aliasCommand('one two', 'alias1', 'alias2');
slackbot.aliasCommand('one two', 'alias3');
expect(slackbot.aliases).to.deep.equal({ alias1: 'one two', alias2: 'one two', alias3: 'one two' });
});

it('finds the correct command when aliased', function () {
var foundCommand;
slackbot.aliasCommand('one two three', 'alias');
foundCommand = slackbot.findCommand('alias arg1 arg2');
expect(foundCommand).to.deep.equal({ commandName: 'one two three', args: ['arg1', 'arg2'] });
});

it('throws an error when attempting to alias a command that does not exist', function () {
expect(function () { slackbot.aliasCommand('invalid', 'alias'); }).to.throw(Error);
});

it('throws an error when attempting to use an invalid alias', function () {
slackbot.aliasCommand('one two', 'alias');
expect(function () { slackbot.aliasCommand('one', 'alias'); }).to.throw(Error);
});
});
4 changes: 3 additions & 1 deletion test/integration-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('integration', function () {
var slackbot = new SlackBot({ token: 'token' });
var assertHelp = function (event, commandContext) {
var descriptions = [
'testA: Test command A',
'testA (tA, A): Test command A',
'testB arg1 arg2 arg3:3: Test command B',
'testC arg1 arg2...: Test command C',
'help: display this help message'
Expand All @@ -35,6 +35,8 @@ describe('integration', function () {
cb(null, this.ephemeralResponse(options.args.arg2.join(' ')));
});

slackbot.aliasCommand('testA', 'tA', 'A');

beforeEach(function () {
context.done = sinon.spy();
});
Expand Down

0 comments on commit 0df17b3

Please sign in to comment.