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

@sourcevault/bindall

v0.3.1

Published

reassign .this to a fixed object for methods

Downloads

2

Readme

Build Status

bindall ( ob , fns , options )

API

  • ob - Object to bind methods to.
  • fns - is an object whose values get fixed bound to ob

optional

  • options

    • select - Array of string providing names of functions attached to fns to bind - in case when binding needs to be done selectively.

    • addto - object to bind the newly created bound methods to. Using this option means fns object is not mutated and the original methods attached to fns is kept in place.

    • s - same as select

    • a - same as addto

Install

npm install @sourcevault/bindall

Internals

  • reassign .this to fixed object for selected functions

  • mutates fns that holds the functions (default behavior) . To prevent mutation use the addto option.

  • Other implementation ( lodash, underscore ) of bindall lack options to control what fns is bound to. Also cannot prevent mutation when needed.

Examples

|view in beautiful livescript | | --- |

  • . . binding all functions that exists in fns object

bindall = require ("@sourcevault/bindall")

log = function()
{
	console.log (this);
}

// purpose of this excercise is we want access to ob.fuel

ob =
{
	fuel:"coffee",
	fns:
	{
		foo:log
	}
}

ob.fns.foo() // { foo: [Function: log] } // cannot access ob.fuel  :(

bindall(ob,ob.fns) // [WARNING] → will mutate ob.fns

ob.fns.foo() 

// { fuel: 'coffee', fns: { foo: [Function] } } // can access .fuel now :)
  • . . for applying to a subset number of functions in fns object
ob = 
{
	fuel:"coffee",
	fns:
	{
		foo:log,
		bar:log
	}
}

// ↓ only ob.fns.bar is bound due to using select option ↓

bindall(ob,ob.fns,(select:["bar"]))

ob.fns.bar()

// { fuel: 'coffee', fns: { foo: [Function: log] , bar: [Function] } }

// ↓ foo is not bound as expected ↓

ob.fns.foo() // {fns: { foo: [Function: log] , bar: [Function] }}

  • .. to prevent mutating original object fns
ob = 
{
	fuel:"coffee",
	fns:{
		foo:log,
		bar:log
	}
}

boundfns = bindall(ob , ob.fns , ( select:["bar"], addto:{} ))

// ↓ boundfns contains the newly minted bound fns ↓

boundfns.bar() // { fuel: 'coffee', fns: { log: [Function] , bar: [Function] }}

// ↓ original object unchanged ↓

ob.fns.bar()

// {fns: { foo: [Function: log], bar: [Function: log] } }

Notes on Immutability

If a tree falls in a forest and no one is around to hear it, does it make a sound ?

Passing an {} (empty) object to the addto option makes bindall immutable - in the sense that {} (empty) object is mutated and the original object from which the methods were extracted is untouched.

This is useful . .

  • . . if the object providing the methods is external.

  • . . when we need to add our bound functions to an independent object of our choosing.

bindall that mutate fns is dangerous and should be used with caution. The main usecase for mutating fns is within the enclave of where it was created.

Updates and API change

  • 0.3.0 added s and a for select and addto respectively for power users

  • 0.2.0 changed selected to option to expand functionality. Third argument is an object instead of an array and the previous functionality of selected is now passed as value to select key in option. addto allows control of what object to attach thee newly created fuctions to.

  • 0.1.0 readme

License

Code and document released under MIT Licence, see LICENSE for details.