mirror of
https://github.com/zhigang1992/replace-in-file.git
synced 2026-01-12 17:43:32 +08:00
Tweaks to readme
This commit is contained in:
@@ -198,7 +198,7 @@ You can also specify a callback that returns a string or a regular expression. T
|
||||
```js
|
||||
const options = {
|
||||
files: 'path/to/file',
|
||||
from: (file) => RegExp(`${foo('SomePattern[A-Za-z-]+', file)}`, g);,
|
||||
from: (file) => new RegExp(file, 'g'),
|
||||
to: 'bar',
|
||||
};
|
||||
```
|
||||
@@ -220,10 +220,7 @@ This callback provides for an extra argument above the String replace method, wh
|
||||
const options = {
|
||||
files: 'path/to/file',
|
||||
from: /SomePattern[A-Za-z-]+/g,
|
||||
to: (...args) => {
|
||||
const file = args.pop();
|
||||
return file;
|
||||
},
|
||||
to: (...args) => args.pop(),
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
@@ -28,8 +28,8 @@ module.exports = function makeReplacements(contents, from, to, file) {
|
||||
|
||||
//Make replacements
|
||||
from.forEach((item, i) => {
|
||||
//`from` callback is called with a filename argument
|
||||
//and returns string or regexp
|
||||
|
||||
//Call function if given, passing the filename
|
||||
if (typeof item === 'function') {
|
||||
item = item(file);
|
||||
}
|
||||
@@ -39,9 +39,10 @@ module.exports = function makeReplacements(contents, from, to, file) {
|
||||
if (replacement === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
//Call function if given, appending the filename
|
||||
if (typeof replacement === 'function') {
|
||||
const original = replacement;
|
||||
//`to` callback is called with an additional filename argument
|
||||
replacement = (...args) => original(...args, file);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user