Skip to content

Commit

Permalink
Add skipping methods to main class
Browse files Browse the repository at this point in the history
  • Loading branch information
make-github-pseudonymous-again committed Aug 8, 2018
1 parent e2ad643 commit 61ab0e9
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 13 deletions.
89 changes: 85 additions & 4 deletions lib/TapeFromCallable.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 24 additions & 3 deletions src/TapeFromCallable.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default class TapeFromCallable {
}

/**
* Returns the next token in the tape or {@link TapeFromCallable#eof}
* Returns the next token on the tape or {@link TapeFromCallable#eof}
* if the tape has been exhausted.
*
* @returns {Object}
Expand All @@ -37,10 +37,10 @@ export default class TapeFromCallable {
}

/**
* Puts a token back in the tape. If {@link TapeFromCallable#read} is
* Puts a token back on the tape. If {@link TapeFromCallable#read} is
* used just after, this token will be returned.
*
* @param {Object} token - The token to put back in the tape.
* @param {Object} token - The token to put back on the tape.
* @returns {TapeFromCallable} Self, for chaining.
*/
unread ( token ) {
Expand All @@ -52,4 +52,25 @@ export default class TapeFromCallable {

}

/**
* Skips the next token on the tape.
*/
async skip ( ) {

if ( this.buffer.length > 0 ) this.buffer.pop( ) ;
else await this.callable( ) ;

}

/**
* Skip the next `n` tokens on the tape.
*
* @param {Number} n - The number of tokens to skip.
*/
async skipMany ( n ) {

while ( n --> 0 ) await this.skip() ;

}

}
11 changes: 5 additions & 6 deletions test/src/abracadabra.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import tape from '../../src' ;
* @test {fromString}
* @test {TapeFromCallable#read}
* @test {TapeFromCallable#unread}
* @test {TapeFromCallable#skip}
* @test {TapeFromCallable#skipMany}
*/
test( 'As a promise' , t => {

Expand All @@ -20,18 +22,15 @@ test( 'As a promise' , t => {
.then( c => t.is(c, 'X') )
.then( () => myTape.read() )
.then( c => t.is(c, 'Z') )
.then( () => myTape.skipMany(0) )
.then( () => myTape.read() )
.then( c => t.is(c, 'r') )
.then( () => myTape.read() )
.then( c => t.is(c, 'a') )
.then( () => myTape.read() )
.then( c => t.is(c, 'c') )
.then( () => myTape.read() )
.then( c => t.is(c, 'a') )
.then( () => myTape.skipMany(2) )
.then( () => myTape.read() )
.then( c => t.is(c, 'd') )
.then( () => myTape.read() )
.then( c => t.is(c, 'a') )
.then( () => myTape.skip() )
.then( () => myTape.read() )
.then( c => t.is(c, 'b') )
.then( () => myTape.read() )
Expand Down

0 comments on commit 61ab0e9

Please sign in to comment.