Fix knuthMorrisPratt for empty word request (#101)

This commit is contained in:
Nicolas DUBIEN 2018-07-24 11:26:22 +02:00 committed by Oleksii Trekhleb
parent 76461f29ee
commit 0361fe5cf8
2 changed files with 6 additions and 2 deletions

View File

@ -2,8 +2,8 @@ import knuthMorrisPratt from '../knuthMorrisPratt';
describe('knuthMorrisPratt', () => {
it('should find word position in given text', () => {
expect(knuthMorrisPratt('', '')).toBe(-1);
expect(knuthMorrisPratt('a', '')).toBe(-1);
expect(knuthMorrisPratt('', '')).toBe(0);
expect(knuthMorrisPratt('a', '')).toBe(0);
expect(knuthMorrisPratt('a', 'a')).toBe(0);
expect(knuthMorrisPratt('abcbcglx', 'abca')).toBe(-1);
expect(knuthMorrisPratt('abcbcglx', 'bcgl')).toBe(3);

View File

@ -30,6 +30,10 @@ function buildPatternTable(word) {
* @return {number}
*/
export default function knuthMorrisPratt(text, word) {
if (word.length === 0) {
return 0;
}
let textIndex = 0;
let wordIndex = 0;