PluginProbe
Hostinger Tools / 3.0.46
Hostinger Tools v3.0.46
3.0.77 3.0.76 3.0.75 3.0.74 3.0.73 3.0.72 3.0.71 3.0.70 3.0.69 3.0.68 3.0.67 3.0.66 1.8.1 1.8.2 1.8.3 1.9.1 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0.0 2.0.1 2.0.4 All 108 releases
hostinger / vue-frontend / src / utils / helpers / helpers.ts

helpers.ts in Hostinger Tools 3.0.46, at vue-frontend/src/utils/helpers/helpers.ts

164 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { toUnicode as punyUnicode } from "punycode";
2 import { AxiosResponse } from "axios";
3 import { toast } from "vue3-toastify";
4 import { ResponseError, BaseResponse } from "@/types";
5 import { useGeneralStoreData } from "@/stores";
6
7 /**
8 * Converts a string to Unicode using Punycode encoding.
9 * If the input string is falsy, it returns the input string itself.
10 *
11 * @param str - The string to convert to Unicode.
12 * @returns The Unicode representation of the input string, or the input string itself if it is falsy.
13 */
14 export const toUnicode = (str: string) => (str ? punyUnicode(str) : str);
15
16 /**
17 * Converts the first character of a word to uppercase and returns the modified word.
18 * If the word is an empty string, an empty string is returned.
19 *
20 * @param word - The word to convert to title case.
21 * @returns The word with the first character in uppercase.
22 */
23 export const toTitleCase = (word: string = "") =>
24 word.charAt(0).toUpperCase() + word.slice(1);
25
26 /**
27 * Delays the execution for the specified number of milliseconds.
28 * @param ms - The number of milliseconds to delay the execution.
29 * @returns A Promise that resolves after the specified delay.
30 */
31 export const timeout = (ms: number) =>
32 new Promise((resolve) => setTimeout(resolve, ms));
33
34 /**
35 * Copies the given string to the clipboard.
36 *
37 * @param copiedString - The string to be copied.
38 * @param message - The success message to be displayed.
39 * @param toastrParams - Additional parameters for the toast notification.
40 */
41 export const copyString = (
42 copiedString: string,
43 message = "Copied successfully",
44 toastrParams = {}
45 ) => {
46 const el = document.createElement("textarea");
47 el.value = copiedString;
48
49 el.setAttribute("readonly", "");
50 document.body.appendChild(el);
51 el.select();
52
53 document.execCommand("copy");
54 document.body.removeChild(el);
55
56 const copyString = "Text has been copied successfully";
57
58 if (!message) return;
59 toast.success(copyString, toastrParams);
60 };
61 /**
62 * Wraps the given value in a CSS variable.
63 * @param value - The value to be wrapped.
64 * @returns The wrapped value as a CSS variable.
65 */
66 export const wrapInCssVar = (value: string | number) => `var(--${value})`;
67 /**
68 * Capitalizes the first letter of a string.
69 * @param str - The input string.
70 * @returns The input string with the first letter capitalized.
71 */
72 export const capitalize = (str: string) =>
73 str.charAt(0).toUpperCase() + str.substring(1);
74
75 // eslint-disable-next-line func-style
76 /**
77 * Calls an asynchronous function and handles the response.
78 * @param promise - The promise to be resolved.
79 * @returns A tuple containing the response data and any error that occurred.
80 */
81 export async function asyncCall<T>(
82 promise: Promise<AxiosResponse<T>>
83 ): BaseResponse<T> {
84 try {
85 const response: any = await promise;
86
87 if (
88 !response.error ||
89 (Array.isArray(response.error) && !response.error.length)
90 ) {
91 return [response.data.data, null];
92 }
93
94 return [{} as any, response];
95 } catch (er) {
96 return [{} as any, er as ResponseError];
97 }
98 }
99 /**
100 * Retrieves the URL of an asset based on the provided path.
101 * @param path - The path of the asset.
102 * @returns The URL of the asset.
103 */
104 export const getAssetSource = (path: string) => {
105 const { assetUrl } = useGeneralStoreData();
106 // @ts-ignore
107 return `${assetUrl}vue-frontend/src/assets/${path}`;
108 };
109
110 /**
111 * Converts a kebab-case string to camelCase.
112 * @param string - The kebab-case string to convert.
113 * @returns The camelCase version of the input string.
114 */
115 export const kebabToCamel = (string: string) => {
116 return string.replace(/-([a-z])/g, (g) => g[1].toUpperCase());
117 };
118
119 /**
120 * Compares two version numbers and returns a comparison result.
121 * @param newVersion - The old version number.
122 * @param currentVersion - The new version number.
123 * @returns -1 if currentVersion is greater than newVersion, 1 if currentVersion is less than newVersion, 0 if they are equal.
124 */
125 export const isNewerVerison = ({
126 newVersion,
127 currentVersion,
128 }: {
129 newVersion: string;
130 currentVersion: string;
131 }) => {
132 if (!newVersion || !currentVersion) return false;
133
134 const newVersionParts = newVersion.split(".");
135 const currentVersionParts = currentVersion.split(".");
136 for (
137 let i = 0;
138 i < Math.max(currentVersionParts.length, newVersionParts.length);
139 i++
140 ) {
141 const newPart = parseInt(currentVersionParts[i]) || 0;
142 const oldPart = parseInt(newVersionParts[i]) || 0;
143 if (newPart > oldPart) return false;
144 if (newPart < oldPart) return true;
145 }
146
147 return false;
148 };
149
150 /**
151 * Returns the base URL of a given URL.
152 * @param url - The input URL.
153 * @returns The base URL of the input URL.
154 */
155 export const getBaseUrl = (url: string) => {
156 const parsedUrl = new URL(url);
157 return `${parsedUrl.protocol}//${parsedUrl.host}${parsedUrl.pathname.split("/").slice(0, -1).join("/")}/`;
158 };
159
160 export const translate = (key: string) => {
161 // @ts-ignore
162 return hostinger_tools_data.translations[key] || key;
163 };
164