Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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.util.strings.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,28 @@ _.implode(["H", "o", "m", "e", "r"]);

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

#### 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
```

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

#### strContains

**Signature:** `_.strContains(str:String, search:String)`
Expand Down Expand Up @@ -95,4 +117,4 @@ _.toQuery({ forms: { perfect: "circle", imperfect: "square" } });
// => "forms%5Bperfect%5D=circle&forms%5Bimperfect%5D=square"
```

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
12 changes: 12 additions & 0 deletions test/util.strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ $(document).ready(function() {
assert.equal(_.implode(['H','o','m','e','r']), 'Homer', 'Should implode an array of characters into a single string.');
});

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('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('camelCase', function(assert) {
assert.equal(_.camelCase('punic-wars'), 'punicWars', 'Should convert a dashed-format string to camelCase.');
});
Expand Down
8 changes: 8 additions & 0 deletions underscore.util.strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@
return a.join('');
},

// Checks whether a string is "trueish"
isTrueish: function(v) {
if(_.isBoolean(v)) {
return v;
}
return !!JSON.parse((v || "false").toLowerCase());
},

// Converts a string to camel case
camelCase : function( string ){
return string.replace(/-([a-z])/g, function (g) { return g[1].toUpperCase(); });
Expand Down