<!DOCTYPE html>
<html>
<head>
<script data-require="lodash.js@*" data-semver="2.4.1" src="http://cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.js"></script>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/mocha/1.13.0/mocha.min.css" />
<script src="//chaijs.com/chai.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/mocha/1.13.0/mocha.min.js"></script>
<script src="strToBool.js"></script>
</head>
<body>
<script>mocha.setup('bdd')</script>
<div id="mocha"></div>
<script src="test.strToBool.js"></script>
<script>
mocha.run();
</script>
</body>
</html>
_.strToBool()
-------------
A mixin for Lo-Dash that will convert any "truthy" strings to boolean true.
```
_.strToBool('y') === true
```
Open ```strToBool.js``` for the code or run Live Preview for the Mocha/Chai
test.
var assert = chai.assert,
expect = chai.expect,
should = chai.should();
describe('_.strToBool', function(){
it('yes', function() {
assert.isTrue(_.strToBool('yes'));
});
it('YES', function() {
assert.isTrue(_.strToBool('YES'));
});
it('Y', function() {
assert.isTrue(_.strToBool('Y'));
});
it('true (str)', function() {
assert.isTrue(_.strToBool('true'));
});
it('TRUE (str)', function() {
assert.isTrue(_.strToBool('TRUE'));
});
it('true (bool)', function() {
assert.isTrue(_.strToBool(true));
});
it('false (bool)', function() {
assert.isFalse(_.strToBool(false));
});
it('1 (str)', function() {
assert.isTrue(_.strToBool('1'));
});
it('1 num)', function() {
assert.isTrue(_.strToBool(1));
});
it('0 (num)', function() {
assert.isFalse(_.strToBool(0));
});
});
_.mixin({
strToBool: function(string) {
var areTrue = [
'yes',
'true',
true,
'y',
1,
'1'
];
if(_.isString(string)) {
string = string.toLowerCase();
}
if(_.indexOf(areTrue, string) > -1) {
return true;
}
return false;
}
});