Skip to content

xgbuils/slice-iterable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

slice-iterable

travis ci npm version Coverage Status Dependency Status

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

About

iterable decorated with slice method

Resources

Stars

Watchers

Forks

Packages

No packages published