Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

piping #94

Open
icambron opened this issue Nov 26, 2015 · 10 comments
Open

piping #94

icambron opened this issue Nov 26, 2015 · 10 comments

Comments

@icambron
Copy link

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:

var foo = function(node){
  return node.transform('stuff').transform('more-stuff');
}

gobble('src').pipe(foo).moveTo('place');
gobble('otherSrc').pipe(foo).moveTo('otherPlace');

I think pipe is probably a very simple function on Node that looks something like:

pipe(f){
  return f(this);
}

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?

@icambron icambron mentioned this issue Nov 27, 2015
@martypdx
Copy link

You can pass a function to .transform, isn't that the same thing?

@icambron
Copy link
Author

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.

@martypdx
Copy link

File transformers take input, options where input is string contents of file and you return output:

gobble( 'src' ).transform( code => '/* bird */' + code ).moveTo( 'place' );

@icambron
Copy link
Author

Right, but I want compose existing transformations and reuse those compositions. I don't need to work directly with the strings.

@msegado
Copy link

msegado commented Nov 27, 2015

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); };

@icambron
Copy link
Author

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.

@msegado
Copy link

msegado commented Nov 27, 2015

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]

@msegado
Copy link

msegado commented Nov 27, 2015

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); };

@icambron
Copy link
Author

Ah, right, I should have figured that out. Thanks!

@msegado
Copy link

msegado commented Nov 27, 2015

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
#94 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants