(cond: Omit<Conditional, 'arg' | 'global'>, value: any)
| 7 | const count = (vals: any[]) => vals.map((v) => typeof v !== 'undefined').filter(Boolean).length; |
| 8 | |
| 9 | export const testValue = (cond: Omit<Conditional, 'arg' | 'global'>, value: any) => { |
| 10 | const { exists, eq, neq, truthy } = cond as any; |
| 11 | if (count([exists, eq, neq, truthy]) > 1) { |
| 12 | throw new Error(`Invalid conditional test ${JSON.stringify({ exists, eq, neq })}`); |
| 13 | } |
| 14 | if (typeof eq !== 'undefined') { |
| 15 | return isEqual(value, eq); |
| 16 | } |
| 17 | if (typeof neq !== 'undefined') { |
| 18 | return !isEqual(value, neq); |
| 19 | } |
| 20 | if (typeof exists !== 'undefined') { |
| 21 | const valueExists = typeof value !== 'undefined'; |
| 22 | return exists ? valueExists : !valueExists; |
| 23 | } |
| 24 | const shouldBeTruthy = typeof truthy === 'undefined' ? true : truthy; |
| 25 | return shouldBeTruthy ? !!value : !value; |
| 26 | }; |
| 27 | |
| 28 | /** |
| 29 | * Helper function to include/exclude an arg based on the value of other other args |
no test coverage detected
searching dependent graphs…