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

100% Statements 70/70
90.9% Branches 10/11
50% Functions 1/2
100% Lines 70/70

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 62 63 64 65 66 67 68 69 70 711x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 1x 1x 7x 8x 2x 2x 5x 5x 5x 8x 1x 1x 4x 4x 1x 1x 3x 3x 3x 3x 8x 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  
import { AmaliaFunctionCategory, AmaliaFunctionKeys, type AmaliaFormula } from '@amalia/amalia-lang/formula/types';
import { FormatsEnum } from '@amalia/data-capture/fields/types';
import { type ComputeEngineResult } from '@amalia/payout-calculation/types';
 
import { AmaliaFunctionDefault } from '../../AmaliaFunction';
 
export const arrayGetInTable = new AmaliaFunctionDefault<
  [ComputeEngineResult, ComputeEngineResult, ComputeEngineResult],
  ComputeEngineResult
>({
  name: AmaliaFunctionKeys.GETINTABLE,
  category: AmaliaFunctionCategory.ARRAY,
  nbParamsRequired: 3,
  description: 'Given a table variable, returns the value of a cell.',
 
  exec: (table, rowIndex, columnIndex) => {
    if (!Array.isArray(table)) {
      throw new Error('In GetInTable, table is not an array. Are you sure this is a table variable?');
    }
 
    if (typeof rowIndex !== 'number' || typeof columnIndex !== 'number') {
      throw new Error('In GetInTable, Row or Column is not numeric');
    }
 
    const row = table[rowIndex];
 
    if (!row) {
      return null;
    }
 
    if (!Array.isArray(row)) {
      throw new Error('In GetInTable, Row is not an array. Does your table have two dimensions?');
    }
 
    const value = row[columnIndex] as ComputeEngineResult;
 
    // Undefined in the calculation engine has no meaning, we prefer `null`.
    return value ?? null;
  },
 
  execMock: () => 1,
 
  params: [
    { name: 'table', description: 'The table used to fetch the value', validFormats: [FormatsEnum.table] },
    { name: 'rowIndex', description: 'Number of the row starting at 0.' },
    { name: 'columnIndex', description: 'Number of the column starting at 0.' },
  ],
 
  examples: [
    {
      desc: 'Returns 200',
      formula: 'GETINTABLE([[100], [200], [300]], 1, 0)' as AmaliaFormula,
      result: 200,
    },
    {
      desc: 'Returns 2000',
      formula: 'GETINTABLE([[100, 1000], [200, 2000], [300, 3000]], 1, 1)' as AmaliaFormula,
      result: 2000,
    },
    {
      desc: 'Returns null because the rowIndex calls a value out of the table',
      formula: 'GETINTABLE([[100, 1000], [200, 2000], [300, 3000]], 5, 1)' as AmaliaFormula,
      result: null,
    },
    {
      desc: 'Returns the value of the first cell of the table statement.tierTable',
      formula: 'GETINTABLE(statement.tierTable, 0, 0)' as AmaliaFormula,
    },
  ],
});