2
0
mirror of https://github.com/yagop/node-telegram-bot-api synced 2025-09-04 00:05:26 +00:00

src/telegram: reset state of global regexp (#326)

References:

  * BR: https://github.com/yagop/node-telegram-bot-api/issues/325
  * BR-by: @Sirius-A
  * PR: https://github.com/yagop/node-telegram-bot-api/pull/326
  * PR-by: @Sirius-A
This commit is contained in:
Fabio Zuber
2017-05-13 09:53:21 +02:00
committed by Gocho Mugo
parent 3d4d10f9b3
commit 9f3107b5ab
2 changed files with 12 additions and 0 deletions

View File

@@ -473,6 +473,8 @@ class TelegramBot extends EventEmitter {
if (!result) { if (!result) {
return false; return false;
} }
// reset index so we start at the beginning of the regex each time
reg.regexp.lastIndex = 0;
debug('Matches %s', reg.regexp); debug('Matches %s', reg.regexp);
reg.callback(message, result); reg.callback(message, result);
// returning truthy value exits .some // returning truthy value exits .some

View File

@@ -975,6 +975,16 @@ describe('TelegramBot', function telegramSuite() {
message: { text: '/onText ECHO ALOHA' }, message: { text: '/onText ECHO ALOHA' },
}); });
}); });
it('should reset the global regex state with each message', function test(done) {
const regexp = /\/onText (.+)/g;
botWebHook.onText(regexp, () => {
assert.equal(regexp.lastIndex, 0);
return done();
});
utils.sendWebHookMessage(webHookPort2, TOKEN, {
message: { text: '/onText ECHO ALOHA' },
});
});
}); });
describe.skip('#onReplyToMessage', function onReplyToMessageSuite() {}); describe.skip('#onReplyToMessage', function onReplyToMessageSuite() {});