-
Notifications
You must be signed in to change notification settings - Fork 66
RFC: Pass the filename as the 2nd argument to the to function (shift remaining existing arguments by 1 to the right) #33
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
RFC: Pass the filename as the 2nd argument to the to function (shift remaining existing arguments by 1 to the right) #33
Conversation
…ning existing arguments by 1 to the right)
|
@gregmagolan Hmm, I wonder if we could make this without being backwards breaking, for example making the filename (and possibly other parameters) available through the That way the order of arguments would remain untouched, while making the filename accessible. Thoughts? |
lib/replace-in-file.js
Outdated
| if (typeof replacement === 'function') { | ||
| const original = replacement; | ||
| replacement = function() { | ||
| if (typeof replacement === 'function') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this second check (already checked on line 102)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yeah. Typo. Will change that.
1 similar comment
|
Having the filename available through
|
|
True, in that case, how about always passing it in as the last parameter? |
|
I thought of that as well but the number of parameters will be variable based on the number of groups in the regex expression when using regex: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace. Usage would be more difficult in that case and you'd have to use |
|
What would be nice is getting the groups as an array in the replacer function instead of as parameters if backward compatability is going to break anyway. That way the number of parameters is fixed like so: (using typescript typing above to annotate the expected types) |
|
That's an option, but instead of changing the function signature you could use the rest operator to get what you need, even with variable arguments; to: (match, ...args) => {
const file = args.pop();
...
}I'm preparing a 3.0.0 branch regardless, so we can include this in there without worrying about backwards compatibility too much. |
|
@gregmagolan Could you open a new PR for the 3.0.0 branch based on that code base? If you could pass the file parameter as the last argument, I think that would be the best solution, as it's the least chance of backwards breaking implementations and it will leave the signature of the function mostly intact, save adding a new param. I think with the rest operator to capture args it's easy enough to get the last one. Feel free to write the tests as well to keep coverage at 100% and I'll merge it in before releasing 3.0.0 👍 |
|
I hadn't thought of using the rest operator like that. That would work well with the file as the last parameter. I'll prepare another PR on the 3.0.0 branch :) |
|
Awesome thanks! |
I found I needed the filename to determine the correct replacement string for my use case so I added the filename as the 2nd parameter of the
toreplacement function (if it is of type 'function').The change is straight forward. I haven't added any tests to .spec for it yet but thought I would check if you would be interested in merging the feature into the library before bothering.
It would break backward compatibility to anyone using more than the first argument in a
toreplacement function (which are passed in by the contents.replace(item, replacement) call).