From 256763c0b3dd17652bf49f66f1b30b58d1a31596 Mon Sep 17 00:00:00 2001 From: Jesse Lieberg Date: Fri, 24 Mar 2023 09:22:58 -0600 Subject: [PATCH 1/5] add isTrueish to strings tools, add docs, add tests --- docs/underscore.util.strings.js.md | 24 +++++++++++++++++++++++- test/util.strings.js | 12 ++++++++++++ underscore.util.strings.js | 8 ++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/docs/underscore.util.strings.js.md b/docs/underscore.util.strings.js.md index 7a1112d..2a1a07e 100644 --- a/docs/underscore.util.strings.js.md +++ b/docs/underscore.util.strings.js.md @@ -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)` @@ -95,4 +117,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/util.strings.js b/test/util.strings.js index a88c5e3..02f18f0 100644 --- a/test/util.strings.js +++ b/test/util.strings.js @@ -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.'); }); diff --git a/underscore.util.strings.js b/underscore.util.strings.js index 291c26a..84e890b 100644 --- a/underscore.util.strings.js +++ b/underscore.util.strings.js @@ -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(); }); From 36446a1bd244155053d6ea8a46ae9d3cc57c49d2 Mon Sep 17 00:00:00 2001 From: Jesse Lieberg Date: Mon, 27 Mar 2023 09:39:21 -0600 Subject: [PATCH 2/5] move to predicates, add test cases, redo to check if string --- docs/underscore.function.predicates.js.md | 24 ++++++++++++++++++++++- docs/underscore.util.strings.js.md | 22 --------------------- test/function.predicates.js | 15 ++++++++++++++ test/util.strings.js | 12 ------------ underscore.function.predicates.js | 8 ++++++++ underscore.util.strings.js | 8 -------- 6 files changed, 46 insertions(+), 43 deletions(-) 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 2a1a07e..57c2fa9 100644 --- a/docs/underscore.util.strings.js.md +++ b/docs/underscore.util.strings.js.md @@ -57,28 +57,6 @@ _.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)` diff --git a/test/function.predicates.js b/test/function.predicates.js index 791d2e7..52cd51b 100644 --- a/test/function.predicates.js +++ b/test/function.predicates.js @@ -239,6 +239,21 @@ $(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('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/test/util.strings.js b/test/util.strings.js index 02f18f0..a88c5e3 100644 --- a/test/util.strings.js +++ b/test/util.strings.js @@ -30,18 +30,6 @@ $(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.'); }); diff --git a/underscore.function.predicates.js b/underscore.function.predicates.js index 492a11c..42f5868 100644 --- a/underscore.function.predicates.js +++ b/underscore.function.predicates.js @@ -103,5 +103,13 @@ _.mixin({ } return true; + }, + + // Checks whether a string is "trueish" + isTrueish: function(v) { + if(_.isString(v)) { + return !!+v || v.toLowerCase() === 'true'; + } + return !!v; } }); diff --git a/underscore.util.strings.js b/underscore.util.strings.js index 84e890b..291c26a 100644 --- a/underscore.util.strings.js +++ b/underscore.util.strings.js @@ -103,14 +103,6 @@ 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(); }); From 463fca590677bdbc0caa1e4f6fa012832a71b0fd Mon Sep 17 00:00:00 2001 From: Jesse Lieberg Date: Tue, 28 Mar 2023 16:48:04 -0600 Subject: [PATCH 3/5] cast v to string, remove isString check --- underscore.function.predicates.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/underscore.function.predicates.js b/underscore.function.predicates.js index 42f5868..7ac6d9f 100644 --- a/underscore.function.predicates.js +++ b/underscore.function.predicates.js @@ -107,9 +107,7 @@ _.mixin({ // Checks whether a string is "trueish" isTrueish: function(v) { - if(_.isString(v)) { - return !!+v || v.toLowerCase() === 'true'; - } - return !!v; + v = '' + v; + return !!+v || v.toLowerCase() === 'true'; } }); From 06423a92b97a1684ab17c7aa6262ef3495527843 Mon Sep 17 00:00:00 2001 From: Jesse Lieberg Date: Tue, 28 Mar 2023 16:49:48 -0600 Subject: [PATCH 4/5] add test case --- test/function.predicates.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/function.predicates.js b/test/function.predicates.js index 52cd51b..64840dc 100644 --- a/test/function.predicates.js +++ b/test/function.predicates.js @@ -247,6 +247,7 @@ $(document).ready(function() { 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'); From d0e11d57b398636428f535a2b66d6d94d019abdb Mon Sep 17 00:00:00 2001 From: Jesse Lieberg Date: Fri, 31 Mar 2023 08:30:23 -0600 Subject: [PATCH 5/5] attempt to quiet jshint --- underscore.function.predicates.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/underscore.function.predicates.js b/underscore.function.predicates.js index 7ac6d9f..9973d13 100644 --- a/underscore.function.predicates.js +++ b/underscore.function.predicates.js @@ -108,6 +108,6 @@ _.mixin({ // Checks whether a string is "trueish" isTrueish: function(v) { v = '' + v; - return !!+v || v.toLowerCase() === 'true'; + return !!(+v) || v.toLowerCase() === 'true'; } });