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

chainable-keyword

v1.0.0

Published

Coffeescript method chaining with a simple keyword.

Downloads

6

Readme

chainable-keyword

One word method chaining with CoffeeScript.

Wait, another chaining package?

Yes, well. All the other chaining packages are much more complicated than this one.

(Yawn) we've heard that before.

No, seriously, the entire functionality is in four lines of code. I wouldn't have even bothered making it a package if it weren't for the lack of green in my Github activity chart.

What's more, those four lines of code make for a pretty cool way to create chaining API:s.

Cool, eh?

Ice cold, bro! Just prepend your functions with the word chainable and you're good to go!

A short example maybe?

Your typical API might look like this:

class MyAPI
	run:  -> console.log "running!"
	jump: -> console.log "jumping!"
	swim: -> console.log "swimming!"

Now, you would like to be able to do that jQuery thing where you can type:

myApi = new MyAPI()
myapi
	.run()
	.jump()
	.swim()

But doing that normally means you have to start doing stuff like this:

class MyAPI
	run:  ->
		console.log "running!"
		return this
	jump: ->
		console.log "jumping!"
		return this
	swim: ->
		console.log "swimming!"
		return this

Now your code is littered with loads of the same return statements! That's not cool, and it's not right for you to have to choose between beautiful API implementation and beautiful API usage.

So, what's the alternative? Put this at the top of your file:

chainable = require "chainable-keyword"

and rewrite your APIs as follows:

class MyAPI
	run:  chainable -> console.log "running!"
	jump: chainable -> console.log "jumping!"
	swim: chainable -> console.log "swimming!"

Now you can chain your API:s to your hearts content. But wait, there's more! If you find yourself writing a method that you for some reason dont want to chain, you simply don't write the chainable keyword in the method declaration:

class MyAPI
	run:  chainable -> console.log "running!"
	jump: chainable -> console.log "jumping!"
	swim: chainable -> console.log "swimming!"
	getAnswer: -> 42 # This method is non-chainable

Of course, your API can take arguments in its functions as well:

class MyAPI2
	buy: chainable (item, cost) ->
		console.log "Bought #{item} for #{cost}!"
	sell: chainable (item) ->
		console.log "Sold #{item}"

Wow, that's amazing!

Thank you, I hope you find it useful for your next social-media-platform-with-a-twist startup.

How do I install it again?

npm install chainable-keyword

Bonus round: Plain Jane Javascript?

You can of course use the package in a similar sort of way in vanilla Javascript, since chainable is a function like any other function:

function MyAPI() {
	this.run = chainable(function() {
		console.log("running");
	});
	this.jump = chainable(function() {
		console.log("jumping");
	});
	this.swim = chainable(function() {
		console.log("swimming");
	});
}
var myAPI = new MyAPI();
myAPI
	.run()
	.jump()
	.swim();

With love, from Mac <3