| 1 |
class C { |
| 2 |
static testIf(o) { |
| 3 |
if (o?.a.b.c.d) { |
| 4 |
return true; |
| 5 |
} |
| 6 |
return false; |
| 7 |
} |
| 8 |
static testConditional(o) { |
| 9 |
return o?.a.b?.c.d ? true : false; |
| 10 |
} |
| 11 |
static testLoop(o) { |
| 12 |
while (o?.a.b.c.d) { |
| 13 |
for (; o?.a.b.c?.d; ) { |
| 14 |
let i = 0; |
| 15 |
do { |
| 16 |
i++; |
| 17 |
if (i === 2) { |
| 18 |
return true; |
| 19 |
} |
| 20 |
} while (o?.a.b?.c.d); |
| 21 |
} |
| 22 |
} |
| 23 |
return false; |
| 24 |
} |
| 25 |
static testNegate(o) { |
| 26 |
return !!o?.a.b?.c.d; |
| 27 |
} |
| 28 |
static testIfDeep(o) { |
| 29 |
if (o.obj?.a.b?.c.d) { |
| 30 |
return true; |
| 31 |
} |
| 32 |
return false; |
| 33 |
} |
| 34 |
static testConditionalDeep(o) { |
| 35 |
return o.obj?.a.b?.c.d ? true : false; |
| 36 |
} |
| 37 |
static testLoopDeep(o) { |
| 38 |
while (o.obj?.a.b.c.d) { |
| 39 |
for (; o.obj?.a.b.c?.d; ) { |
| 40 |
let i = 0; |
| 41 |
do { |
| 42 |
i++; |
| 43 |
if (i === 2) { |
| 44 |
return true; |
| 45 |
} |
| 46 |
} while (o.obj?.a.b?.c.d); |
| 47 |
} |
| 48 |
} |
| 49 |
return false; |
| 50 |
} |
| 51 |
static testNegateDeep(o) { |
| 52 |
return !!o.obj?.a.b?.c.d; |
| 53 |
} |
| 54 |
|
| 55 |
static testLogicalInIf(o) { |
| 56 |
if (o?.a.b?.c.d && o?.a?.b.c.d) { |
| 57 |
return true; |
| 58 |
} |
| 59 |
return false; |
| 60 |
} |
| 61 |
|
| 62 |
static testLogicalInReturn(o) { |
| 63 |
return o?.a.b?.c.d && o?.a?.b.c.d; |
| 64 |
} |
| 65 |
|
| 66 |
static testNullishCoalescing(o) { |
| 67 |
if (o?.a.b?.c.non_existent ?? o?.a.b?.c.d) { |
| 68 |
return o?.a.b?.c.non_existent ?? o?.a.b?.c.d; |
| 69 |
} |
| 70 |
return o?.a.b?.c.non_existent ?? o; |
| 71 |
} |
| 72 |
|
| 73 |
static test() { |
| 74 |
const c = { |
| 75 |
a: { |
| 76 |
b: { |
| 77 |
c: { |
| 78 |
d: 2, |
| 79 |
}, |
| 80 |
}, |
| 81 |
}, |
| 82 |
}; |
| 83 |
expect(C.testIf(c)).toBe(true); |
| 84 |
expect(C.testConditional(c)).toBe(true); |
| 85 |
expect(C.testLoop(c)).toBe(true); |
| 86 |
expect(C.testNegate(c)).toBe(true); |
| 87 |
|
| 88 |
expect(C.testIfDeep({ obj: c })).toBe(true); |
| 89 |
expect(C.testConditionalDeep({ obj: c })).toBe(true); |
| 90 |
expect(C.testLoopDeep({ obj: c })).toBe(true); |
| 91 |
expect(C.testNegateDeep({ obj: c })).toBe(true); |
| 92 |
|
| 93 |
expect(C.testLogicalInIf(c)).toBe(true); |
| 94 |
expect(C.testLogicalInReturn(c)).toBe(2); |
| 95 |
|
| 96 |
expect(C.testNullishCoalescing(c)).toBe(2); |
| 97 |
} |
| 98 |
|
| 99 |
static testNullish() { |
| 100 |
for (const n of [null, undefined]) { |
| 101 |
expect(C.testIf(n)).toBe(false); |
| 102 |
expect(C.testConditional(n)).toBe(false); |
| 103 |
expect(C.testLoop(n)).toBe(false); |
| 104 |
expect(C.testNegate(n)).toBe(false); |
| 105 |
|
| 106 |
expect(C.testIfDeep({ obj: n })).toBe(false); |
| 107 |
expect(C.testConditionalDeep({ obj: n })).toBe(false); |
| 108 |
expect(C.testLoopDeep({ obj: n })).toBe(false); |
| 109 |
expect(C.testNegateDeep({ obj: n })).toBe(false); |
| 110 |
|
| 111 |
expect(C.testLogicalInIf(n)).toBe(false); |
| 112 |
expect(C.testLogicalInReturn(n)).toBe(undefined); |
| 113 |
|
| 114 |
expect(C.testNullishCoalescing(n)).toBe(n); |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
C.test(); |
| 120 |
C.testNullish(); |