2024-04-16 19:06:47 +08:00
|
|
|
import isObject from 'lodash/isObject';
|
|
|
|
|
import snakeCase from 'lodash/snakeCase';
|
|
|
|
|
|
|
|
|
|
export const isFormData = (data: unknown): data is FormData => {
|
|
|
|
|
return data instanceof FormData;
|
|
|
|
|
};
|
|
|
|
|
|
2024-04-23 17:46:56 +08:00
|
|
|
const excludedFields = ['img2txt_id'];
|
|
|
|
|
|
|
|
|
|
const isExcludedField = (key: string) => {
|
|
|
|
|
return excludedFields.includes(key);
|
|
|
|
|
};
|
|
|
|
|
|
2024-04-16 19:06:47 +08:00
|
|
|
export const convertTheKeysOfTheObjectToSnake = (data: unknown) => {
|
|
|
|
|
if (isObject(data) && !isFormData(data)) {
|
|
|
|
|
return Object.keys(data).reduce<Record<string, any>>((pre, cur) => {
|
|
|
|
|
const value = (data as Record<string, any>)[cur];
|
2024-04-23 17:46:56 +08:00
|
|
|
pre[isFormData(value) || isExcludedField(cur) ? cur : snakeCase(cur)] =
|
|
|
|
|
value;
|
2024-04-16 19:06:47 +08:00
|
|
|
return pre;
|
|
|
|
|
}, {});
|
|
|
|
|
}
|
|
|
|
|
return data;
|
|
|
|
|
};
|
2024-04-18 19:27:53 +08:00
|
|
|
|
|
|
|
|
export const getSearchValue = (key: string) => {
|
|
|
|
|
const params = new URL(document.location as any).searchParams;
|
|
|
|
|
return params.get(key);
|
|
|
|
|
};
|
2024-04-26 17:22:23 +08:00
|
|
|
|
|
|
|
|
// Formatize numbers, add thousands of separators
|
|
|
|
|
export const formatNumberWithThousandsSeparator = (numberStr: string) => {
|
|
|
|
|
const formattedNumber = numberStr.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
|
|
|
|
|
return formattedNumber;
|
|
|
|
|
};
|