2025-03-08 07:11:57 +00:00
|
|
|
import { UpdateField } from '@components/options/ToolOptions';
|
|
|
|
|
|
2024-06-25 09:35:44 +01:00
|
|
|
export function capitalizeFirstLetter(string: string | undefined) {
|
|
|
|
|
if (!string) return '';
|
2024-06-23 01:26:04 +01:00
|
|
|
return string.charAt(0).toUpperCase() + string.slice(1);
|
|
|
|
|
}
|
2024-06-27 16:51:40 +00:00
|
|
|
|
2025-03-24 20:23:28 +00:00
|
|
|
export function isNumber(number: any): boolean {
|
2024-06-27 16:51:40 +00:00
|
|
|
return !isNaN(parseFloat(number)) && isFinite(number);
|
2024-06-27 18:55:20 +01:00
|
|
|
}
|
2024-07-09 21:58:08 +01:00
|
|
|
|
2025-03-08 07:11:57 +00:00
|
|
|
export const updateNumberField = <T>(
|
|
|
|
|
val: string,
|
|
|
|
|
key: keyof T,
|
|
|
|
|
updateField: UpdateField<T>
|
|
|
|
|
) => {
|
|
|
|
|
if (val === '') {
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
updateField(key, '');
|
|
|
|
|
} else if (isNumber(val)) {
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
updateField(key, Number(val));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-09 21:58:08 +01:00
|
|
|
export const replaceSpecialCharacters = (str: string) => {
|
|
|
|
|
return str
|
2025-02-27 13:05:38 +00:00
|
|
|
.replace(/\\"/g, '"')
|
2024-07-09 21:58:08 +01:00
|
|
|
.replace(/\\n/g, '\n')
|
|
|
|
|
.replace(/\\t/g, '\t')
|
|
|
|
|
.replace(/\\r/g, '\r')
|
|
|
|
|
.replace(/\\b/g, '\b')
|
|
|
|
|
.replace(/\\f/g, '\f')
|
|
|
|
|
.replace(/\\v/g, '\v');
|
|
|
|
|
};
|
2024-07-10 14:33:20 +00:00
|
|
|
|
|
|
|
|
export function reverseString(input: string): string {
|
|
|
|
|
return input.split('').reverse().join('');
|
|
|
|
|
}
|
2025-03-24 20:23:28 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Checks if the input string contains only digits.
|
|
|
|
|
* @param input - The string to validate.
|
2025-03-26 17:39:51 +00:00
|
|
|
* @returns True if the input contains only digits including float, false otherwise.
|
2025-03-24 20:23:28 +00:00
|
|
|
*/
|
|
|
|
|
export function containsOnlyDigits(input: string): boolean {
|
2025-03-26 17:39:51 +00:00
|
|
|
return /^\d+(\.\d+)?$/.test(input.trim());
|
2025-03-24 20:23:28 +00:00
|
|
|
}
|
2025-04-05 03:50:55 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* unquote a string if properly quoted.
|
|
|
|
|
* @param value - The string to unquote.
|
|
|
|
|
* @param quoteCharacter - The character used for quoting (e.g., '"', "'").
|
|
|
|
|
* @returns The unquoted string if it was quoted, otherwise the original string.
|
|
|
|
|
*/
|
|
|
|
|
export function unquoteIfQuoted(value: string, quoteCharacter: string): string {
|
2025-04-06 01:45:56 +02:00
|
|
|
if (
|
|
|
|
|
quoteCharacter &&
|
|
|
|
|
value.startsWith(quoteCharacter) &&
|
|
|
|
|
value.endsWith(quoteCharacter)
|
|
|
|
|
) {
|
2025-04-05 03:50:55 +02:00
|
|
|
return value.slice(1, -1); // Remove first and last character
|
|
|
|
|
}
|
|
|
|
|
return value;
|
|
|
|
|
}
|