diff --git a/build/functions/argument-parser.js b/build/functions/argument-parser.js index bc29f5a..1c6648b 100644 --- a/build/functions/argument-parser.js +++ b/build/functions/argument-parser.js @@ -13,6 +13,9 @@ var FORMAT_REST = /\.{3}(\w+)/g; var ESCAPABLE = '.^$*+?()[{\\|}]'.split(''); +var REQUIRED = 0; +var OPTIONAL = 1; + /** * Parses a message for arguments, based on format * @@ -46,23 +49,28 @@ function argumentParser(format, string) { string = string.replace(/[^\s]+/, '').trim(); format = format.replace(/[^\s]+/, '').trim(); - if (!string || !format) { + if (!format) { return {}; } - var indexes = []; + if (!string) return { args: {}, params: params }; + + var indexes = [], + params = {}; format = format.replace(/\s/g, '\\s*'); format = format.replace(FORMAT_REQUIRED, function (f, symbols, arg, type, offset) { if (type === undefined) type = 'word'; indexes.push({ arg: arg, offset: offset }); + params[arg] = REQUIRED; return (escape(symbols) + getFormat(type, 'required')).trim(); }); format = format.replace(FORMAT_OPTIONAL, function (f, symbols, arg, type, offset) { if (type === undefined) type = 'word'; indexes.push({ arg: arg, offset: offset }); + params[arg] = OPTIONAL; return (escape(symbols, '?') + getFormat(type, 'optional')).trim(); }); format = format.replace(FORMAT_REST, function (full, arg, offset) { @@ -109,7 +117,7 @@ function argumentParser(format, string) { } } - return object; + return { args: object, params: params }; } function escape(symbols) { diff --git a/build/index.js b/build/index.js index 6f3e16f..d0fa549 100644 --- a/build/index.js +++ b/build/index.js @@ -196,7 +196,6 @@ var Bot = (function (_EventEmitter) { } update.forEach(function (res) { - console.log(res); var text = res.message.text; if (!text) return; diff --git a/lib/functions/argument-parser.js b/lib/functions/argument-parser.js index 8589bad..622c8c4 100644 --- a/lib/functions/argument-parser.js +++ b/lib/functions/argument-parser.js @@ -4,6 +4,9 @@ const FORMAT_REST = /\.{3}(\w+)/g; const ESCAPABLE = '.^$*+?()[{\\|}]'.split(''); +const REQUIRED = 0; +const OPTIONAL = 1; + /** * Parses a message for arguments, based on format * @@ -36,21 +39,26 @@ export default function argumentParser(format, string) { string = string.replace(/[^\s]+/, '').trim(); format = format.replace(/[^\s]+/, '').trim(); - if (!string || !format) { + if (!format) { return {}; } - let indexes = []; + if (!string) return {args: {}, params}; + + let indexes = [], + params = {}; format = format.replace(/\s/g, '\\s*'); format = format.replace(FORMAT_REQUIRED, (f, symbols, arg, type = 'word', offset) => { indexes.push({arg, offset}); + params[arg] = REQUIRED; return (escape(symbols) + getFormat(type, 'required')).trim(); }); format = format.replace(FORMAT_OPTIONAL, (f, symbols, arg, type = 'word', offset) => { indexes.push({arg, offset}); + params[arg] = OPTIONAL; return (escape(symbols, '?') + getFormat(type, 'optional')).trim(); }); format = format.replace(FORMAT_REST, (full, arg, offset) => { @@ -73,7 +81,7 @@ export default function argumentParser(format, string) { object[argument.arg] = match; } - return object; + return {args: object, params}; } function escape(symbols, append = '') {