diff --git a/docs/underscore.function.predicates.js.md b/docs/underscore.function.predicates.js.md index 6983b9b..5991569 100644 --- a/docs/underscore.function.predicates.js.md +++ b/docs/underscore.function.predicates.js.md @@ -294,6 +294,28 @@ _.isSequential(new Date); -------------------------------------------------------------------------------- +#### isTrueish + +**Signature:** `_.isTrueish(value:String)` + +Checks whether an optional string parses to `true` with JSON. Lowercases variable before checking to ensure strings like `"True"` evaluate properly. + +```javascript +_.isTrueish(); +// => false + +_.isTrueish(true); +// => true + +_.isTrueish('true'); +// => true + +_.isTrueish('FaLsE'); +// => false +``` + +-------------------------------------------------------------------------------- + #### isValidDate **Signature:** `_.isValidDate(value:Any)` @@ -333,4 +355,4 @@ _.isZero("Pythagoras"); -------------------------------------------------------------------------------- -[momentjs]:http://momentjs.com/ \ No newline at end of file +[momentjs]:http://momentjs.com/ diff --git a/docs/underscore.util.strings.js.md b/docs/underscore.util.strings.js.md index 7a1112d..57c2fa9 100644 --- a/docs/underscore.util.strings.js.md +++ b/docs/underscore.util.strings.js.md @@ -95,4 +95,4 @@ _.toQuery({ forms: { perfect: "circle", imperfect: "square" } }); // => "forms%5Bperfect%5D=circle&forms%5Bimperfect%5D=square" ``` --------------------------------------------------------------------------------- \ No newline at end of file +-------------------------------------------------------------------------------- diff --git a/test/function.predicates.js b/test/function.predicates.js index 791d2e7..64840dc 100644 --- a/test/function.predicates.js +++ b/test/function.predicates.js @@ -239,6 +239,22 @@ $(document).ready(function() { assert.equal(_.isDecreasing.apply(null, decNM), false, 'should identify when its arguments monotonically decrease'); }); + QUnit.test('isTrueish', function(assert) { + assert.equal(_.isTrueish(), false, 'should return false'); + assert.equal(_.isTrueish(undefined), false, 'should return false'); + assert.equal(_.isTrueish(null), false, 'should return false'); + assert.equal(_.isTrueish(true), true, 'should return true'); + assert.equal(_.isTrueish(false), false, 'should return false'); + assert.equal(_.isTrueish(1), true, 'should return true'); + assert.equal(_.isTrueish(0), false, 'should return false'); + assert.equal(_.isTrueish({}), false, 'should return false'); + assert.equal(_.isTrueish('}'), false, 'should return false'); + assert.equal(_.isTrueish('true'), true, 'should return true'); + assert.equal(_.isTrueish('false'), false, 'should return false'); + assert.equal(_.isTrueish('True'), true, 'should return true'); + assert.equal(_.isTrueish('FaLsE'), false, 'should return false'); + }); + QUnit.test("isValidDate", function(assert) { assert.equal(_.isValidDate(new Date), true, 'should recognize a fresh Date instance as valid'); assert.equal(!_.isValidDate(new Date("bad date")), true, 'should recognize a Date constructed with gibberish'); diff --git a/underscore.function.predicates.js b/underscore.function.predicates.js index 492a11c..9973d13 100644 --- a/underscore.function.predicates.js +++ b/underscore.function.predicates.js @@ -103,5 +103,11 @@ _.mixin({ } return true; + }, + + // Checks whether a string is "trueish" + isTrueish: function(v) { + v = '' + v; + return !!(+v) || v.toLowerCase() === 'true'; } });