change return data in argument-parser.

This commit is contained in:
Ali Movahedi 2015-07-09 00:42:11 +04:30
parent bdfd4ee99d
commit 477f99b40c
3 changed files with 22 additions and 7 deletions

View File

@ -13,6 +13,9 @@ var FORMAT_REST = /\.{3}(\w+)/g;
var ESCAPABLE = '.^$*+?()[{\\|}]'.split(''); var ESCAPABLE = '.^$*+?()[{\\|}]'.split('');
var REQUIRED = 0;
var OPTIONAL = 1;
/** /**
* Parses a message for arguments, based on format * Parses a message for arguments, based on format
* *
@ -46,23 +49,28 @@ function argumentParser(format, string) {
string = string.replace(/[^\s]+/, '').trim(); string = string.replace(/[^\s]+/, '').trim();
format = format.replace(/[^\s]+/, '').trim(); format = format.replace(/[^\s]+/, '').trim();
if (!string || !format) { if (!format) {
return {}; return {};
} }
var indexes = []; if (!string) return { args: {}, params: params };
var indexes = [],
params = {};
format = format.replace(/\s/g, '\\s*'); format = format.replace(/\s/g, '\\s*');
format = format.replace(FORMAT_REQUIRED, function (f, symbols, arg, type, offset) { format = format.replace(FORMAT_REQUIRED, function (f, symbols, arg, type, offset) {
if (type === undefined) type = 'word'; if (type === undefined) type = 'word';
indexes.push({ arg: arg, offset: offset }); indexes.push({ arg: arg, offset: offset });
params[arg] = REQUIRED;
return (escape(symbols) + getFormat(type, 'required')).trim(); return (escape(symbols) + getFormat(type, 'required')).trim();
}); });
format = format.replace(FORMAT_OPTIONAL, function (f, symbols, arg, type, offset) { format = format.replace(FORMAT_OPTIONAL, function (f, symbols, arg, type, offset) {
if (type === undefined) type = 'word'; if (type === undefined) type = 'word';
indexes.push({ arg: arg, offset: offset }); indexes.push({ arg: arg, offset: offset });
params[arg] = OPTIONAL;
return (escape(symbols, '?') + getFormat(type, 'optional')).trim(); return (escape(symbols, '?') + getFormat(type, 'optional')).trim();
}); });
format = format.replace(FORMAT_REST, function (full, arg, offset) { 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) { function escape(symbols) {

View File

@ -196,7 +196,6 @@ var Bot = (function (_EventEmitter) {
} }
update.forEach(function (res) { update.forEach(function (res) {
console.log(res);
var text = res.message.text; var text = res.message.text;
if (!text) return; if (!text) return;

View File

@ -4,6 +4,9 @@ const FORMAT_REST = /\.{3}(\w+)/g;
const ESCAPABLE = '.^$*+?()[{\\|}]'.split(''); const ESCAPABLE = '.^$*+?()[{\\|}]'.split('');
const REQUIRED = 0;
const OPTIONAL = 1;
/** /**
* Parses a message for arguments, based on format * Parses a message for arguments, based on format
* *
@ -36,21 +39,26 @@ export default function argumentParser(format, string) {
string = string.replace(/[^\s]+/, '').trim(); string = string.replace(/[^\s]+/, '').trim();
format = format.replace(/[^\s]+/, '').trim(); format = format.replace(/[^\s]+/, '').trim();
if (!string || !format) { if (!format) {
return {}; return {};
} }
let indexes = []; if (!string) return {args: {}, params};
let indexes = [],
params = {};
format = format.replace(/\s/g, '\\s*'); format = format.replace(/\s/g, '\\s*');
format = format.replace(FORMAT_REQUIRED, format = format.replace(FORMAT_REQUIRED,
(f, symbols, arg, type = 'word', offset) => { (f, symbols, arg, type = 'word', offset) => {
indexes.push({arg, offset}); indexes.push({arg, offset});
params[arg] = REQUIRED;
return (escape(symbols) + getFormat(type, 'required')).trim(); return (escape(symbols) + getFormat(type, 'required')).trim();
}); });
format = format.replace(FORMAT_OPTIONAL, format = format.replace(FORMAT_OPTIONAL,
(f, symbols, arg, type = 'word', offset) => { (f, symbols, arg, type = 'word', offset) => {
indexes.push({arg, offset}); indexes.push({arg, offset});
params[arg] = OPTIONAL;
return (escape(symbols, '?') + getFormat(type, 'optional')).trim(); return (escape(symbols, '?') + getFormat(type, 'optional')).trim();
}); });
format = format.replace(FORMAT_REST, (full, arg, offset) => { format = format.replace(FORMAT_REST, (full, arg, offset) => {
@ -73,7 +81,7 @@ export default function argumentParser(format, string) {
object[argument.arg] = match; object[argument.arg] = match;
} }
return object; return {args: object, params};
} }
function escape(symbols, append = '') { function escape(symbols, append = '') {