npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

async-functional

v0.6.0

Published

A functional approach to deal with async generators

Downloads

3

Readme

async-functional

The asynchronous generator AsyncGenerator appears on many occasions. For example, fetching a set of records from a REST API, or dealing with database records. This package tries to give a functional flavor to dealing with such situations.

One can consider AysncGenerator as a stream. This library allows async generators to be treated in functional manner.

New JSON Path transformer added as functors so that it is possible to process set of JSONPaths together to create a single object.

Contents

Functional Operators

Inspired by rxjs and functional programming, this package gives operators such as map, filter, zip, take or takeWhile alongwith folds such as foldl. Hopefully, this allows programs to be implemented using simple for loop.

TODO - Controlled Parallalism

While working with multiple such async generators, this package will try to create a controlled parallalism.

Documentation

Prelude - A set of generic functions

Convert an Iterable into an async generator

  import {of} from 'async-functional';

  const input = of([1,2,3,4,5]);

  // Now you can iterate using for..await..of
  for await (const i of input)
    console.log("Output: " + i);

Collect the result of async generator into a promise

  import {of, collect} from 'async-functional';

  const input = of([1,2,3,4,5]);
  // Convert it back into a promise
  const result = await collect(input);

Functors and folds - Higher order functions

Functors and other higher order functions to process async generator.

Create a functor - Lift a function to process async generator

  import {functor, of} from 'async-functional';

  // Create a async generator from an array
  const input = of([1, 2, 3, 4, 5]);
  // A simple function to square the numbers
  const square = (x: number) => x * x;
  // Use functor to lift square to squareMap
  const squareMap = functor(square);

  // Use map to square each element
  for await (const i of squareMap(input)) {
    console.log('output = ' + i);
  }

Use map as a functor

import {map, of} from 'async-functional';

// Create a async generator from an array
const input = of([1, 2, 3, 4, 5]);

// Use map to square each element
for await (const i of map(x => x * x, input)) {
  console.log('output = ' + i);
}

Use filter to selectively iterate over elements

import {filter, of} from 'async-functional';

// Create a async generator from an array
const input = of([1, 2, 3, 4, 5]);

// Filter to remove odd elements
const evenFilter = (x: number) => x % 2 == 0

// Use map to square each element
for await (const i of filter(evenFilter, input)) {
  console.log('output = ' + i);
}

// Output =>
// output = 2
// output = 4

JSON Path transformation

import {jsonPathNumber, jsonTransform, jsonPathBoolean} from '..';

  const numberMap = jsonPathNumber('$.number', 'mynumber');
  const booleanMap = jsonPathBoolean('$.result', 'myboolean');
  const transf = jsonTransform(nkeyMap, bkeyMap);
  const input = {
    number: 101.0,
    result: false,
  };
  const output = transf(input);

  // Should see { mynumber: 101.0, myboolean: false }