Skip to content

Commit 73c54e7

Browse files
Reshetnikov_ASReshetnikov_AS
Reshetnikov_AS
authored and
Reshetnikov_AS
committed
Added strict mode + tests with refactoring.
1 parent 86f0fbd commit 73c54e7

File tree

2 files changed

+241
-116
lines changed

2 files changed

+241
-116
lines changed

index.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,21 @@ function processOptions(source, options) {
77
options.search = new RegExp(options.search, options.flags);
88
}
99

10-
source = source.replace(options.search, options.replace);
10+
newSource = source.replace(options.search, options.replace);
11+
if (options.strict === true && newSource === source) {
12+
throw new Error('Cannot replace ' + options.search + ' → ' + options.replace);
13+
}
1114
}
1215

13-
return source;
16+
if (options.strict === true && _.isUndefined(options.search)) {
17+
throw new Error('Cannot replace: search option is not defined → ' + JSON.stringify(options));
18+
}
19+
20+
if (options.strict === true && _.isUndefined(options.replace)) {
21+
throw new Error('Cannot replace: replace option is not defined → ' + JSON.stringify(options));
22+
}
23+
24+
return newSource;
1425
}
1526

1627
module.exports = function (source) {
@@ -20,6 +31,7 @@ module.exports = function (source) {
2031

2132
if (_.isArray(options.multiple)) {
2233
options.multiple.forEach(function (suboptions) {
34+
suboptions.strict = !_.isUndefined(suboptions.strict) ? suboptions.strict : options.strict;
2335
source = processOptions(source, suboptions);
2436
});
2537
} else {

0 commit comments

Comments
 (0)