Package Exports
- most-nth
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (most-nth) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
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-nth
In 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)): c
first
stream.thru(first) -> Promise
stream: -a--b--c----d-->
stream.thru(first): a
last
stream.thru(last) -> Promise
stream: -a--b--c--d--|
stream.thru(last): d
Examples
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))