-
Notifications
You must be signed in to change notification settings - Fork 66
Callback for from
#46
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
Conversation
Added a callback for `from`, which takes file as an argument. Allows the user to tailor the search string according to the filename.
adamreisnz
left a comment
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.
Thanks for the PR! Just a few minor changes.
| //Make replacements | ||
| from.forEach((item, i) => { | ||
|
|
||
| if (typeof item === '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.
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", |
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.
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.
|
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'); |
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.
Sorry missed this, could you change var to const please, as the project is using ES6 throughout.
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.
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)}`, |
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.
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.
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.
Ok, will fix these later in the day!
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.
Changes made as requested!
|
On the issue of |
|
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.
|
Just from an API point of view I had another idea. Instead of adding an extra argument {
from: (file) => [`SomeStringThatDependsOn${file}`, ...]
to: (file) => [(match) => `SomethingElseThatDependsOn${file}`, ...]
}Just an idea 😄 ! |
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
tocallback.