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

100% Statements 52/52
100% Branches 2/2
50% Functions 1/2
100% Lines 52/52

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 531x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 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  
import { AmaliaFunctionCategory, AmaliaFunctionKeys, type AmaliaFormula } from '@amalia/amalia-lang/formula/types';
import { assert } from '@amalia/ext/typescript';
 
import { AmaliaFunctionDefault } from '../../AmaliaFunction';
 
export const stringSplit = new AmaliaFunctionDefault<[string | null | undefined, string, number], string | null>({
  name: AmaliaFunctionKeys.SPLIT,
  category: AmaliaFunctionCategory.STRING,
  nbParamsRequired: 3,
  description: 'Takes a pattern and divides a String into an ordered list of substrings',
 
  execMock: () => 'mock',
  exec: (text, separator, index) => {
    assert(text !== null && text !== undefined, `${AmaliaFunctionKeys.SPLIT}: source string is null or undefined`);
    return text.toString().split(separator)[index] ?? null;
  },
 
  params: [
    { name: 'string', description: 'String: variables, fields, properties, string' },
    {
      name: 'separator',
      description: 'The pattern describing where each split should occur',
    },
    {
      name: 'position',
      description: 'The position of the split part the function should return. Starting at 0 for the first part',
    },
  ],
 
  examples: [
    {
      desc: 'Returns first.',
      formula: 'SPLIT("first,second", ",", 0)' as AmaliaFormula,
      result: 'first',
    },
    {
      desc: 'Returns second.',
      formula: 'SPLIT("first,second", ",", 1)' as AmaliaFormula,
      result: 'second',
    },
    {
      desc: 'Returns null since all split parts are before that position.',
      formula: 'SPLIT("first,second", ",", 30)' as AmaliaFormula,
      result: null,
    },
    {
      desc: 'Returns first.',
      formula: 'SPLIT("first", ",", 0)' as AmaliaFormula,
      result: 'first',
    },
  ],
});