All files / libs/amalia-lang/formula/evaluate/shared/src/functions/array/get-at-index index.ts

100% Statements 61/61
100% Branches 4/4
100% Functions 3/3
100% Lines 61/61

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 53 54 55 56 57 58 59 60 61 621x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 1x 1x 1x 1x  
import { AmaliaFunctionCategory, AmaliaFunctionKeys, type AmaliaFormula } from '@amalia/amalia-lang/formula/types';
import { FormatsEnum } from '@amalia/data-capture/fields/types';
 
import { CalculationParser } from '../../../CalculationParser';
import { SanitizeFormula } from '../../../sanitizeFormula';
import { AmaliaFunctionRawArgs } from '../../AmaliaFunction';
import { getValueOrFormula } from '../../utils';
 
export const arrayGetAtIndex = new AmaliaFunctionRawArgs({
  name: AmaliaFunctionKeys.GETATINDEX,
  category: AmaliaFunctionCategory.ARRAY,
 
  exec: (args, _, scope) => CalculationParser.getFunctionResult(args, scope, AmaliaFunctionKeys.GETATINDEX),
  execMock: () => 1,
 
  nbParamsRequired: 2,
  description: 'Get a value from a filtered dataset',
 
  params: [
    {
      name: 'array',
      description: 'Array, Filters or Links',
      validFormats: [FormatsEnum.table],
    },
    {
      name: 'rowIndex',
      description: 'Number of the record (row) starting at 0.',
    },
    {
      name: 'result',
      description: 'Name of the field to return. It can include a formula.',
      defaultValue: '',
    },
    {
      name: 'condition',
      description: 'Boolean condition to filter on the array before getting the index (OPTIONAL).',
      defaultValue: '',
    },
  ],
 
  examples: [
    {
      desc: 'Returns the amount of the first record in this filtered dataset.',
      formula: 'GETATINDEX(filter.closedInPeriod, 0, opportunity.amount)' as AmaliaFormula,
    },
    {
      desc: 'Returns the amount of the second record in this filtered dataset with invoiceSent equals to true.',
      formula:
        'GETATINDEX(filter.closedInPeriod, 1, opportunity.amount, IF(opportunity.invoiceSent, 1, 0))' as AmaliaFormula,
    },
  ],
 
  generateComputedFunctionResult: (args) => ({
    array: getValueOrFormula(args[0]),
    index: getValueOrFormula(args[1]),
    formula: SanitizeFormula.amaliaFormulaToMathJs(getValueOrFormula(args[2])) as AmaliaFormula,
    predicate: args[3] && (SanitizeFormula.amaliaFormulaToMathJs(getValueOrFormula(args[3])) as AmaliaFormula),
  }),
 
  parametersToEscapeOnParse: [0, 2, 3],
});