Skip to content

Conversation

@CxRes
Copy link
Contributor

@CxRes CxRes commented Mar 18, 2018

Added a callback for from, which takes file as an argument. Allows the user to tailor the search string according to the filename.

You may want to rename the tests of fix the language in the documentation.

Also fixed the documentation for the to callback.

Added a callback for `from`, which takes file as an argument. Allows the user to tailor the search string according to the filename.
@coveralls
Copy link

Coverage Status

Coverage increased (+0.02%) to 99.642% when pulling ebfbaa6 on CxRes:master into 015571a on adamreisnz:master.

@coveralls
Copy link

coveralls commented Mar 18, 2018

Coverage Status

Coverage increased (+0.02%) to 99.642% when pulling 38623c7 on CxRes:master into 015571a on adamreisnz:master.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.02%) to 99.642% when pulling ebfbaa6 on CxRes:master into 015571a on adamreisnz:master.

Copy link
Owner

@adamreisnz adamreisnz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! Just a few minor changes.

//Make replacements
from.forEach((item, i) => {

if (typeof item === 'function') {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment above this block to indicate if a function is given it's called with the file name?

package.json Outdated
{
"name": "replace-in-file",
"version": "3.2.0",
"version": "3.3.0",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to bump the package version, I'll do that when I release a new version

Additional comments to explain the callback forms.
Reversed minor version bump.
@CxRes
Copy link
Contributor Author

CxRes commented Mar 18, 2018

Changes made as requested...

README.md Outdated
As the `to` parameter is passed to the native [String replace method](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace), you can also specify a callback. This callback provides as an extra argument the name of the file in which the replacement is being performed. The following example uses a callback to convert matching strings to lowercase and appends the file extension to it:

```js
var path = require('path');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry missed this, could you change var to const please, as the project is using ES6 throughout.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually maybe remove the path reference altogether, as I'd like to keep the examples as simple as possible, without external dependencies.

README.md Outdated
from: /SomePattern[A-Za-z-]+/g,
to: (match) => match.toLowerCase(),
from: /SomePattern([A-Za-z-]+)/g,
to: (match, p1, offset, string, file) => `${match.toLowerCase()}${path.extname(file)}`,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's revert this example back to how it was to keep it simple, and instead create a second example for the file parameter usage.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, will fix these later in the day!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes made as requested!

@CxRes
Copy link
Contributor Author

CxRes commented Mar 19, 2018

On the issue of file argument: Could we consider making file the first argument in 4.0? The problem with String.prototype.replace is that it produces an arbitrary number of matches and hence detecting file requires some extra logic on part of the user. (I can see the other side of the argument that most users will never use file). I am open to suggestion and can make an issue if you wish...

@adamreisnz
Copy link
Owner

adamreisnz commented Mar 19, 2018

You could use rest parameters to get around that, e.g.

(match, ...rest) => {
  const file = rest.pop();
}

Reverted to original `to` callback example.
Added a separate example example for using the `file` argument.
@CxRes
Copy link
Contributor Author

CxRes commented Mar 21, 2018

Just from an API point of view I had another idea. Instead of adding an extra argument file, which spoils the purity of the replace function, we can have a single callback with file argument for the to and from options which return the array of String/RegExp/Function. Something like this:

{
  from: (file) => [`SomeStringThatDependsOn${file}`, ...]
  to: (file) => [(match) => `SomethingElseThatDependsOn${file}`, ...]
}

Just an idea 😄 !

@adamreisnz adamreisnz merged commit d94c3aa into adamreisnz:master Mar 21, 2018
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