All files / libs/amalia-lang/formula/evaluate/shared/src/functions/string/match index.ts

100% Statements 51/51
100% Branches 8/8
100% Functions 1/1
100% Lines 51/51

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 521x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 1x 1x 5x 6x 6x 4x 4x 7x 7x 4x 3x 3x 4x 2x 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 { type RecordContent } from '@amalia/data-capture/connectors/types';
 
import { AmaliaFunctionDefault } from '../../AmaliaFunction';
 
export const stringMatch = new AmaliaFunctionDefault<[string, RecordContent[], string[]?], boolean>({
  name: AmaliaFunctionKeys.MATCH,
  category: AmaliaFunctionCategory.STRING,
  nbParamsRequired: 2,
  description:
    'Return true if lookupValue matches searchKey in lookupArray. If you are using Team Hierarchy on Amalia, please use MATCH_USERS_ASSIGNED function',
 
  exec: (lookupValue, lookupArray, searchKeys) => {
    if (!lookupValue) {
      return false;
    }
 
    const keys = searchKeys || ['id'];
    for (const object of lookupArray) {
      let hasMatch = true;
      for (const key of keys) {
        hasMatch &&= lookupValue.toString().includes(object[key] as string);
      }
      if (hasMatch) {
        return true;
      }
    }
    return false;
  },
 
  // + 'Loop through the `lookupArray`. Returns true if a value of one of '
  // + 'the `searchKeys` properties of one item of this array is equal or included inside the `lookupValue`.'
  params: [
    { name: 'lookupValue', description: 'Value to look up: variables, fields, properties, string, keywords' },
    { name: 'lookupArray', description: 'Array to search in: array, keywords such as team.employees' },
    { name: 'searchKeys', description: 'Keys to look up such as ["externalId"]', defaultValue: '["id"]' },
  ],
 
  examples: [
    {
      desc: "Returns true if Opportunity Owner ID matches one of the user's team members.",
      formula: 'MATCH(opportunity.ownerId, user.teamMembers, ["externalId"])',
    },
    {
      desc: 'Returns true.',
      formula:
        'MATCH("Jean Dupont", [{ firstName: "Jean", lastName: "Dupont" }], ["firstName", "lastName"])' as AmaliaFormula,
      result: true,
    },
  ],
});