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

@funkia/go-notation

v0.0.4

Published

Type-safe do-notation

Downloads

10

Readme

Build Status codecov Gitter

go-notation

Type-safe do-notation for TypeScript.

Like async/await but for anything that has a flatMap method.

Table of contents

What

go-notation is a TypeScript custom transformation plugin that transforms code like the following.

const arrayA = [5, 6, 7, 8, 9];
const arrayB = [2, 3, 4];
const arrayC = go(bind => {
  const a = bind(arrayA);
  const b = bind(arrayB);
  return a % b === 0 ? [a / b] : [];
});
console.log(arrayC); // logs: [ 3, 2, 4, 2, 3 ]

Into the following.

const arrayA = [5, 6, 7, 8, 9];
const arrayB = [2, 3, 4];
const arrayC = (() =>
  arrayA.flatMap(a =>
    arrayB.flatMap(b => {
      a % b === 0 ? [a / b] : [];
    });
  });
)();
console.log(arrayC);

Why

Working with monads while making repeated usage of flatMap quickly gets tedious. One ends up with hard to read deeply nested code like this.

monadA.flatMap(a =>
  monadB.flatMap(b =>
    monadC.flatMap(c => (c ? createMonad(a + b) : createMonad(b - a)))
  )
);

This problem has been solved in languages like Haskell by introducing a syntax called do-notation at the language level. In JavaScript the problem has been solved for the specific monad Promise with the async/await syntax. go-notation solves the same problem more generally for anything that has a flatMap method.

With go-notation the above example can be written as:

import { go } from "@funkia/go-notation";

go(bind => {
  const a = bind(monadA);
  const b = bind(monadB);
  const c = bind(monadC);
  return c ? createMonad(a + b) : createMonad(b - a);
});

go-notation is implemented as a custom transformation plugin for the TypeScript compiler. The above code gets transpiled into code equivalent to the previous example with explicit flatMap calls.

Features

  • 100% type-safe.
  • Works with any type that has a flatMap method.
  • Compiles to clean, efficient code.
  • Doesn't extend JavaScript with new syntax. Therefore it works flawlessly with any text editor or IDE.

Installation and setup

Install the go-notation from npm.

npm i --save-dev @funkia/go-notation

While the TypeScript compiler supports custom transformers in its programmatic API there is no way to configure custom transformers through tsconfig.json. We therefore recommend using TTypeScript which is a small wrapper around TypeScript that adds support for configuration of custom transformers in tsconfig.json.

First install TTypeScript.

npm i --save-dev ttypescript

Then add the following to your tsconfig.json.

 {
     "compilerOptions": {
+        "plugins": [
+            { "transform": "@funkia/do-notation" }
+        ]
     }
 }

You can now use ttsc instead of tsc and TTypeScript will apply the go-notation transformation during the compile step. TTypeScript also supports ts-node, Parcel, Webpack, and more. See its readme for more information.

Caveats

As of right now the transformation only supports usages of bind of the following form:

const ... = bind (...);

We plan to extend the transformation to support occurrences of bind in any expression or subexpression. For instance the following:

const a = bind(arrayA) + bind(arrayB);