11
2+ /**
3+ * @param {string|object[] }
4+ * @returns {string|[] }
5+ */
6+ function empty ( value ) {
7+ return Array . isArray ( value ) ? [ ] : ''
8+ }
9+
210class Stream {
311
412 index = - 1
@@ -21,13 +29,6 @@ class Stream {
2129 return this . data [ this . index ]
2230 }
2331
24- /**
25- * @returns {string|object[] }
26- */
27- get empty ( ) {
28- return Array . isArray ( this . data ) ? [ ] : ''
29- }
30-
3132 /**
3233 * @yields {object|string}
3334 */
@@ -69,7 +70,7 @@ class Stream {
6970 const fallback = args [ 0 ] ?? null
7071 if ( typeof n === 'number' ) {
7172 if ( 1 < n ) {
72- let consumed = this . empty
73+ let consumed = empty ( this . data )
7374 while ( n -- && ! this . atEnd ( ) ) {
7475 consumed = consumed . concat ( data [ ++ this . index ] )
7576 }
@@ -95,7 +96,7 @@ class Stream {
9596 * @returns {string|object[] }
9697 */
9798 consumeRunOf ( ...items ) {
98- let consumed = this . empty
99+ let consumed = empty ( this . data )
99100 let last
100101 while ( items . some ( item => last = this . consume ( item ) ) ) {
101102 consumed = consumed . concat ( last )
@@ -108,8 +109,8 @@ class Stream {
108109 * @returns {string|object[] }
109110 */
110111 consumeUntil ( item ) {
111- const { data, empty , index : start } = this
112- let consumed = empty
112+ const { data, index : start } = this
113+ let consumed = empty ( data )
113114 while ( this . index < data . length ) {
114115 const next = data [ this . index + 1 ]
115116 if ( next === item ) {
@@ -143,7 +144,7 @@ class Stream {
143144 }
144145 switch ( size ) {
145146 case 0 :
146- return this . empty
147+ return empty ( this . data )
147148 case 1 :
148149 return this . data [ this . index + offset + size ]
149150 default :
@@ -164,7 +165,7 @@ class Stream {
164165 }
165166 switch ( size ) {
166167 case 0 :
167- return this . empty
168+ return empty ( this . data )
168169 case 1 :
169170 return this . data [ this . index - offset - size ]
170171 default :
0 commit comments