most-nth
TypeScript icon, indicating that this package has built-in type declarations

0.0.11 • Public • Published

most-nth

Version License

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))

Package Sidebar

Install

npm i most-nth

Weekly Downloads

1

Version

0.0.11

License

BSD-3-Clause

Unpacked Size

4.23 kB

Total Files

5

Last publish

Collaborators

  • craft-ai