( formula: types.Formula, context: EvalFormulaContext )
| 32 | * @param context - Collection context containing property schema and values. |
| 33 | */ |
| 34 | export function evalFormula( |
| 35 | formula: types.Formula, |
| 36 | context: EvalFormulaContext |
| 37 | ): types.FormulaResult { |
| 38 | const { endDate, ...ctx } = context |
| 39 | |
| 40 | // TODO: coerce return type using `formula.return_type` |
| 41 | switch (formula?.type) { |
| 42 | case 'symbol': |
| 43 | // TODO: this isn't documented anywhere but seen in the wild |
| 44 | return formula.name === 'true' |
| 45 | |
| 46 | case 'constant': { |
| 47 | const value = formula.value |
| 48 | switch (formula.result_type) { |
| 49 | case 'text': |
| 50 | return value |
| 51 | case 'number': |
| 52 | return Number.parseFloat(value) |
| 53 | default: |
| 54 | return value |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | case 'property': { |
| 59 | const value = ctx.properties[formula.id] |
| 60 | const text = getTextContent(value) |
| 61 | |
| 62 | switch (formula.result_type) { |
| 63 | case 'text': |
| 64 | return text |
| 65 | |
| 66 | case 'number': |
| 67 | return Number.parseFloat(text) |
| 68 | |
| 69 | case 'boolean': |
| 70 | // TODO: handle chceckbox properties |
| 71 | if (typeof text === 'string') { |
| 72 | return text === 'true' |
| 73 | } else { |
| 74 | return !!text |
| 75 | } |
| 76 | |
| 77 | case 'date': { |
| 78 | // console.log('date', text, value) |
| 79 | |
| 80 | const v = getDateValue(value!) |
| 81 | if (v) { |
| 82 | if (endDate && v.end_date) { |
| 83 | const date = new Date(v.end_date) |
| 84 | return new Date( |
| 85 | date.getUTCFullYear(), |
| 86 | date.getUTCMonth(), |
| 87 | date.getUTCDate() |
| 88 | ) |
| 89 | } else { |
| 90 | const date = new Date(v.start_date) |
| 91 | return new Date( |
no test coverage detected