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

function-call

v0.43.0

Published

build code that defines and calls functions in the browser

Downloads

105

Readme

Function-call allows you to build JavaScript strings that do stuff. It's convenient for creating an onclick handler:

var functionCall = require("function-call")

var build = functionCall("buildTemple").withArgs({height: "30 cubits"})
build.evalable()

// returns 'buildTemple({"height":"30 cubits"})'

You can also keep tacking more arguments on to a function call. Arguments can be literals or other function calls:

var moveIn = functionCall("moveIn").withArgs("Tuesday")
build.withArgs(moveIn).evalable()

// returns 'buildTemple({"height":"30 cubits"}, moveIn.bind(null, "Tuesday"))'

Singletons and methods

If you want to reference an object or its methods:

var me = functionCall("me").singleton()
me.methodCall("getName").withArgs("formal").evalable()

// returns 'me.getName("formal")'

These bindings are getting out of hand, what do I do?

FunctionCall works great if you are mostly just passing literals to pure(ish) functions, but if you are calling functions and callbacks with serious dependencies, browser-bridge can help.

It allows you to bake dependencies into a function definition so your functionCalls stay sane:

var bridge = require("browser-bridge")

var a = bridge.defineFunction(function a() {})

var c = bridge.defineFunction(function b(x) { x })

var c = bridge.defineFunction(
  [a, b.withArgs(4000), {some: "data"}],
  function(a, b, data, moar) {
    a()
    b()
    return data.some + moar
  }
)

This will pre-bind a, b, b's args, and your data into a reference called c, so that you get a nice clear function call:

c.withArgs("goats").evalable()

// returns 'c("goats")'

When you eval that, a will be called, b will be called with 4000, and you'll get "datagoats" back.

Using bindings in the browser

Sometimes you want to use a bridge function again in response to a javascript event or something. Use .asCall() to get a binding of the binding, so to speak.

var sayHi = bridge.defineFunction(function(name) {
  alert("wuzzup "+name)
})

bridge.defineFunction(
  [sayHi.asCall()],
  function(sayHi) {
    var name = askTheSpiritsBeyond.whoAmi()
    someElement.onclick = sayHi.withArgs(name).evalable()
  }
)

Globals and other manual references

To reference window or event or similar as an argument:

var js = functionCall("myFunc").withArgs(functionCall.raw("window")).evalable()

// returns 'myFunc(window)'

The raw function is also available on the calls themselves:

var add = functionCall("add")
el.onclick = add.withArgs(add.raw("event")).evalable()

// sets onclick to 'add(event)'

Why?

  • Many JavaScript frameworks don't actually put onclick handlers in the DOM, which means it's difficult to see what happens when a button is pushed.

  • Even if you can figure out what the event handler is, it often plumbs straight into framework internals.

  • FunctionCall allows you to put human-readable, irrefutable JavaScript strings into your HTML so that you can see exactly what's going on and debug problems.