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

callbag-switch-map-operator

v2.0.0

Published

A pure-callbag-operator implementation of switch-map (not exactly flat-map) for callbags 👜

Downloads

9

Readme

callbag-switch-map-operator

Callbag operator implementing switchMap, allowing you to map each element of a source callbag to a new callbag, and optionally to custom-collapse each pair of original/new elements into a final source callbag. (Unlike flatMap, this operator will eagerly switch to new callbags as they emit. If your callbags are pullable only, then there's no difference, but if all callbags are listenables, you might need a proper flatMap.)

This library contains a single function that implements the example in callbag-flatten's README showing the basic pattern of calling map inside map, and then flattening. That is, this library's implementation is a combination of existing callbag operators (namely, pipe, map, and flatten), so there is absolutely no magic here.

This operator was originally published as callbag-flat-map-operator but was renamed.

Installation

In a Node.js project, run

$ npm install --save callbag-switch-map-operator

API and examples

const switchmap = require('callbag-switch-map-operator');

switchmap(mapper[, flattener = (a, b) => b])

I think it's much easier to see what this does with examples, but I'll try to formalize this: with an abuse of TypeScript notation, this can be notionally described as:

switchmap(mapper: A => Callbag<B>, flattener?: (A, B) => C)(source: Callbag<A>): Callbag<C>

That is, switchmap is a callbag operator (a higher-order function) that takes up to two functions:

  • a mapper maps each emission of a source callbag and converts it to a new callbag, and
  • a flattener that maps each pair of values from the original source callbag and the callbag spawned from it to a final value,

and produces a new source callbag that emits the outputs of flattener. By default, flattener = (a, b) => b, that is, the returned callbag will just emit a flattened stream of all callbags created by the mapper, but by overriding this you can achieve all sorts of useful behavior.

An example will hopefully be much more illuminating. This shows how we can repeatedly use the outputs of switchmap in more switchmaps, and shows a couple of uses for flattener (for this example, be sure to run npm install callbag-basics):

const switchmap = require('callbag-flat-map-operator');

const { pipe, fromIter, forEach } = require('callbag-basics'); // npm i callbag-basics

const years = [ 2017, 2016 ];
const urls = 'users,edits'.split(',');
const langs = 'en,zh'.split(',');

// This is the Cartesian product of {years, urls, langs}, so 2 * 2 * 2 elements will be emitted
const parametersCallbag = pipe(
    fromIter(urls),
    switchmap(
        _ => fromIter(years),
        (url, year) => [url, year],
        ),
    switchmap(
        _ => fromIter(langs),
        ([ url, year ], lang) => [url, year, lang],
        ),
);

const parametersToEndpoints = (url, year, lang) => Array.from(Array(2), (_, i) => `/${url}/${year}/${lang}-v${i + 1}`);
const resultsCallbag = pipe(
    parametersCallbag,
    switchmap(
        ([ url, year, lang ]) => fromIter(parametersToEndpoints(url, year, lang)),
        (_, res) => res, // optional, this is the default
        ),
);

pipe(
    resultsCallbag,
    forEach(x => console.log(x)),
);
// /users/2017/en-v1
// /users/2017/en-v2
// /users/2017/zh-v1
// /users/2017/zh-v2
// /users/2016/en-v1
// /users/2016/en-v2
// /users/2016/zh-v1
// /users/2016/zh-v2
// /edits/2017/en-v1
// /edits/2017/en-v2
// /edits/2017/zh-v1
// /edits/2017/zh-v2
// /edits/2016/en-v1
// /edits/2016/en-v2
// /edits/2016/zh-v1
// /edits/2016/zh-v2

Background