Skip to content

Commit 2889d99

Browse files
committed
Support basic types (bool, number, null) as replacements.
1 parent ec2d1c1 commit 2889d99

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

lib/grunt-text-replace.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ gruntTextReplace = {
139139
} else if (typeof replacement === 'string') {
140140
return this.expandStringReplacement(replacement);
141141
} else {
142-
return '' + replacement;
142+
return replacement == null ? '' : '' + replacement;
143143
}
144144
},
145145

@@ -153,7 +153,7 @@ gruntTextReplace = {
153153
var returnValue = replacement(matchedSubstring, index, fullText,
154154
regexMatches);
155155
return (typeof returnValue === 'string') ?
156-
gruntTextReplace.processGruntTemplate(returnValue) : returnValue;
156+
gruntTextReplace.processGruntTemplate(returnValue) : returnValue == null ? '' : '' + returnValue;
157157
};
158158
},
159159

@@ -165,7 +165,6 @@ gruntTextReplace = {
165165
var isProcessTemplateTrue = true;
166166
if (grunt.task.current.data &&
167167
grunt.task.current.data.options &&
168-
typeof grunt.task.current.data.options.processTemplates !== 'undefined' &&
169168
grunt.task.current.data.options.processTemplates === false) {
170169
isProcessTemplateTrue = false;
171170
}

test/text-replace-unit-tests.js

+3
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ exports.textReplace = {
4646
test.equal(replaceText('Hello \\foo', '\\', ''), 'Hello foo');
4747
test.equal(replaceText('Foo bar bar', 'bar', 'foo'), 'Foo foo foo');
4848
test.equal(replaceText('Foo bar bar', 'bar', 'Foo bar'), 'Foo Foo bar Foo bar');
49+
test.equal(replaceText('Foo bar baz', ' bar', ''), 'Foo baz');
4950
test.done();
5051
},
5152

@@ -102,6 +103,8 @@ exports.textReplace = {
102103
test.equal(replaceText('Hello 0 true 1 false 2345', '0', 1), 'Hello 1 true 1 false 2345');
103104
test.equal(replaceText('Hello 0 true 1 false 2345', /true|false/g, 0), 'Hello 0 0 1 0 2345');
104105
test.equal(replaceText('Hello 0 true 1 false 2345', 'Hello', 1e5), '100000 0 true 1 false 2345');
106+
test.equal(replaceText('Hello 0 true 1 false 2345', 'Hello', null), ' 0 true 1 false 2345');
107+
test.equal(replaceText('Hello 0 true 1 false 2345', 'true', undefined), 'Hello 0 1 false 2345');
105108
test.done();
106109
},
107110

0 commit comments

Comments
 (0)