change return data in argument-parser.
This commit is contained in:
parent
bdfd4ee99d
commit
477f99b40c
@ -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) {
|
||||
|
@ -196,7 +196,6 @@ var Bot = (function (_EventEmitter) {
|
||||
}
|
||||
|
||||
update.forEach(function (res) {
|
||||
console.log(res);
|
||||
var text = res.message.text;
|
||||
if (!text) return;
|
||||
|
||||
|
@ -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 = '') {
|
||||
|
Loading…
Reference in New Issue
Block a user