2022-10-12 14:21:49 +00:00
|
|
|
import { format } from 'date-fns';
|
2022-11-10 20:44:58 +00:00
|
|
|
import { DATE_TIME_FORMAT, DATE_FORMAT } 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, '');
|
|
|
|
};
|
|
|
|
|
|
|
|
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-02 16:42:49 +00:00
|
|
|
export const convertStringToDate = (dateString: string) => {
|
|
|
|
if (dateString) {
|
2022-11-09 19:51:22 +00:00
|
|
|
// add midnight time to the date so it c uses the correct date
|
|
|
|
// after converting to timezone
|
|
|
|
return new Date(`${dateString}T00:10:00`);
|
2022-11-02 16:42:49 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
2022-11-10 20:44:58 +00:00
|
|
|
export const convertSecondsToFormattedDateTime = (seconds: number) => {
|
|
|
|
if (seconds) {
|
|
|
|
const dateObject = new Date(seconds * 1000);
|
|
|
|
return format(dateObject, DATE_TIME_FORMAT);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
2022-10-12 14:21:49 +00:00
|
|
|
export const convertSecondsToFormattedDate = (seconds: number) => {
|
|
|
|
if (seconds) {
|
2022-11-02 16:42:49 +00:00
|
|
|
const dateObject = new Date(seconds * 1000);
|
|
|
|
return format(dateObject, DATE_FORMAT);
|
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,
|
|
|
|
defaultPage: string | number = DEFAULT_PAGE
|
|
|
|
) => {
|
|
|
|
const page = parseInt(searchParams.get('page') || defaultPage.toString(), 10);
|
|
|
|
const perPage = parseInt(
|
|
|
|
searchParams.get('per_page') || defaultPerPage.toString(),
|
|
|
|
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) {
|
|
|
|
return `${text.split(' ').slice(0, len).join(' ')} ...`;
|
|
|
|
}
|
|
|
|
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-09 15:12:13 +00:00
|
|
|
export const modifyProcessModelPath = (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 unModifyProcessModelPath = (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
|
|
|
};
|