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

nxst

v1.0.4

Published

A simple module to help with the infamous function nesting problem.

Downloads

290

Readme

nxst - A simple module to help with the infamous function nesting problem.

nxst (a combination of the words next and nest) is a fairly simple module to solve the infamous JavaScript function nesting issue.

This module is notoriously simple, but I found it useful, so ¯\(ツ)

What does it do?

nxst allows you to easily nest multiple functions together. Normally, you would have to write something like this:

console.log(5, ((a) => {return a})("Hello, world!")); // Prints: 5 Hello, world!

// Note, the code above is not meant to be used in a real scenario - this is an example. This module may be useful in larger codebases, where nesting like this can make code very hard to read.

nxst allows you to write something like this:

import nest from "nxst";

nest()
.add(console.log, 5)
.add((a) => {return a}, "Hello, world!")
.execute(); // Prints: 5 Hello, world!
// This is now significantly easier to read and follow - and it works!

In the case that you want to change the position of where the last function is placed, you can simply call nest().insert

import nest from "nxst";

nest()
.add(console.log, nest().insert, 5)
.add((a) => {return a}, "Hello, world!")
.execute(); // Prints: Hello, world! 5

And yes, it also works with multiple inserts, if you need that for whatever reason. Go ahead and try it!

How does it work?

Ok, let's say you still don't understand what it does and how it actually works.

nest() creates a new Nest class, which will let you add functions, which will get put inside of each other.

Example:

import nest from "nxst";

// This:
nest()
.add(x)
.add(y)
.execute();

// Is equivalent to this:
x(y())

The first argument of the .add() function is always a function, then the rest are any arguments you want to supply to the function you provided.

import nest from "nxst";

nest()
.add(x, 5) // this adds a function with an extra space - x(5, _)
.add(y) // the output of y() goes into the place of the underscore.
.execute();

If you want to change the location of where this output goes, you can use nest().insert at that location.

import nest from "nxst";

nest()
.add(x, nest().insert, 5) // this will now be x(_, 5)
.add(y) // the output of y() goes into the place of the underscore.
.execute();

What happens upon execution?

Let's say we have the following example code:

import nest from "nxst";

nest()
.add(x, nest().insert, 5)
.add(y)
.execute();

Before doing any execution, when you add a function to a nest, it is added to an array.

Upon trying to execute this, it will read the array backwards - this means y() gets ran first, then the output is provided to the insert position (or positions), and it recurses until there is nothing left.

This means:

  1. y() is ran first
  2. The returned value is provided to x: x(y(), 5) (except y already had its value computed)
  3. This process recurses until we went through the full nest. For our example here, this is already done - we ran everything we needed to run.
  4. The entire return value is provided as a return value of .execute().

How do I install nxst?

Ok, let's say I convinced you to use this module. Maybe you're a beginner, and have no clue how to install a node module.

Generally, you're going to be using the NPM package manager for Node.js.

You're gonna want to simply run this in your terminal, while inside your project directory:

npm install nxst

If you use yarn:

yarn add nxst

If you use pnpm:

pnpm install nxst

Then there is no real other setup, other than a simple import:

// EcmaScript (type: "module" in the package.json)
import nest from "nxst";

// CommonJS (type: "commonjs" in the package.json, or no type at all)
const nest = require("nxst");

Voila! Now you can use nxst.