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

@vsadx/postoffice

v1.0.3

Published

A more powerful `.bind`

Downloads

6

Readme

PostOffice

PostOffice helps you create more reusable functions.

  • Powerful: PostOffice lets you create simple functions, then bind any of the parameters you choose. Like .bind in JavaScript, binding a parameter creates a copy of your function. This way, you can bind parameters to the same function many times creating new functions (like Dependency Injection or Partial Application).
  • Simple: You don't need to write in a particular style! PostOffice exports just one function. If you pass in a function to postoffice(), you get a new function that support arbitrary parameter binding.
  • Tiny: The principles behind this library are so simple, you could do it yourself. However, postoffice.js is only 22 lines of code, so feel free to check out how it works.
import { postoffice, N } from "postoffice.js"

const subtract = postoffice((first, second) => first - second)

// `subtract` takes two parameters, but we only fill in the `first`
const take_from_100 = subract(100)

console.log(take_from_100(70)) // 30

console.log(take_from_100(127)) // -27
const subtract = postoffice((first, second) => first - second)

// `subtract` takes two parameters, but we only supply the `second`
// the const `N` tells our function that we don't want to place a value in it yet
const take_away_20 = subract(N, 20)

console.log(take_away_20(80)) // 60

console.log(take_away_20(13)) // -7

What's PostOffice doing?

Like a real postoffice, when you fill-out an envelope but forget some of the information, you get your letter back. You also get instructions on what is missing. Once you add all the missing information, the postoffice delivers your mail. This is not how most programming languages work. If you try to run a function, but don't supply all the required parameters, expect an exception or unexpected behavior.

What does this library do? When you run a postoffice function, you don't have to supply all the required parameters. The function will bind all the parameters you added, but return a new function that takes the missing parameters. When you supply all the missing parameters, the function promptly executes.

This is more than a debugging tool. This library is designed to give you more freedom as a function author or API user. How so? The next code example shows how you can use N to add placeholders for any parameters of a function. Also, PostOffice can create multiple functions from just one original function. Each copied function can have different parameters bound, or the same one bound but using a different value.

const sell_food = postoffice((item, price, count) => `${item}: $${price * count}`)

const sell_pizza = sell_food("Pizza", 12.50, N)

const sell_drink = sell_food(N, 1.20, 1)

//////

sell_pizza(3) // "Pizza: $37.50"

sell_drink("Sprite") // "Sprite: $1.20"

sell_drink("Pepsi") // "Pepsi: $1.20"

References
XMoñOcci, XBind, Ocxi.js