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

100% Statements 42/42
100% Branches 1/1
100% Functions 1/1
100% Lines 42/42

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 431x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 13x 13x 13x 13x 13x 13x 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 { assert } from '@amalia/ext/typescript';
 
import { AmaliaFunctionDefault } from '../../AmaliaFunction';
 
export const stringContains = new AmaliaFunctionDefault<[string | null | undefined, string], boolean>({
  name: AmaliaFunctionKeys.CONTAINS,
  category: AmaliaFunctionCategory.STRING,
  nbParamsRequired: 2,
  description: 'Return true when the given Search String is present in the given Source String',
 
  exec: (sourceString, searchString) => {
    assert(
      sourceString !== null && sourceString !== undefined,
      `${AmaliaFunctionKeys.CONTAINS}: source string is null or undefined`,
    );
 
    return sourceString.toString().includes(searchString);
  },
 
  params: [
    { name: 'sourceString', description: 'String to look into: array, variables, fields, properties, string' },
    { name: 'searchString', description: 'String to look for: array, variables, fields, properties, string' },
  ],
 
  examples: [
    {
      desc: 'Returns true if Opportunity type contains Renewal.',
      formula: 'CONTAINS(opportunity.type, "Renewal")' as AmaliaFormula,
    },
    {
      desc: 'Returns true.',
      formula: 'CONTAINS("Jean Dupont", "Jean")' as AmaliaFormula,
      result: true,
    },
    {
      desc: 'Returns false since the check is case sensitive.',
      formula: 'CONTAINS("Opportunity - Renewal XYZ", "renewal")' as AmaliaFormula,
      result: false,
    },
  ],
});