slice-iterable
v0.1.3
Published
iterable decorated with slice method
Downloads
5
Readme
slice-iterable
slice-iterable
exports a class that builds iterables that provide slice method.
Install
$ npm install slice-iterable --save
Usage
const SliceIterable = require('slice-iterable')
const iterable = new SliceIterable(new Set([4, 2, 7, 8, 4, 7])) // (4 2 7 8 4 7)
.slice(0, 5) // (4 2 7 8 7)
.slice(2, 5) // (7 8 7)
.slice(1, 2) // (8)
// converting to array:
[...iterable] // [8]
// traversing values:
for (const val of iterable) {
// ...
}
// creating an iterator that traverses the values
let iterator = iterable[Symbol.iterator]()
iterator.next() // {value: 8, done: false}
iterator.next() // {value: undefined, done: true}
// Infinite iterable
const naturals = {
[Symbol.iterator]: function* () {
let i = 1
while(true) { yield i++ }
}
} // (1 2 3 4...)
new SliceIterable(naturals) // (1 2 3 4 5 6 7 8 9...)
.slice(3, 8) // (4 5 6 7 8)
Support
- Node.js >=6
- ES2015 transpilers
License
MIT