Skip to content

timgoeller/peekable-array-iterator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

Peekable Array Iterator

This module allows iterating over an array (or any other object with an [] accessor and a length attribute, like a string for example) and creates an iterator for peeking at each iteration step.

Example

const PeekableArrayIterator = require('peekable-array-iterator')

const it = new PeekableArrayIterator('HelloWorld')

console.log(it.next().value) // prints 'H'
console.log(it.next().value) // prints 'e'

console.log(it.peek.next().value) // prints 'l'
console.log(it.peek.next().value) // prints 'l'
console.log(it.peek.next().value) // prints 'o'

console.log(it.next().value) // prints 'l'
console.log(it.next().value) // prints 'l'

for (const peek of it.peek) {
  console.log(peek) 
  // prints 'oWorld' in seperate lines
}

API

const it = new PeekableArrayIterator(container, [start])

This creates a new iterator with the container to be iterated over (any object with an [] accessor and a length attribute, like an array or a string). Optionally, a start value can be passed to set the starting index. The starting index is 0 by default. The iterator (aswell, as the peek iterator) uses the standard interface for iterators [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators]. This is allows the usage in for-of loops.

it.next()

Return next entry. The returned object has the following form:

  {
    data: The next value
    done: `true`, until there is no next value available
  }

it.peek

Iterator starting at the current index.

it.peek.next()

Same as it.next(), but won't modify the state of it.

About

Array iterator containing an iterator for peeking

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published