1 import type { EscapeVariableType, LABEL_KEY_TYPE } from './interface';
2 import { LABEL_KEYS } from './interface';
4 type ReturnValue = { Value: String; Type: String };
7 * Builds a value label object.
9 export const buildLabelValueObject = (value: LABEL_KEY_TYPE) => ({
11 label: LABEL_KEYS[value],
15 * Escapes the sieve specific characters. (aka *, ? and \)
17 export const escapeCharacters = (text: string) => text.replace(/([*?])/g, '\\$1').replace(/\\/g, '\\\\');
20 * Unescape the sieve specific characters (*, ? and \)
22 export const unescapeCharacters = (text: string) => text.replace(/\\\\/g, '\\').replace(/\\([?*])/g, '$1');
25 * Escapes sieve variables
27 export const escapeVariables = (text: string): EscapeVariableType => {
28 const regex = /\$({[\w._]+})/g;
29 if (!text.match(regex)) {
34 Value: text.replace(regex, '${dollar}$1'),
35 Type: 'VariableString',
40 * Unescape sieve variables
42 export const unescapeVariables = (text: string | ReturnValue) => {
43 if (typeof text === 'string') {
46 const { Value: value, Type: type } = text;
47 if (type !== 'VariableString' || value.match(/\${(?!dollar)[\w._]+}/)) {
51 const regex = /\${dollar}({[\w._]+})/g;
53 return text.Value.replace(regex, '$$$1');
57 * Remove duplicates in array.
59 export const unique = <T>(arr: T[]) => [...new Set(arr)];
62 * Find last value that pass the given callback
64 export const findLatest = <T>(arr: T[], callback: (item: T) => {}) => {
67 if (callback(arr[i])) {