mirror of
https://github.com/zhigang1992/replace-in-file.git
synced 2026-01-12 17:43:32 +08:00
Update make-replacements.js
This commit is contained in:
@@ -25,16 +25,15 @@ module.exports = function makeReplacements(contents, from, to, file, count) {
|
||||
|
||||
//Check if replace value is an array and prepare result
|
||||
const isArray = Array.isArray(to);
|
||||
const oldContents = contents;
|
||||
const result = {file};
|
||||
|
||||
//Counting?
|
||||
//Counting? Initialize number of matches
|
||||
if (count) {
|
||||
result.numMatches = 0;
|
||||
}
|
||||
|
||||
//Make replacements
|
||||
from.forEach((item, i) => {
|
||||
const newContents = from.reduce((contents, item, i) => {
|
||||
|
||||
//Call function if given, passing in the filename
|
||||
if (typeof item === 'function') {
|
||||
@@ -44,7 +43,7 @@ module.exports = function makeReplacements(contents, from, to, file, count) {
|
||||
//Get replacement value
|
||||
let replacement = getReplacement(to, isArray, i);
|
||||
if (replacement === null) {
|
||||
return;
|
||||
return contents;
|
||||
}
|
||||
|
||||
//Call function if given, appending the filename
|
||||
@@ -62,12 +61,12 @@ module.exports = function makeReplacements(contents, from, to, file, count) {
|
||||
}
|
||||
|
||||
//Make replacement
|
||||
contents = contents.replace(item, replacement);
|
||||
});
|
||||
return contents.replace(item, replacement);
|
||||
}, contents);
|
||||
|
||||
//Check if changed
|
||||
result.hasChanged = (oldContents !== contents);
|
||||
result.hasChanged = (newContents !== contents);
|
||||
|
||||
//Return result and new contents
|
||||
return [result, contents];
|
||||
return [result, newContents];
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user