0.0.11 • Published 8 years ago
most-nth v0.0.11
most-nth
Retrieves the event at ordinal index n from a most.js stream as a Promise. If n is negative (and the stream finishes), the nth element from the end is returned.
Installation
Using npm:
$ npm install --save most-nthIn Node.js:
const { nth, first, last } = require('most-nth');Usage
nth
stream.thru(nth(index)) -> Promise
stream: -a--b--c----d-->
stream.thru(nth(2)): cfirst
stream.thru(first) -> Promise
stream: -a--b--c----d-->
stream.thru(first): alast
stream.thru(last) -> Promise
stream: -a--b--c--d--|
stream.thru(last): dExamples
const most = require('most');
const { nth } = require('most-nth');
// Logs
// 4
most.iterate(x => x + 1, 0)
.take(9) // 9 first numbers
.thru(nth(4)) // Retrieve the event at index 4
.then(x => console.log(x))const most = require('most');
const { nth } = require('most-nth');
// Logs
// 7
most.iterate(x => x + 1, 0)
.take(9) // 9 first numbers
.thru(nth(-2)) // Retrieve the 2nd event from the end
.then(x => console.log(x))const most = require('most');
const { first } = require('most-nth');
// Logs
// 0
most.iterate(x => x + 1, 0)
.take(9) // 9 first numbers
.thru(first) // Retrieve the first event
.then(x => console.log(x))const most = require('most');
const { last } = require('most-nth');
// Logs
// 8
most.iterate(x => x + 1, 0)
.take(9) // 9 first numbers
.thru(last) // Retrieve the last event
.then(x => console.log(x))