Skip to content

Commit f92c08b

Browse files
authored
feat(toBoolean): use regex for false string value (#1081)
Use regex matching so that it converts False or FALSE (strings) to false (boolean) and True or TRUE (strings) to true (boolean). Co-authored-by: Abhisek Pattnaik <[email protected]> closes #1021
1 parent 8111e38 commit f92c08b

File tree

5 files changed

+17
-7
lines changed

5 files changed

+17
-7
lines changed

lib/toBoolean.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ function toBoolean(str, strict) {
1313
(0, _assertString.default)(str);
1414

1515
if (strict) {
16-
return str === '1' || str === 'true';
16+
return str === '1' || /^true$/i.test(str);
1717
}
1818

19-
return str !== '0' && str !== 'false' && str !== '';
19+
return str !== '0' && !/^false$/i.test(str) && str !== '';
2020
}
2121

2222
module.exports = exports.default;

src/lib/toBoolean.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import assertString from './util/assertString';
33
export default function toBoolean(str, strict) {
44
assertString(str);
55
if (strict) {
6-
return str === '1' || str === 'true';
6+
return str === '1' || /^true$/i.test(str);
77
}
8-
return str !== '0' && str !== 'false' && str !== '';
8+
return str !== '0' && !/^false$/i.test(str) && str !== '';
99
}

test/sanitizers.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,13 @@ describe('Sanitizers', () => {
3434
'': false,
3535
1: true,
3636
true: true,
37+
True: true,
38+
TRUE: true,
3739
foobar: true,
3840
' ': true,
41+
false: false,
42+
False: false,
43+
FALSE: false,
3944
},
4045
});
4146
test({
@@ -46,8 +51,13 @@ describe('Sanitizers', () => {
4651
'': false,
4752
1: true,
4853
true: true,
54+
True: true,
55+
TRUE: true,
4956
foobar: false,
5057
' ': false,
58+
false: false,
59+
False: false,
60+
FALSE: false,
5161
},
5262
});
5363
});

validator.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,10 @@ function toBoolean(str, strict) {
240240
assertString(str);
241241

242242
if (strict) {
243-
return str === '1' || str === 'true';
243+
return str === '1' || /^true$/i.test(str);
244244
}
245245

246-
return str !== '0' && str !== 'false' && str !== '';
246+
return str !== '0' && !/^false$/i.test(str) && str !== '';
247247
}
248248

249249
function equals(str, comparison) {

validator.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)