dotdot
Replaced foo..bar() with foo.bar.bind(foo) automatically using Nodejs require hook
dotdot installs a Node loader hook that transforms each loaded JavaScript file.
var foo = n: 101 { console; };var printN = foo;;// becomesvar foo = n: 101 { console; };var printN = fooprint;;// prints101
Both object properties and stand alone functions are supported, including arguments
foo //=> foo.bar.bind(foo, 'arg1', 'arg2')bar3 '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;;// add.js { return a + b; }add23 // 5add10 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; // with;
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