Underscore.js incompatible =(

101 views
Skip to first unread message

Aseem Kishore

unread,
Apr 4, 2011, 7:09:44 PM4/4/11
to streamline.js
I just tried Streamline for the first time in an actual project, and
it's freaking awesome. Our test cases that were nested 8 levels deep
are now actually comprehensible!

However, we've ran into one other gotcha -- it doesn't work when we
use the also-elegant underscore.js ( http://documentcloud.github.com/underscore/
)!

I worried it might not work, since it also uses the underscore
character ( _ ), but I hoped we might be fine since we never use it in
the functional style -- e.g. _([1,2,3]).each(...) -- but rather always
the OOP style -- e.g. _.each([1,2,3], ...). That is, we always use it
only as an object (static class) rather than as a function.

Unfortunately, that doesn't seem to matter. Here's a super simple test
case:

var _ = require('underscore');
_.each([1, 2, 3], console.log);

And trying to compile that throws:

Error: error streamlining /Users/akishore/Projects/Foo/bar.js: :
invalid use of '_' parameter on line 1
at Object.transform (/usr/lib/node/.npm/streamline/0.1.10/package/
lib/transform.js:1499:10)
at _transformIt (/usr/lib/node/.npm/streamline/0.1.10/package/lib/
register.js:73:31)
at Object.loadFile (/usr/lib/node/.npm/streamline/0.1.10/package/
lib/register.js:81:11)
at Object..js (/usr/lib/node/.npm/streamline/0.1.10/package/lib/
register.js:37:26)
at Module.load (module.js:315:31)
at Function._load (module.js:276:12)
at require (module.js:327:19)
at Object.<anonymous> (/Users/akishore/Projects/Foo/bar.js:3:1)
at Module._compile (module.js:383:26)
at Object.<anonymous> (module.js:389:10)

Has anyone else ran into this? Any suggestions? Underscore.js is also
a very valuable library that I'd love to be able to use within my
Streamline files.

I realize I can import it as a different variable name than
underscore, but that renders the code a lot less readable /
immediately recognizable.

Thanks in advance!

Aseem

Bruno Jouhier

unread,
Apr 5, 2011, 3:33:14 PM4/5/11
to streamline.js
Hi Aseem,

This is a problem. The current workaround is to import underscore with
a different name but I agree that this is not very satisfactory.

One thing I could do is modify the transformation to accept
alternative markers. For example, sequences of underscores of any
length. This way you could use _ for the underscore library and __ for
streamline. Should not be too difficult to implement but I need to
changes intermediate variables because __ is already used.

Bruno

On Apr 5, 1:09 am, Aseem Kishore <aseem.kish...@gmail.com> wrote:
> I just tried Streamline for the first time in an actual project, and
> it's freaking awesome. Our test cases that were nested 8 levels deep
> are now actually comprehensible!
>
> However, we've ran into one other gotcha -- it doesn't work when we
> use the also-elegant underscore.js (http://documentcloud.github.com/underscore/

Aseem Kishore

unread,
Apr 5, 2011, 3:51:26 PM4/5/11
to stream...@googlegroups.com, Bruno Jouhier
Is it technically correct to say that with Streamline, the only times I use the underscore character are:

- foo(bar, baz[, ...], _)
- function foo(bar, baz, _) {

That is, it's always followed by a closing parenthesis?

If so, the good news is that while you *can* pass the Underscore.js object like that, you never really do in practice, so in theory, you could safely support it by just looking for the underscore+parenthesis pattern.

Cheers,
Aseem

Bruno Jouhier

unread,
Apr 7, 2011, 5:47:50 PM4/7/11
to streamline.js
Things are a little more complex. Most node APIs have the callback as
last parameter but you can also have it in a different position. For
example, if you want optional arguments, it is convenient to pass the
callback as first parameter. This is what I chose for the flows module
(and also what I chose for most internal APIs in our application).

Also, streamline uses _ to represent the callback in the generated
code. So if I keep _ the _ parameter will override the "underscore"
variable and the generated code won't work as you would expect it to.
This is why I want a cleaner solution in which the callback is called
__ instead of _.

Bruno

Aseem Kishore

unread,
Apr 8, 2011, 4:59:15 AM4/8/11
to stream...@googlegroups.com, Bruno Jouhier
Good points!

Perhaps the solution is a file-level config, sort of like JSLint:

/*streamline _cb */

Actually fwiw, this might be a really nice API to expose. It's similar to "!!STREAMLINE!!" but more elegant and gives you the flexibility to expand the config, since the parsing algorithm would be:

1. Find "/*streamline ".
2. Find the next "*/".
3. Extract the contents in between, trim, and then parse however you want.

The contents inside can be any format you want. If it's empty, the config is just the default. For now, since the only config is what token to use for the callback, I figure this is empty.

Either way, "/*streamline " also tells you it's a streamline file. Nice and elegant. What do you think?

Aseem

Aseem Kishore

unread,
Apr 8, 2011, 5:00:36 AM4/8/11
to stream...@googlegroups.com, Bruno Jouhier
Sorry, this:

"For now, since the only config is what token to use for the callback, I figure this is empty."

Should read:

"For now, since the only config is what token to use for the callback, I figure this [just /*streamline _cb */] is nice and simple / good enough."

Aseem

Bruno Jouhier

unread,
Apr 9, 2011, 12:12:12 PM4/9/11
to streamline.js
Hi Aseem,

I've implemented something close. I chose to have the directive
formatted as a block of JSON. This will make it easier to introduce
more options if we need to later.

BTW, posting as a GitHub issue was a good idea. I prefer to manage
suggestions and bugs this way (the workflow is nice).

Bruno

On Apr 8, 11:00 am, Aseem Kishore <aseem.kish...@gmail.com> wrote:
> Sorry, this:
>
> "For now, since the only config is what token to use for the callback, I
> figure this is empty."
>
> Should read:
>
> "For now, since the only config is what token to use for the callback, I
> figure this [just /*streamline _cb */] is nice and simple / good enough."
>
> Aseem
>
> On Fri, Apr 8, 2011 at 1:59 AM, Aseem Kishore <aseem.kish...@gmail.com>wrote:
>
>
>
>
>
>
>
> > Good points!
>
> > Perhaps the solution is a file-level config, sort of like JSLint:
>
> > /*streamline _cb */
>
> > Actually fwiw, this might be a really nice API to expose. It's similar to
> > "!!STREAMLINE!!" but more elegant and gives you the flexibility to expand
> > the config, since the parsing algorithm would be:
>
> > 1. Find "/*streamline ".
> > 2. Find the next "*/".
> > 3. Extract the contents in between, trim, and then parse however you want.
>
> > The contents inside can be any format you want. If it's empty, the config
> > is just the default. For now, since the only config is what token to use for
> > the callback, I figure this is empty.
>
> > Either way, "/*streamline " also tells you it's a streamline file. Nice and
> > elegant. What do you think?
>
> > Aseem
>

Aseem Kishore

unread,
Apr 9, 2011, 2:46:21 PM4/9/11
to stream...@googlegroups.com
I saw, and that looks great too! I've yet to try this though (it didn't seem like the version on npm was updated when I checked yesterday) so will let you know when I do.

Cheers,
Aseem

Bruno Jouhier

unread,
Apr 9, 2011, 3:39:41 PM4/9/11
to streamline.js
I did not publish on npm yesterday (a bit superstitious about
0.1.13).
I bumped the version again and published today.

Bruno

Koala D

unread,
Mar 25, 2014, 1:02:35 PM3/25/14
to stream...@googlegroups.com
I was worried about the same thing! I'm trying to decide between q and streamline but because we are trying to use Coffee in our codebase I wanted to choose streamline. Saw that it uses '_' and my first thought was, 'Oh... would that work with Underscore.js?' haha

Is this kind of sorted out now?? 

Thanks, so much!

Seth Pollack

unread,
Mar 26, 2014, 11:11:13 AM3/26/14
to stream...@googlegroups.com
Yes, underscore works fine with streamline. Just be sure to name the underscore library variable something other than '_' to avoid confusion. e.g.:
var underscore = require('underscore');
Reply all
Reply to author
Forward
0 new messages