1 | export function isTruthy ( node ) {
|
2 | if ( node.type === 'Literal' ) return !!node.value;
|
3 | if ( node.type === 'ParenthesizedExpression' ) return isTruthy( node.expression );
|
4 | if ( node.operator in operators ) return operators[ node.operator ]( node );
|
5 | }
|
6 |
|
7 | export function isFalsy ( node ) {
|
8 | return not( isTruthy( node ) );
|
9 | }
|
10 |
|
11 | function not ( value ) {
|
12 | return value === undefined ? value : !value;
|
13 | }
|
14 |
|
15 | function equals ( a, b, strict ) {
|
16 | if ( a.type !== b.type ) return undefined;
|
17 | if ( a.type === 'Literal' ) return strict ? a.value === b.value : a.value == b.value;
|
18 | }
|
19 |
|
20 | const operators = {
|
21 | '==': x => {
|
22 | return equals( x.left, x.right, false );
|
23 | },
|
24 |
|
25 | '!=': x => not( operators['==']( x ) ),
|
26 |
|
27 | '===': x => {
|
28 | return equals( x.left, x.right, true );
|
29 | },
|
30 |
|
31 | '!==': x => not( operators['===']( x ) ),
|
32 |
|
33 | '!': x => isFalsy( x.argument ),
|
34 |
|
35 | '&&': x => isTruthy( x.left ) && isTruthy( x.right ),
|
36 |
|
37 | '||': x => isTruthy( x.left ) || isTruthy( x.right )
|
38 | };
|