Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

isTrueish #255

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion docs/underscore.function.predicates.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)`
Expand Down Expand Up @@ -333,4 +355,4 @@ _.isZero("Pythagoras");

--------------------------------------------------------------------------------

[momentjs]:http://momentjs.com/
[momentjs]:http://momentjs.com/
2 changes: 1 addition & 1 deletion docs/underscore.util.strings.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,4 @@ _.toQuery({ forms: { perfect: "circle", imperfect: "square" } });
// => "forms%5Bperfect%5D=circle&forms%5Bimperfect%5D=square"
```

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
jgonggrijp marked this conversation as resolved.
Show resolved Hide resolved
16 changes: 16 additions & 0 deletions test/function.predicates.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
6 changes: 6 additions & 0 deletions underscore.function.predicates.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,11 @@ _.mixin({
}

return true;
},

// Checks whether a string is "trueish"
isTrueish: function(v) {
v = '' + v;
return !!(+v) || v.toLowerCase() === 'true';
}
});