All files / libs/amalia-lang/formula/evaluate/shared/src/functions/misc/default index.ts

96.15% Statements 50/52
83.33% Branches 5/6
100% Functions 1/1
96.15% Lines 50/52

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 531x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 9x 9x 9x 9x 9x 9x 4x 4x 9x 2x 2x     2x 5x 5x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
import { AmaliaFunctionCategory, AmaliaFunctionKeys, type AmaliaFormula } from '@amalia/amalia-lang/formula/types';
import { type ComputeEngineResult } from '@amalia/payout-calculation/types';
 
import { AmaliaFunctionRawArgs } from '../../AmaliaFunction';
 
export const miscDefault = new AmaliaFunctionRawArgs({
  name: AmaliaFunctionKeys.DEFAULT,
  category: AmaliaFunctionCategory.MISC,
  nbParamsRequired: 2,
  description: 'Return a default value if the parameter is null or undefined',
 
  exec: (args, _, scope): ComputeEngineResult => {
    const [parameter, defaultValue] = args;
 
    try {
      const evaluationResult = parameter.evaluate(scope) as ComputeEngineResult;
 
      if (evaluationResult !== undefined && evaluationResult !== null) {
        return evaluationResult;
      }
    } catch (e) {
      // Silence TypeErrors.
      if (!(e instanceof TypeError)) {
        throw e;
      }
    }
 
    // If we're here, it means that either the evaluation returned null, or threw a type error.
    // Fallback to the default value.
    return defaultValue.evaluate(scope) as ComputeEngineResult;
  },
 
  params: [
    { name: 'parameter', description: 'Variables, Fields or Properties' },
    { name: 'defaultValue', description: 'Value to return if parameter is null or undefined.' },
  ],
 
  examples: [
    {
      desc: 'Returns the close date if it exists else, it returns the end date of the period.',
      formula: 'DEFAULT(opportunity.closeDate, statementPeriod.endDate)' as AmaliaFormula,
    },
    {
      desc: 'Returns the invoice date if it exists else, it returns an empty string.',
      formula: 'DEFAULT(opportunity.invoice.closeDate, "")' as AmaliaFormula,
    },
    {
      desc: 'Returns the amount cancelled if it exists else, it returns 0.',
      formula: 'DEFAULT(opportunity.amountCancelled, 0)' as AmaliaFormula,
    },
  ],
});