dotdot

0.1.2 • Public • Published

dotdot

Replaced foo..bar() with foo.bar.bind(foo) automatically using Nodejs require hook

NPM

Build status dependencies devdependencies

endorse

dotdot installs a Node loader hook that transforms each loaded JavaScript file.

var foo = {
    n: 101,
    print: function () {
        console.log(n);
    }
};
var printN = foo..print();
printN();
// becomes
var foo = {
    n: 101,
    print: function () {
        console.log(n);
    }
};
var printN = foo.print.bind(foo);
printN();
// prints
101

Both object properties and stand alone functions are supported, including arguments

foo..bar('arg1', 'arg2')  //=> foo.bar.bind(foo, 'arg1', 'arg2')
bar..(3, 'something') //=> bar.bind(null, 3, 'something')

for more examples see test/e2e.js

install and use

npm install --save dotdot

then require dotdot BEFORE any source files with .. are required. The hook does NOT transform the file that loads dotdot itself, since it is too late.

// index.js
require('dotdot');
require('./add');
// add.js
function add(a, b) { return a + b; }
add..(2)(3) // 5
add..(10, 11)() // 21

performance

The source code transformation happens once per file, on the first require call. The substitution itself is a simple RegExp execution, there is no abstract syntax tree, so it should be fast enough.

Later I plan to support a filter function / option to limit substitution to certain files, for example to avoid transforming any source file loaded from node_modules folder.

why?

I got tired of constantly writing .bind(null, ...), especially in promise chains. I looked at using sweet.js macros, but that project is still too fresh (most of their own examples did not work using v0.3.x), and too ambitious for this small change. Now my promise chains are much cleaner:

// compare
asyncSquare(2)
.then(console.log.bind(null, '2 ='))
.then(asyncSquare.bind(null, 3))
.then(console.log.bind(null, '3 ='))
.then(asyncSquare.bind(null, 4))
.then(console.log.bind(null, '4 ='));
 
// with
asyncSquare(2)
.then(console..log('2 ='))
.then(asyncSquare..(3))
.then(console..log('3 ='))
.then(asyncSquare..(4))
.then(console..log('4 ='));

Small print

Author: Gleb Bahmutov © 2013

License: MIT - do anything with the code, but don't blame me if it does not work.

Support: if you find any problems with this module, email / tweet / open issue on Github

Readme

Keywords

none

Package Sidebar

Install

npm i dotdot

Weekly Downloads

1

Version

0.1.2

License

MIT

Last publish

Collaborators

  • bahmutov