Skip to content

agj/dot-into

Repository files navigation

dot-into ↪️

A small JavaScript and TypeScript utility that allows you to convert nested code like this

third(second(first(a)), b);

into code that flows left-to-right or top-to-bottom, like this

first(a).into(second).into(third, b);

You might be familiar with this style of programming from terminal shells (Bash and friends) which use the | (pipe) operator, or from many (mostly functional) programming languages that implement function piping, commonly through the |> operator. It allows you to avoid nesting function calls, maintaining left-to-right order.

⚠️ DO NOT use in library code! dot-into modifies the Object prototype with an into method, which is part of the global state! This ensures that most anything will have this method available, but it will affect your entire project.

This package was originally prompted by Reg Brathwaite's 2008 blog post. He also implemented it in his Katy library as the T method, but mine was a more basic and focused approach with no added magic. And now it comes with types!

Fun ways to use

Some of these examples use Ramda, which is a library that makes it easy to do these kinds of sequential transformations of data!

import "dot-into";
import * as R from "ramda"; // See: https://ramdajs.com/

// Get sum of amounts in objects, then get its power of 2.

const amountSumSquared = [
  { amounts: [10, 20] },
  { amounts: [35, 15] },
  { amounts: [2, 3] },
]
  .flatMap((o) => o.amounts)
  .reduce((n, m) => n + m)
  .into(Math.pow, 2);

// Group strings into an object.

const namesDict = ["Claudia", "Pedro", "Amalia"]
  .map((name) => [name, `My friend ${name}!`])
  .into(Object.fromEntries);

// Take the third string in alphabetical order.

const [third] = ["foo", "bar", "baz", "hoge", "bar", "foo"]
  .into(R.sort(R.ascend))
  .into(R.uniq)
  .into(R.drop(2));

// Get every latin alphabet letter used in some text.

const latinLettersInCorpus = "corpus"
  .toLowerCase()
  .split("")
  .filter((char) => /[a-z]/.test(char))
  .into(R.uniq);

Installation

With npm:

npm install --save dot-into

Remember to import "dot-into" from the main file of your project!

No AI slop policy

This project does not use LLMs or any form of generative AI, be it in whole or in part, for the authoring of its code or any of its related content, and will not accept such contributions. This policy does not necessarily reflect on the dependencies and tools used herein.

Please read the Open Slopware “Why not LLMs?” rationale to learn about the multitude of externalities that plague the use and development of LLMs.

About

Next best thing to a pipeline operator in JS and TS. ↪️

Topics

Resources

License

Stars

Watchers

Forks

Contributors