Open icambron opened 8 years ago
You can pass a function to .transform
, isn't that the same thing?
Hmm, but the function passed to transform takes input and output dirs or files, not nodes. It's not obvious to me how to do this sort of thing with transform
directly.
File transformers take input, options
where input
is string contents of file and you return output
:
gobble( 'src' ).transform( code => '/* bird */' + code ).moveTo( 'place' );
Right, but I want compose existing transformations and reuse those compositions. I don't need to work directly with the strings.
This is actually a common enough operation that there's been talk of adding a pipe operator into JS itself: https://github.com/mindeavor/es-pipeline-operator . That's a long way off though, so for now I could see the value of a pipe method for convenience. Biggest downside I see is that it increases the API surface area slightly.
Re. monkey patching, I think you can grab any Node
instance and add the pipe method to the original prototype as follows (assuming the prototype chain is only one level deep; some experimentation may be needed):
nodeInstance.constructor.prototype.pipe = function pipe(f) { return f(this); };
Yeah, I played around with that for a bit yesterday and was surprised that I couldn't quite get anything to work. But could easily have been user error.
Odd. Give this a shot:
gobble('').constructor.prototype.pipe = function pipe(f) { return f(this); };
[EDIT: huh yeah, I see what you mean; not working for me]
OK... the Source
and Merger
nodes returned by gobble()
are subclasses of Node
, so we do need to go a level higher in the prototype chain. The following works, albeit with reliance on __proto__
:
gobble([]).__proto__.__proto__.pipe = function pipe(f) { return f(this); };
Ah, right, I should have figured that out. Thanks!
Np! On Nov 27, 2015 5:38 PM, "Isaac Cambron" notifications@github.com wrote:
Ah, right, I should have figured that out. Thanks!
— Reply to this email directly or view it on GitHub https://github.com/gobblejs/gobble/issues/94#issuecomment-160223181.
One thing that frustrates me about the gobble API is that I can't figure out how to compose things. Specifically, I want to do something like this:
I think
pipe
is probably a very simple function onNode
that looks something like:So...is there:
a) a way I'm missing to accomplish this? b) a way of hacking this in from userspace (I couldn't figure out how to monkey patch it)? c) a chance you'd take that addition as a PR?