Skip to content

Commit 04f7272

Browse files
committed
Switch empty object to falsy, and add iterator check
1 parent f9f565c commit 04f7272

File tree

4 files changed

+31
-11
lines changed

4 files changed

+31
-11
lines changed

asyncLogic.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,16 @@ class AsyncLogicEngine {
5656
*/
5757
truthy (value) {
5858
if (!value) return value
59-
if (Array.isArray(value) && value.length === 0) return false
60-
// Uncomment the following line to switch empty object to falsy.
61-
// if (value && typeof value === 'object') return Object.keys(value).length > 0
59+
// The following check could be erased, as it'd be caught by the iterator check,
60+
// but it's here for performance reasons.
61+
if (Array.isArray(value)) return value.length > 0
62+
if (typeof value === 'object') {
63+
if (value[Symbol.iterator]) {
64+
if ('length' in value && value.length === 0) return false
65+
if ('size' in value && value.size === 0) return false
66+
}
67+
if (value.constructor.name === 'Object') return Object.keys(value).length > 0
68+
}
6269
return value
6370
}
6471

logic.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,16 @@ class LogicEngine {
4747
*/
4848
truthy (value) {
4949
if (!value) return value
50-
if (Array.isArray(value) && value.length === 0) return false
51-
// Uncomment the following line to switch empty object to falsy.
52-
// if (typeof value === 'object') return Object.keys(value).length > 0
50+
// The following check could be erased, as it'd be caught by the iterator check,
51+
// but it's here for performance reasons.
52+
if (Array.isArray(value)) return value.length > 0
53+
if (typeof value === 'object') {
54+
if (value[Symbol.iterator]) {
55+
if ('length' in value && value.length === 0) return false
56+
if ('size' in value && value.size === 0) return false
57+
}
58+
if (value.constructor.name === 'Object') return Object.keys(value).length > 0
59+
}
5360
return value
5461
}
5562

suites/control/if.json

+9-3
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121
},
2222
"Object checks",
2323
{
24-
"description": "Objects are always truthy, even without keys.",
24+
"description": "Empty object is falsy.",
2525
"rule": { "if": [{}, "apple", "banana"] },
2626
"data": null,
27-
"result": "apple"
27+
"result": "banana"
2828
},
2929
{
30-
"description": "Objects are always truthy, with keys defined",
30+
"description": "Objects are truthy, with keys defined",
3131
"rule": { "if": [{ "val": [] }, "apple", "banana"] },
3232
"data": { "some": "value" },
3333
"result": "apple"
@@ -240,6 +240,12 @@
240240
"description": "Returns first truthy value, some conditions truthy (8)",
241241
"rule": { "if": [{}, "apple", 0, "banana", 7, "carrot", "date"] },
242242
"data": null,
243+
"result": "carrot"
244+
},
245+
{
246+
"description": "Returns first truthy value, some conditions truthy (9)",
247+
"rule": { "if": [{ "val": "a" }, "apple", 0, "banana", 7, "carrot", "date"] },
248+
"data": { "a": { "b": 1 } },
243249
"result": "apple"
244250
},
245251
"Bad Arguments",

suites/truthiness.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
{
4141
"description": "Truthy: {}",
4242
"rule": { "!!": [{}] },
43-
"result": true,
43+
"result": false,
4444
"data": null
4545
},
4646
{
@@ -59,7 +59,7 @@
5959
{
6060
"description": "Truthy: Zero Key Object",
6161
"rule": { "!!": { "val": "obj" } },
62-
"result": true,
62+
"result": false,
6363
"data": { "obj": {} }
6464
},
6565
{

0 commit comments

Comments
 (0)