Add Argument Parser 💪
This commit is contained in:
98
lib/functions/argument-parser.js
Normal file
98
lib/functions/argument-parser.js
Normal file
@ -0,0 +1,98 @@
|
||||
const FORMAT_REQUIRED = /<(\W*)(\w+)\|?(\w+)?>/g;
|
||||
const FORMAT_OPTIONAL = /\[(\W*)(\w+)\|?(\w+)?\]/g;
|
||||
const FORMAT_REST = /\.{3}(\w+)/g;
|
||||
|
||||
const ESCAPABLE = '.^$*+?()[{\\|}]'.split('');
|
||||
|
||||
/**
|
||||
* Parses a message for arguments, based on format
|
||||
*
|
||||
* The format option may include '<requiredParam>' and '[optionalParam]' and
|
||||
* '...[restParam]'
|
||||
* <requiredParam> indicates a required, single-word argument
|
||||
* [optionalParam] indicates an optinal, single-word argument
|
||||
* ...[restParam] indicates a multi-word argument which records until end
|
||||
*
|
||||
* You can define a type for your arguments using pipe | sign, like this:
|
||||
* [count|number]
|
||||
* Supported Types are: number and word, defaults to word
|
||||
*
|
||||
* Example:
|
||||
* format: '<name> [count|number] ...text'
|
||||
* string 1: 'Someone Hey, wassup'
|
||||
* {name: 'Someone',
|
||||
* count: undefined,
|
||||
* text: 'Hey, wassup'}
|
||||
*
|
||||
* string 2: 'Someone 5 Hey, wassup'
|
||||
* {name: 'Someone',
|
||||
* count: 5,
|
||||
* text: 'Hey, wassup'}
|
||||
* @param {string} format Format, as described above
|
||||
* @param {string} string The message to parse
|
||||
* @return {object} Parsed arguments
|
||||
*/
|
||||
export default function argumentParser(format, string) {
|
||||
string = string.replace(/[^\s]+/, '');
|
||||
format = format.replace(/[^\s]+/, '');
|
||||
let indexes = [];
|
||||
|
||||
format = format.replace(/\s/g, '\\s*');
|
||||
format = format.replace(FORMAT_REQUIRED,
|
||||
(f, symbols, arg, type = 'word', offset) => {
|
||||
indexes.push({arg, offset});
|
||||
return (escape(symbols) + getFormat(type, 'required')).trim();
|
||||
});
|
||||
format = format.replace(FORMAT_OPTIONAL,
|
||||
(f, symbols, arg, type = 'word', offset) => {
|
||||
indexes.push({arg, offset});
|
||||
return (escape(symbols, '?') + getFormat(type, 'optional')).trim();
|
||||
});
|
||||
format = format.replace(FORMAT_REST, (full, arg, offset) => {
|
||||
indexes.push({offset, arg});
|
||||
return getFormat(null, 'rest');
|
||||
});
|
||||
|
||||
indexes = indexes.sort((a, b) => {
|
||||
return a.offset < b.offset ? -1 : 1;
|
||||
});
|
||||
|
||||
console.log(format);
|
||||
const regex = new RegExp(format);
|
||||
|
||||
const matched = regex.exec(string).slice(1);
|
||||
|
||||
const object = {};
|
||||
for (let [index, match] of matched.entries()) {
|
||||
const argument = indexes[index];
|
||||
|
||||
object[argument.arg] = match;
|
||||
}
|
||||
|
||||
return object;
|
||||
}
|
||||
|
||||
function escape(symbols, append = '') {
|
||||
return symbols.split('').map(symbol => {
|
||||
return (ESCAPABLE.indexOf(symbol) ? `\\${symbol}` : symbol) + append;
|
||||
}).join('');
|
||||
}
|
||||
|
||||
|
||||
const TYPES = {
|
||||
'number': '\\d',
|
||||
'word': '\\w'
|
||||
};
|
||||
|
||||
function getFormat(type = 'word', param = 'required') {
|
||||
const t = TYPES[type];
|
||||
|
||||
switch (param) {
|
||||
case 'required':
|
||||
return `(${t}+)`;
|
||||
case 'optional':
|
||||
return `(${t}+)?`;
|
||||
case 'rest':
|
||||
return `(.*)`;
|
||||
}
|
||||
}
|
20
lib/index.js
20
lib/index.js
@ -1,6 +1,7 @@
|
||||
import API from './api';
|
||||
import webhook from './webhook';
|
||||
import poll from './poll';
|
||||
import API from './functions/api';
|
||||
import webhook from './functions/webhook';
|
||||
import poll from './functions/poll';
|
||||
import argumentParser from './functions/argument-parser';
|
||||
import {EventEmitter} from 'events';
|
||||
|
||||
const DEFAULTS = {
|
||||
@ -100,14 +101,19 @@ export default class Bot extends EventEmitter {
|
||||
|
||||
/**
|
||||
* Listens on a command
|
||||
* @param {string} cmd the command string, should not include slash (/)
|
||||
* @param {string} command the command string, should not include slash (/)
|
||||
* @param {function} listener function to call when the command is received,
|
||||
* gets the update
|
||||
* @return {object} returns the bot object
|
||||
*/
|
||||
command(cmd, listener) {
|
||||
command(command, listener) {
|
||||
const regex = /[^\s]+/;
|
||||
|
||||
const cmd = command.match(regex)[0];
|
||||
|
||||
this._userEvents.push({
|
||||
pattern: new RegExp(`^/${cmd}`),
|
||||
parse: argumentParser.bind(null, command),
|
||||
listener
|
||||
});
|
||||
|
||||
@ -156,6 +162,10 @@ export default class Bot extends EventEmitter {
|
||||
return;
|
||||
}
|
||||
|
||||
if (ev.parse) {
|
||||
res.message.args = ev.parse(res.message.text);
|
||||
}
|
||||
|
||||
ev.listener(res.message);
|
||||
});
|
||||
}
|
||||
|
Reference in New Issue
Block a user