2022-10-12 14:21:49 +00:00
|
|
|
import { format } from 'date-fns';
|
2023-03-08 21:57:12 +00:00
|
|
|
import { Buffer } from 'buffer';
|
|
|
|
|
2022-11-17 21:05:54 +00:00
|
|
|
import {
|
|
|
|
DATE_TIME_FORMAT,
|
|
|
|
DATE_FORMAT,
|
|
|
|
TIME_FORMAT_HOURS_MINUTES,
|
|
|
|
} from './config';
|
2022-10-12 14:21:49 +00:00
|
|
|
import {
|
|
|
|
DEFAULT_PER_PAGE,
|
|
|
|
DEFAULT_PAGE,
|
|
|
|
} from './components/PaginationForTable';
|
|
|
|
|
|
|
|
// https://www.30secondsofcode.org/js/s/slugify
|
|
|
|
export const slugifyString = (str: any) => {
|
|
|
|
return str
|
|
|
|
.toLowerCase()
|
|
|
|
.trim()
|
|
|
|
.replace(/[^\w\s-]/g, '')
|
|
|
|
.replace(/[\s_-]+/g, '-')
|
|
|
|
.replace(/^-+/g, '')
|
|
|
|
.replace(/-+$/g, '');
|
|
|
|
};
|
|
|
|
|
2022-12-27 03:54:51 +00:00
|
|
|
export const underscorizeString = (inputString: string) => {
|
|
|
|
return slugifyString(inputString).replace(/-/g, '_');
|
|
|
|
};
|
|
|
|
|
2022-10-12 14:21:49 +00:00
|
|
|
export const capitalizeFirstLetter = (string: any) => {
|
|
|
|
return string.charAt(0).toUpperCase() + string.slice(1);
|
|
|
|
};
|
|
|
|
|
2022-11-02 16:42:49 +00:00
|
|
|
export const convertDateToSeconds = (
|
|
|
|
date: any,
|
|
|
|
onChangeFunction: any = null
|
|
|
|
) => {
|
2022-10-12 14:21:49 +00:00
|
|
|
let dateInSeconds = date;
|
|
|
|
if (date !== null) {
|
|
|
|
let dateInMilliseconds = date;
|
|
|
|
if (typeof date.getTime === 'function') {
|
|
|
|
dateInMilliseconds = date.getTime();
|
|
|
|
}
|
|
|
|
dateInSeconds = Math.floor(dateInMilliseconds / 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (onChangeFunction) {
|
|
|
|
onChangeFunction(dateInSeconds);
|
|
|
|
} else {
|
|
|
|
return dateInSeconds;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
2022-11-17 20:03:11 +00:00
|
|
|
export const convertDateObjectToFormattedString = (dateObject: Date) => {
|
|
|
|
if (dateObject) {
|
|
|
|
return format(dateObject, DATE_FORMAT);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
2022-11-17 21:05:54 +00:00
|
|
|
export const convertDateAndTimeStringsToDate = (
|
|
|
|
dateString: string,
|
|
|
|
timeString: string
|
|
|
|
) => {
|
|
|
|
if (dateString && timeString) {
|
|
|
|
return new Date(`${dateString}T${timeString}`);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const convertDateAndTimeStringsToSeconds = (
|
|
|
|
dateString: string,
|
|
|
|
timeString: string
|
|
|
|
) => {
|
|
|
|
const dateObject = convertDateAndTimeStringsToDate(dateString, timeString);
|
|
|
|
if (dateObject) {
|
|
|
|
return convertDateToSeconds(dateObject);
|
2022-11-02 16:42:49 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
2022-11-17 21:05:54 +00:00
|
|
|
export const convertStringToDate = (dateString: string) => {
|
|
|
|
return convertDateAndTimeStringsToSeconds(dateString, '00:10:00');
|
|
|
|
};
|
|
|
|
|
2022-11-17 20:03:11 +00:00
|
|
|
export const convertSecondsToDateObject = (seconds: number) => {
|
2022-11-10 20:44:58 +00:00
|
|
|
if (seconds) {
|
2022-11-17 20:03:11 +00:00
|
|
|
return new Date(seconds * 1000);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const convertSecondsToFormattedDateTime = (seconds: number) => {
|
|
|
|
const dateObject = convertSecondsToDateObject(seconds);
|
|
|
|
if (dateObject) {
|
2022-11-10 20:44:58 +00:00
|
|
|
return format(dateObject, DATE_TIME_FORMAT);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
2022-11-17 21:05:54 +00:00
|
|
|
export const convertDateObjectToFormattedHoursMinutes = (dateObject: Date) => {
|
|
|
|
if (dateObject) {
|
|
|
|
return format(dateObject, TIME_FORMAT_HOURS_MINUTES);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const convertSecondsToFormattedTimeHoursMinutes = (seconds: number) => {
|
|
|
|
const dateObject = convertSecondsToDateObject(seconds);
|
|
|
|
if (dateObject) {
|
|
|
|
return convertDateObjectToFormattedHoursMinutes(dateObject);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
2022-11-17 20:03:11 +00:00
|
|
|
export const convertSecondsToFormattedDateString = (seconds: number) => {
|
|
|
|
const dateObject = convertSecondsToDateObject(seconds);
|
|
|
|
if (dateObject) {
|
|
|
|
return convertDateObjectToFormattedString(dateObject);
|
2022-10-12 14:21:49 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
2022-11-02 16:42:49 +00:00
|
|
|
export const convertDateStringToSeconds = (dateString: string) => {
|
|
|
|
const dateObject = convertStringToDate(dateString);
|
|
|
|
return convertDateToSeconds(dateObject);
|
|
|
|
};
|
|
|
|
|
2022-10-12 14:21:49 +00:00
|
|
|
export const objectIsEmpty = (obj: object) => {
|
|
|
|
return Object.keys(obj).length === 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getPageInfoFromSearchParams = (
|
|
|
|
searchParams: any,
|
|
|
|
defaultPerPage: string | number = DEFAULT_PER_PAGE,
|
2022-11-14 21:29:04 +00:00
|
|
|
defaultPage: string | number = DEFAULT_PAGE,
|
|
|
|
paginationQueryParamPrefix: string | null = null
|
2022-10-12 14:21:49 +00:00
|
|
|
) => {
|
2022-11-14 21:29:04 +00:00
|
|
|
const paginationQueryParamPrefixToUse = paginationQueryParamPrefix
|
|
|
|
? `${paginationQueryParamPrefix}_`
|
|
|
|
: '';
|
|
|
|
const page = parseInt(
|
|
|
|
searchParams.get(`${paginationQueryParamPrefixToUse}page`) ||
|
|
|
|
defaultPage.toString(),
|
|
|
|
10
|
|
|
|
);
|
2022-10-12 14:21:49 +00:00
|
|
|
const perPage = parseInt(
|
2022-11-14 21:29:04 +00:00
|
|
|
searchParams.get(`${paginationQueryParamPrefixToUse}per_page`) ||
|
|
|
|
defaultPerPage.toString(),
|
2022-10-12 14:21:49 +00:00
|
|
|
10
|
|
|
|
);
|
|
|
|
|
|
|
|
return { page, perPage };
|
|
|
|
};
|
|
|
|
|
|
|
|
// https://stackoverflow.com/a/1349426/6090676
|
|
|
|
export const makeid = (length: number) => {
|
|
|
|
let result = '';
|
|
|
|
const characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
|
|
|
|
const charactersLength = characters.length;
|
|
|
|
for (let i = 0; i < length; i += 1) {
|
|
|
|
result += characters.charAt(Math.floor(Math.random() * charactersLength));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getProcessModelFullIdentifierFromSearchParams = (
|
|
|
|
searchParams: any
|
|
|
|
) => {
|
|
|
|
let processModelFullIdentifier = null;
|
2022-11-09 15:12:13 +00:00
|
|
|
if (searchParams.get('process_model_identifier')) {
|
2022-10-12 14:21:49 +00:00
|
|
|
processModelFullIdentifier = `${searchParams.get(
|
2022-11-09 15:12:13 +00:00
|
|
|
'process_model_identifier'
|
|
|
|
)}`;
|
2022-10-12 14:21:49 +00:00
|
|
|
}
|
|
|
|
return processModelFullIdentifier;
|
|
|
|
};
|
2022-11-01 20:26:24 +00:00
|
|
|
|
|
|
|
// https://stackoverflow.com/a/71352046/6090676
|
|
|
|
export const truncateString = (text: string, len: number) => {
|
|
|
|
if (text.length > len && text.length > 0) {
|
2022-11-22 14:05:33 +00:00
|
|
|
return `${text.split('').slice(0, len).join('')} ...`;
|
2022-11-01 20:26:24 +00:00
|
|
|
}
|
|
|
|
return text;
|
|
|
|
};
|
2022-11-07 22:22:46 +00:00
|
|
|
|
|
|
|
// Because of limitations in the way openapi defines parameters, we have to modify process models ids
|
|
|
|
// which are basically paths to the models
|
2022-11-22 15:56:40 +00:00
|
|
|
export const modifyProcessIdentifierForPathParam = (path: string) => {
|
2022-11-09 20:45:45 +00:00
|
|
|
return path.replace(/\//g, ':') || '';
|
2022-11-07 22:22:46 +00:00
|
|
|
};
|
|
|
|
|
2022-11-22 15:56:40 +00:00
|
|
|
export const unModifyProcessIdentifierForPathParam = (path: string) => {
|
2022-11-09 20:45:45 +00:00
|
|
|
return path.replace(/:/g, '/') || '';
|
2022-11-07 22:22:46 +00:00
|
|
|
};
|
|
|
|
|
2022-11-09 15:12:13 +00:00
|
|
|
export const getGroupFromModifiedModelId = (modifiedId: string) => {
|
2022-11-07 22:22:46 +00:00
|
|
|
const finalSplitIndex = modifiedId.lastIndexOf(':');
|
2022-11-08 21:00:44 +00:00
|
|
|
return modifiedId.slice(0, finalSplitIndex);
|
|
|
|
};
|
|
|
|
|
2022-11-09 15:12:13 +00:00
|
|
|
export const splitProcessModelId = (processModelId: string) => {
|
2022-11-08 21:00:44 +00:00
|
|
|
return processModelId.split('/');
|
2022-11-07 22:22:46 +00:00
|
|
|
};
|
2022-11-18 03:52:26 +00:00
|
|
|
|
|
|
|
export const refreshAtInterval = (
|
|
|
|
interval: number,
|
|
|
|
timeout: number,
|
|
|
|
func: Function
|
|
|
|
) => {
|
|
|
|
const intervalRef = setInterval(() => func(), interval * 1000);
|
|
|
|
const timeoutRef = setTimeout(
|
|
|
|
() => clearInterval(intervalRef),
|
|
|
|
timeout * 1000
|
|
|
|
);
|
2022-12-13 19:16:28 +00:00
|
|
|
return () => {
|
|
|
|
clearInterval(intervalRef);
|
|
|
|
clearTimeout(timeoutRef);
|
|
|
|
};
|
2022-11-18 03:52:26 +00:00
|
|
|
};
|
2022-12-15 19:55:06 +00:00
|
|
|
|
2023-01-06 17:00:24 +00:00
|
|
|
// bpmn:SubProcess shape elements do not have children
|
|
|
|
// but their moddle elements / businessOjects have flowElements
|
|
|
|
// that can include the moddleElement of the subprocesses
|
|
|
|
const getChildProcessesFromModdleElement = (bpmnModdleElement: any) => {
|
|
|
|
let elements: string[] = [];
|
|
|
|
bpmnModdleElement.flowElements.forEach((c: any) => {
|
|
|
|
if (c.$type === 'bpmn:SubProcess') {
|
|
|
|
elements.push(c.id);
|
|
|
|
elements = [...elements, ...getChildProcessesFromModdleElement(c)];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return elements;
|
|
|
|
};
|
|
|
|
|
2022-12-15 19:55:06 +00:00
|
|
|
const getChildProcesses = (bpmnElement: any) => {
|
|
|
|
let elements: string[] = [];
|
|
|
|
bpmnElement.children.forEach((c: any) => {
|
|
|
|
if (c.type === 'bpmn:Participant') {
|
|
|
|
if (c.businessObject.processRef) {
|
|
|
|
elements.push(c.businessObject.processRef.id);
|
|
|
|
}
|
|
|
|
elements = [...elements, ...getChildProcesses(c)];
|
|
|
|
} else if (c.type === 'bpmn:SubProcess') {
|
|
|
|
elements.push(c.id);
|
2023-01-06 17:00:24 +00:00
|
|
|
elements = [
|
|
|
|
...elements,
|
|
|
|
...getChildProcessesFromModdleElement(c.businessObject),
|
|
|
|
];
|
2022-12-15 19:55:06 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return elements;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getBpmnProcessIdentifiers = (rootBpmnElement: any) => {
|
|
|
|
const childProcesses = getChildProcesses(rootBpmnElement);
|
|
|
|
childProcesses.push(rootBpmnElement.businessObject.id);
|
|
|
|
return childProcesses;
|
|
|
|
};
|
2022-12-28 17:27:37 +00:00
|
|
|
|
2023-01-05 19:59:59 +00:00
|
|
|
export const isInteger = (str: string | number) => {
|
|
|
|
return /^\d+$/.test(str.toString());
|
|
|
|
};
|
2023-03-08 21:57:12 +00:00
|
|
|
|
|
|
|
export const encodeBase64 = (data: string) => {
|
|
|
|
return Buffer.from(data).toString('base64');
|
|
|
|
};
|
|
|
|
|
|
|
|
export const decodeBase64 = (data: string) => {
|
|
|
|
return Buffer.from(data, 'base64').toString('ascii');
|
|
|
|
};
|
2023-03-28 17:41:58 +00:00
|
|
|
|
|
|
|
const MINUTES_IN_HOUR = 60;
|
|
|
|
const SECONDS_IN_MINUTE = 60;
|
|
|
|
const SECONDS_IN_HOUR = MINUTES_IN_HOUR * SECONDS_IN_MINUTE;
|
|
|
|
const FOUR_HOURS_IN_SECONDS = SECONDS_IN_HOUR * 4;
|
|
|
|
|
|
|
|
export const REFRESH_INTERVAL_SECONDS = 5;
|
|
|
|
export const REFRESH_TIMEOUT_SECONDS = FOUR_HOURS_IN_SECONDS;
|