@@ -30,22 +30,45 @@ export function isStringPunctuated(text) {
3030// Filters a list of tokens by token type only when they match
3131// a specific token type order.
3232// For example, if a list of tokens contains:
33- // [ { type: 'inline'}, { type: 'list_item_close'}, { type: 'list_item_open'},
34- // { type: 'paragraph_open'}, { type: 'inline'}, { type: 'paragraph_close'}]
35- // And if the tokenOrder being looked for is
36- // `tokenOrder` defined ['list_item_open', 'paragraph_open', 'inline']
33+ //
34+ // [
35+ // { type: 'inline'},
36+ // { type: 'list_item_close'},
37+ // { type: 'list_item_open'},
38+ // { type: 'paragraph_open'},
39+ // { type: 'inline'},
40+ // { type: 'paragraph_close'},
41+ // ]
42+ //
43+ // And if the `tokenOrder` being looked for is:
44+ //
45+ // [
46+ // 'list_item_open',
47+ // 'paragraph_open',
48+ // 'inline'
49+ // ]
50+ //
3751// Then the return value would be the items that match that seaquence:
3852// Index 2-4:
39- // [{ type: 'list_item_open'}, { type: 'paragraph_open'}, { type: 'inline'}]
53+ // [
54+ // { type: 'inline'}, <-- Index 0 - NOT INCLUDED
55+ // { type: 'list_item_close'}, <-- Index 1 - NOT INCLUDED
56+ // { type: 'list_item_open'}, <-- Index 2 - INCLUDED
57+ // { type: 'paragraph_open'}, <-- Index 3 - INCLUDED
58+ // { type: 'inline'}, <-- Index 4 - INCLUDED
59+ // { type: 'paragraph_close'}, <-- Index 5 - NOT INCLUDED
60+ // ]
61+ //
4062export function filterTokensByOrder ( tokens , tokenOrder ) {
4163 const matches = [ ]
4264
4365 // Get a list of token indexes that match the
4466 // first token (root) in the tokenOrder array
4567 const tokenRootIndexes = [ ]
68+ const firstTokenOrderType = tokenOrder [ 0 ]
4669 tokens . forEach ( ( token , index ) => {
47- if ( token . type === tokenOrder [ 0 ] ) {
48- return tokenRootIndexes . push ( index )
70+ if ( token . type === firstTokenOrderType ) {
71+ tokenRootIndexes . push ( index )
4972 }
5073 } )
5174
@@ -54,7 +77,10 @@ export function filterTokensByOrder(tokens, tokenOrder) {
5477 for ( const tokenRootIndex of tokenRootIndexes ) {
5578 for ( let i = 1 ; i < tokenOrder . length ; i ++ ) {
5679 if ( tokens [ tokenRootIndex + i ] . type !== tokenOrder [ i ] ) {
57- return
80+ // This tokenRootIndex was a possible start,
81+ // but doesn't match the tokenOrder perfectly, so break out
82+ // of the inner loop before it reaches the end.
83+ break
5884 }
5985 if ( i === tokenOrder . length - 1 ) {
6086 matches . push ( ...tokens . slice ( tokenRootIndex , tokenRootIndex + i + 1 ) )
0 commit comments