Mirror: The magical sticky regex-based parser generator 🧙
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Readd string matching logic to core.js

+35 -8
+21 -8
src/core.js
··· 5 5 6 6 export const __private = { 7 7 pattern(input) { 8 - if (typeof input === 'function') return input; 8 + if (typeof input === 'function' || typeof input === 'string') { 9 + return input; 10 + } 11 + 9 12 const source = typeof input !== 'string' ? input.source : input; 10 13 return isStickySupported 11 14 ? new RegExp(source, 'y') ··· 21 24 } 22 25 23 26 const input = state.quasis[state.x]; 24 - if (input && (pattern.lastIndex = state.y) < input.length) { 25 - if (isStickySupported) { 26 - if (pattern.test(input)) 27 - match = input.slice(state.y, pattern.lastIndex); 27 + if (input && state.y < input.length) { 28 + if (typeof pattern === 'string') { 29 + const end = state.y + pattern.length; 30 + const sub = input.slice(state.y, end); 31 + if (sub === pattern) { 32 + state.y = end; 33 + match = sub; 34 + } 28 35 } else { 29 - match = pattern.exec(input)[0] || match; 30 - } 36 + pattern.lastIndex = state.y; 37 + if (isStickySupported) { 38 + if (pattern.test(input)) 39 + match = input.slice(state.y, pattern.lastIndex); 40 + } else { 41 + match = pattern.exec(input)[0] || match; 42 + } 31 43 32 - state.y = pattern.lastIndex; 44 + state.y = pattern.lastIndex; 45 + } 33 46 } 34 47 35 48 return match;
+14
src/core.test.js
··· 573 573 expect(parse(node)`1${1}3`).toBe(undefined); 574 574 }); 575 575 }); 576 + 577 + describe('string matching', () => { 578 + const node = match('node')` 579 + ${'1'} 580 + ${'2'} 581 + `; 582 + 583 + it('matches strings', () => { 584 + const expected = ['1', '2']; 585 + expected.tag = 'node'; 586 + expect(parse(node)('12')).toEqual(expected); 587 + expect(parse(node)('13')).toBe(undefined); 588 + }); 589 + });