chore(lint): update eslint configuration, re-lint

chore(gitignore): don't ignore src/types
This commit is contained in:
Mahdi Dibaiee
2016-03-28 20:40:59 +04:30
parent a357cd7368
commit c76eb0c64d
12 changed files with 87 additions and 132 deletions

View File

@ -20,7 +20,7 @@ export default class API {
}
API.prototype.request = function request(method, data) {
return fetch(this.token + '/' + method, data);
return fetch(`${this.token}/${method}`, data);
};
const methods = ['getMe', 'sendMessage', 'forwardMessage', 'sendPhoto',
@ -29,7 +29,7 @@ const methods = ['getMe', 'sendMessage', 'forwardMessage', 'sendPhoto',
'getUpdates', 'setWebhook'];
methods.forEach(method => {
API.prototype[method] = function(data) {
API.prototype[method] = function(data) { //eslint-disable-line
return this.request(method, data);
};
});

View File

@ -8,6 +8,32 @@ const REQUIRED = 0;
const OPTIONAL = 1;
const REST = 2;
function escape(symbols, append = '') {
return symbols.split('').map(symbol =>
(ESCAPABLE.indexOf(symbol) ? `\\${symbol}` : symbol) + append
).join('');
}
const TYPES = {
number: '\\d',
word: '\\S'
};
function getFormat(type = 'word', param = 'required') {
const t = TYPES[type];
switch (param) { // eslint-disable-line
case 'required':
return `(${t}+)`;
case 'optional':
return `(${t}+)?`;
case 'rest':
return '(.*)';
}
return '';
}
/**
* Parses a message for arguments, based on format
*
@ -41,74 +67,49 @@ export default function argumentParser(format, string) {
format = format.replace(/[^\s]+/, '').trim();
if (!format) {
return {args: {}, params: {}};
return { args: {}, params: {} };
}
let indexes = [],
params = {};
let indexes = [];
const params = {};
format = format.replace(/\s/g, '\\s*');
format = format.replace(FORMAT_REQUIRED,
(f, symbols, arg, type = 'word', offset) => {
indexes.push({arg, 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});
indexes.push({ arg, offset });
params[arg] = OPTIONAL;
return (escape(symbols, '?') + getFormat(type, 'optional')).trim();
});
format = format.replace(FORMAT_REST, (full, arg, offset) => {
indexes.push({offset, arg});
indexes.push({ offset, arg });
params[arg] = REST;
return getFormat(null, 'rest');
});
if (!string) {
return {args: {}, params};
return { args: {}, params };
}
indexes = indexes.sort((a, b) => {
return a.offset < b.offset ? -1 : 1;
});
indexes = indexes.sort((a, b) =>
(a.offset < b.offset ? -1 : 1)
);
const regex = new RegExp(format);
const matched = regex.exec(string).slice(1);
const object = {};
for (let [index, match] of matched.entries()) {
for (const [index, match] of matched.entries()) {
const argument = indexes[index];
object[argument.arg] = match;
}
return {args: object, params};
}
function escape(symbols, append = '') {
return symbols.split('').map(symbol => {
return (ESCAPABLE.indexOf(symbol) ? `\\${symbol}` : symbol) + append;
}).join('');
}
const TYPES = {
'number': '\\d',
'word': '\\S'
};
function getFormat(type = 'word', param = 'required') {
const t = TYPES[type];
switch (param) {
case 'required':
return `(${t}+)`;
case 'optional':
return `(${t}+)?`;
case 'rest':
return `(.*)`;
}
return { args: object, params };
}

View File

@ -4,14 +4,14 @@ export default function fetch(path, data = {}) {
return new Promise((resolve, reject) => {
const files = {};
for (let key of Object.keys(data)) {
for (const key of Object.keys(data)) {
if (data[key].file) {
files[key] = data[key].file;
delete data[key];
}
}
unirest.post('https://api.telegram.org/bot' + path)
unirest.post(`https://api.telegram.org/bot${path}`)
.field(data)
.attach(files)
.end(response => {

View File

@ -1,6 +1,6 @@
import https from 'http';
import qs from 'qs';
import {getBody} from './fetch';
import { getBody } from './fetch';
const DEFAULTS = {
server: {},
@ -11,14 +11,12 @@ export default function webhook(options = {}, bot) {
options = Object.assign(DEFAULTS, options);
return bot.api.setWebhook(options.url).then(() => {
bot._webhookServer = https.createServer(options.server, (req, res) => {
return getBody(req).then(data => {
bot._webhookServer = https.createServer(options.server, (req, res) =>
getBody(req).then(data => {
bot.emit('update', qs.parse(data).result);
res.end('OK');
});
}).listen(options.port);
})
).listen(options.port);
});
}