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

babel-plugin-functional-composition

v1.0.3

Published

Functional Composition for JavaScript

Downloads

11

Readme

Functional Composition

Babel plugin for functional code bases

Get it

npm install --save-dev babel-plugin-functional-composition

Use it

// .babelrc
{
  "plugins": ["functional-composition", {
    "name": "pipe" // optional - defaults to `pipe`
    "as": "pipe" // optional - defaults to `pipe`
    "from": "ramda" // optional, but required for automatic imports
  }]
}

Left to right composition >>

const add3 = x => x + 3
const add2 = x => x + 2

const y = add2 >> add3
// will become
const y = pipe(add2, add3)

Right to left composition <<

const add3 = x => x + 3
const add2 = x => x + 2

const y = add2 << add3
// will become
const y = pipe(add3, add2)

Both directions of Composition

const add3 = x => x + 3
const add2 = x => x + 2
const add1 = x => x + 1

const y = add2 >> add3 << add1
// will become
const y = pipe(add1, pipe(add2, add3))

>>> For non-curried functions

Suppose you have a functional API that is not curried you can now do this. >>> will place the left expression as the last parameter to the right expression.

import { map, from, scan } from 'most'

const add1 = x => x + 1

const x = from([1, 2, 3]) >>> map(add1) >>> scan(add1, 0)
// will become
const x = scan(add1, 0, map(add1, from([1, 2, 3])))

const y = 3 >>> add1
// will become
const y = add1(3)

This plugin is opt-in only - use fc

By default >>, <<, and >>> will do nothing for you that JS does not already do, because in order to activate these features a directive must be applied.

3 >>> 3 // still a bitwise operation

const add1 = x => x + 1

function add1To (x) {
  'use fc' // activates plugin in this scope

  return x >>> add1 // becomes add1(x)
}

Notes about the options object

If the (optional) options object is provided, with at the minimum from, if the import does not exist yet, the import will be added for you. This is useful for things like eslint complaining about unused imports. name is the the part of the object that you would normally import e.g. pipe in import { pipe } from 'ramda'. If name is not the name you'd like to have imported as, perhaps it conflicts with another import, you can specify as to import it as something else. import { pipe as sequence } from 'ramda', will be added to your files if the as option is provided.

So for instance if your are using ramda all you will need is

{
  "plugins": [
    ["functional-composition", { "from": "ramda" }]
  ]
}

and at the top of your file this plugin will add import { pipe } from 'ramda' for you.