0.1.1 • Published 9 years ago

thisize v0.1.1

Weekly downloads
2
License
MIT
Repository
github
Last release
9 years ago

thisize

Converts normal functions to methods. This is mostly useful for the es7 :: operator which allows chaining with simple functions.

Install

It's available on npm. It has no dependencies so you can grab a wzrd.in standalone build.

npm install --save thisize@0.1

thisize(fn, thisArgIndex=0)

Say you have the function first:

function first(xs) {
    return xs[0];
}

first('abc') === 'a';

You could thisize it which uses the this context instead of the first argument.

thisize(first).call('abc') === 'a';

You can specify an index relative to the start of the argument list, or the end (if you need this for some reason).

For example:

var readFile = thisize(fs.readFile, -1);

function handleResponse(err, data) { ... };

// the following are equivilent
handleResponse::readFile('foo.txt', 'utf8');
readFile.call(handleResponse, 'foo.txt', 'utf8');
fs.readFile('foo.txt', 'utf8', handleResponse);

Useful examples!

Think about the places we use chaining in JS, for example, jQuery plugins!

function setColor(el, color){
    el.css({color: color});
    return el;
}

export setColor;
export default thisize(setColor);

And then it can be used in a functional manner:

import {setColor} from 'jq-set-color';

compose(setColor, somethingElse)($('.foo'));

Or with the es7 :: operator:

import setColor from 'jq-set-color';

$('.foo')
 .show()
 ::setColor('green')
 .text('Cool?')

Either way is fine, and not having to pollute $.fn is a win.