Replaced foo..bar() with foo.bar.bind(foo) to shorten common JS syntax under node
Replaced foo..bar() with foo.bar.bind(foo) automatically using Nodejs require hook
var foo =n: 101console.logn;;var printN = fooprint;printN;// becomesvar foo =n: 101console.logn;;var printN = fooprintbindfoo;printN;// prints101
Both object properties and stand alone functions are supported, including arguments
foobar'arg1' 'arg2' //=> foo.bar.bind(foo, 'arg1', 'arg2')bar3 'something' //=> bar.bind(null, 3, 'something')
for more examples see test/e2e.js
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.jsrequire'dotdot';require'./add';// add.jsreturn a + b;add23 // 5add10 11 // 21
The source code transformation happens once per file, on the first
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
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:
// compareasyncSquare2thenconsole.logbindnull '2 ='thenasyncSquarebindnull 3thenconsole.logbindnull '3 ='thenasyncSquarebindnull 4thenconsole.logbindnull '4 =';// withasyncSquare2thenconsole.log'2 ='thenasyncSquare3thenconsole.log'3 ='thenasyncSquare4thenconsole.log'4 =';
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