Skip to content

Conversation

@gregmagolan
Copy link
Contributor

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 to replacement 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 to replacement function (which are passed in by the contents.replace(item, replacement) call).

@coveralls
Copy link

coveralls commented Sep 13, 2017

Coverage Status

Coverage decreased (-1.0%) to 99.022% when pulling dc9e4bc on gregmagolan:feature/pass-filename-to-to-function into c044c5a on adamreisnz:master.

@coveralls
Copy link

coveralls commented Sep 13, 2017

Coverage Status

Coverage decreased (-1.5%) to 98.54% when pulling 3472274 on gregmagolan:feature/pass-filename-to-to-function into c044c5a on adamreisnz:master.

@adamreisnz
Copy link
Owner

adamreisnz commented Sep 14, 2017

@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 this context of the replacement function.

That way the order of arguments would remain untouched, while making the filename accessible.

Thoughts?

if (typeof replacement === 'function') {
const original = replacement;
replacement = function() {
if (typeof replacement === 'function') {
Copy link
Owner

@adamreisnz adamreisnz Sep 14, 2017

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)?

Copy link
Contributor Author

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.

@coveralls
Copy link

coveralls commented Sep 14, 2017

Coverage Status

Coverage decreased (-1.2%) to 98.78% when pulling 5dc1d62 on gregmagolan:feature/pass-filename-to-to-function into c044c5a on adamreisnz:master.

1 similar comment
@coveralls
Copy link

coveralls commented Sep 14, 2017

Coverage Status

Coverage decreased (-1.2%) to 98.78% when pulling 5dc1d62 on gregmagolan:feature/pass-filename-to-to-function into c044c5a on adamreisnz:master.

@gregmagolan
Copy link
Contributor Author

gregmagolan commented Sep 14, 2017

Having the filename available through this would force the usage of ES5 function (match) { ... } wouldn't it? If you use an ES6 arrow function within an ES6 class like so:

class Something {
  replaceStuff() {
    replace.sync({
      files: [ ... ],
      from: 'foobar',
      to: (match) => { ... }
    });
  }
}

this would be the class context that the arrow function is in.

@adamreisnz
Copy link
Owner

True, in that case, how about always passing it in as the last parameter?

@gregmagolan
Copy link
Contributor Author

gregmagolan commented Sep 14, 2017

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 arguments instead of function parameters when dealing with variable parameters.

@gregmagolan
Copy link
Contributor Author

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:

to: (match: string, filename: string, groups: string[], offset: number, wholeString: string) => {
  ...
}

(using typescript typing above to annotate the expected types)

@adamreisnz
Copy link
Owner

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.

adamreisnz added a commit that referenced this pull request Sep 14, 2017
- Drop deprecated API
- Drop support for Node 4 and 5
- Refactor and clean up code base
- Leverage destructuring
- Option to disable globs (#33)
- Update readme
@adamreisnz
Copy link
Owner

@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 👍

@gregmagolan
Copy link
Contributor Author

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 :)

@gregmagolan gregmagolan deleted the feature/pass-filename-to-to-function branch September 14, 2017 20:36
@adamreisnz
Copy link
Owner

Awesome thanks!

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

Successfully merging this pull request may close these issues.

3 participants