Skip to content

Commit

Permalink
feat(toBoolean): use regex for false string value (#1081)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ezkemboi authored Feb 17, 2020
1 parent 8111e38 commit f92c08b
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lib/toBoolean.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ function toBoolean(str, strict) {
(0, _assertString.default)(str);

if (strict) {
return str === '1' || str === 'true';
return str === '1' || /^true$/i.test(str);
}

return str !== '0' && str !== 'false' && str !== '';
return str !== '0' && !/^false$/i.test(str) && str !== '';
}

module.exports = exports.default;
Expand Down
4 changes: 2 additions & 2 deletions src/lib/toBoolean.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import assertString from './util/assertString';
export default function toBoolean(str, strict) {
assertString(str);
if (strict) {
return str === '1' || str === 'true';
return str === '1' || /^true$/i.test(str);
}
return str !== '0' && str !== 'false' && str !== '';
return str !== '0' && !/^false$/i.test(str) && str !== '';
}
10 changes: 10 additions & 0 deletions test/sanitizers.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,13 @@ describe('Sanitizers', () => {
'': false,
1: true,
true: true,
True: true,
TRUE: true,
foobar: true,
' ': true,
false: false,
False: false,
FALSE: false,
},
});
test({
Expand All @@ -46,8 +51,13 @@ describe('Sanitizers', () => {
'': false,
1: true,
true: true,
True: true,
TRUE: true,
foobar: false,
' ': false,
false: false,
False: false,
FALSE: false,
},
});
});
Expand Down
4 changes: 2 additions & 2 deletions validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,10 @@ function toBoolean(str, strict) {
assertString(str);

if (strict) {
return str === '1' || str === 'true';
return str === '1' || /^true$/i.test(str);
}

return str !== '0' && str !== 'false' && str !== '';
return str !== '0' && !/^false$/i.test(str) && str !== '';
}

function equals(str, comparison) {
Expand Down
2 changes: 1 addition & 1 deletion validator.min.js

Large diffs are not rendered by default.

0 comments on commit f92c08b

Please sign in to comment.