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

100% Statements 53/53
100% Branches 2/2
100% Functions 1/1
100% Lines 53/53

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 541x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 41x 41x 41x 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  
import { isEqual, pick, uniqWith } from 'lodash-es';
 
import { AmaliaFunctionCategory, AmaliaFunctionKeys, type AmaliaFormula } from '@amalia/amalia-lang/formula/types';
import { type RecordContent } from '@amalia/data-capture/connectors/types';
import { FormatsEnum } from '@amalia/data-capture/fields/types';
import { type ComputeEngineResult } from '@amalia/payout-calculation/types';
 
import { AmaliaFunctionDefault } from '../../AmaliaFunction';
 
export const arrayUnique = new AmaliaFunctionDefault<[RecordContent[], string[] | string], ComputeEngineResult>({
  name: AmaliaFunctionKeys.UNIQUE,
  category: AmaliaFunctionCategory.ARRAY,
 
  exec: (rows, fields) =>
    uniqWith(rows, (actVal, otherVal) => {
      const arrPick = pick(actVal, fields);
      const otherPick = pick(otherVal, fields);
      return isEqual(arrPick, otherPick);
    }),
 
  nbParamsRequired: 2,
 
  description:
    'Return the filtered dataset with unique values on a parameter. Used in advanced settings of filters or links and can be combined with SORT function.',
 
  params: [
    {
      name: 'array',
      description: '$rows for the dataset of the active filter or SORT($rows, ...) if you want the sorted dataset.',
      validFormats: [FormatsEnum.table],
    },
    {
      name: 'parameters',
      description:
        'Fields or properties on which we want to have only unique records. Format as follow: "machineName".',
    },
  ],
 
  examples: [
    {
      desc: 'Returns the dataset with unique records on the property “opportunity name”',
      formula: 'UNIQUE($rows, “accountId”)' as AmaliaFormula,
    },
    {
      desc: 'Returns the dataset with unique records on both properties “opportunity name” and “accountId”',
      formula: 'UNIQUE($rows, [“opportunityName”, “accountId”])' as AmaliaFormula,
    },
    {
      desc: 'Returns the dataset with unique records having the highest value for amount on the property “opportunity name”',
      formula: 'UNIQUE(SORT($rows, “amount”, “DESC”), “opportunityName”)' as AmaliaFormula,
    },
  ],
});