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

@markomitranic/tspipe

v1.0.4

Published

A set of utility functions, inspired by good design of Elixir lang and poor design of lodash.

Downloads

10

Readme

TsPipe

Simplify data transformations and debugging - ts-pipe is a type-safe implementation of a pipeline in TS.

TsPipe is literally a thin wrapper around native JS promises. No 400 IQ code here.

In simplest terms, you should use normal chaining when you can... But when you can't, instead of writing a separate function or defining local constants at every step, just pipe the value. You can save pipes into variables or return them from functions and as such compose them into other pipes.

The pipeline pattern is supposed to visually streamline the flow of data transformations. Its a tool, not a religion. Sometimes it works out, sometimes not.

Usage Examples

Naming arguments

We're working with annonymous functions here. So our choice of _ is completely randomly selected. In fact, we ourselves, personally often use $ or __ instead! Remember, the GOAL of pipelines is readability, so there are cases where the simplest solution is just to give the argument a real name!

pipe(
  "1, 2, 3",
  (__) => waitForSeconds(__, 30),
  ($) => $.split(", "),
  (explodedList) => explodedList.map((i) => parseInt(i))
);

I need more arguments, mom!

The rule of thumb in functional languages is, that you don't. "A pipeline must always start with an atomic literal value." What you can do instead is bend the rules - pass an array or an object.

pipe(
  {goals: 12, shots: 17}
  ({ goals, shots }) => shots / goals,
  ($) => $.toFixed(2),
  parseFloat
);

Data transformations

Use stdlib with slight transformations and returns where stdlib falls short. No reason to declare transient/inline single-use variables.

pipe(
  "1, 2, 3",
  (_) => _.split(", "),
  (_) => _.map((i) => parseInt(i)),
  (_) => _.map((i) => i * 2),
  console_log,
  (_) => _.map((i) => i.toString()),
  (_) => {
    _.push("end");
    return _;
  },
  (_) => _.join("-")
);

Auto-resolves promises

Pipelines visualize data transformations, and as such, they enforce await on every single step. This means that you don't have to define async/await steps. You just await for the whole pipe, once.

await pipe(
  "1, 2, 3",
  ($) => waitForSeconds($, 30),
  ($) => $.split(", ")
);

// ["1", "2", "3"]

Downloading a file via Axios Break down the pyramids. Everything in the pipeline awaits one another, so any blocking operations will be resolved before taking the next step.

await pipe(
  axios({
    url: "https://loremflickr.com/320/240/cat",
    method: "get",
    responseType: "stream",
  }),
  (resp) =>
    pipe(path.resolve("./cat.jpg"), fs.createWriteStream, resp.data.pipe)
);

Pipes as promises

The first thing pipelines do, is to create a promise from the literal data you provided. This means you can chain then's onto them :), or await for them. All three statements below will pass tests.

pipe(
  1,
  (_) => _ + 1,
  (_) => _ + 1
).then((_) => {
  expect(_).toBe(3);
});

pipe(
  1,
  (_) => _ + 1,
  (_) => _ + 1,
  (_) => expect(_).toBe(3)
);

const res = await pipe(
  1,
  (_) => _ + 1,
  (_) => _ + 1
);
expect(res).toBe(3);

Long-haul vehicles

Some pipes are just too long. The author of this package got tired of writing boilerplate to support pipes longer than 12 steps. But, all is not lost! You can open a PR! Or, just put your pipe inside of another pipe!

pipe(
  1,
  (_) => _ + 1,
  (_) => _ + 1,
  (_) =>
    pipe(
      _,
      (_) => _ + 1,
      (_) => _ + 1,
      (_) => _ + 1
    ),
  (_) => _ + 1,
  (_) => _ + 1
);