@creopse/utils 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/dist/enums/account-status.d.ts +4 -0
- package/dist/enums/account-status.js +5 -0
- package/dist/enums/ad-content-type.d.ts +4 -0
- package/dist/enums/ad-content-type.js +5 -0
- package/dist/enums/auth-type.d.ts +8 -0
- package/dist/enums/auth-type.js +9 -0
- package/dist/enums/bool.d.ts +4 -0
- package/dist/enums/bool.js +5 -0
- package/dist/enums/content-type.d.ts +6 -0
- package/dist/enums/content-type.js +7 -0
- package/dist/enums/index.d.ts +16 -0
- package/dist/enums/index.js +16 -0
- package/dist/enums/media-file-type.d.ts +7 -0
- package/dist/enums/media-file-type.js +8 -0
- package/dist/enums/menu-item-target-type.d.ts +5 -0
- package/dist/enums/menu-item-target-type.js +6 -0
- package/dist/enums/news-article-status.d.ts +6 -0
- package/dist/enums/news-article-status.js +7 -0
- package/dist/enums/notification-source.d.ts +4 -0
- package/dist/enums/notification-source.js +5 -0
- package/dist/enums/notification-type.d.ts +3 -0
- package/dist/enums/notification-type.js +4 -0
- package/dist/enums/permalink-content-type.d.ts +6 -0
- package/dist/enums/permalink-content-type.js +7 -0
- package/dist/enums/profile-type.d.ts +3 -0
- package/dist/enums/profile-type.js +4 -0
- package/dist/enums/response-error-code.d.ts +18 -0
- package/dist/enums/response-error-code.js +20 -0
- package/dist/enums/user-role.d.ts +5 -0
- package/dist/enums/user-role.js +6 -0
- package/dist/enums/video-display-type.d.ts +4 -0
- package/dist/enums/video-display-type.js +5 -0
- package/dist/enums/video-item-source.d.ts +4 -0
- package/dist/enums/video-item-source.js +5 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/models/ad-identifier.d.ts +14 -0
- package/dist/models/ad-identifier.js +11 -0
- package/dist/models/ad.d.ts +21 -0
- package/dist/models/ad.js +16 -0
- package/dist/models/admin-profile.d.ts +7 -0
- package/dist/models/admin-profile.js +8 -0
- package/dist/models/app-information.d.ts +8 -0
- package/dist/models/app-information.js +9 -0
- package/dist/models/app-setting.d.ts +8 -0
- package/dist/models/app-setting.js +9 -0
- package/dist/models/content-model-item.d.ts +17 -0
- package/dist/models/content-model-item.js +12 -0
- package/dist/models/content-model.d.ts +18 -0
- package/dist/models/content-model.js +16 -0
- package/dist/models/index.d.ts +30 -0
- package/dist/models/index.js +30 -0
- package/dist/models/media-file.d.ts +23 -0
- package/dist/models/media-file.js +22 -0
- package/dist/models/menu-item-group.d.ts +8 -0
- package/dist/models/menu-item-group.js +9 -0
- package/dist/models/menu-item.d.ts +39 -0
- package/dist/models/menu-item.js +25 -0
- package/dist/models/menu-location.d.ts +8 -0
- package/dist/models/menu-location.js +9 -0
- package/dist/models/menu.d.ts +15 -0
- package/dist/models/menu.js +11 -0
- package/dist/models/news-article.d.ts +35 -0
- package/dist/models/news-article.js +23 -0
- package/dist/models/news-category.d.ts +20 -0
- package/dist/models/news-category.js +17 -0
- package/dist/models/news-comment.d.ts +18 -0
- package/dist/models/news-comment.js +15 -0
- package/dist/models/news-tag.d.ts +15 -0
- package/dist/models/news-tag.js +13 -0
- package/dist/models/newsletter-campaign.d.ts +17 -0
- package/dist/models/newsletter-campaign.js +13 -0
- package/dist/models/newsletter-email.d.ts +7 -0
- package/dist/models/newsletter-email.js +8 -0
- package/dist/models/newsletter-phone.d.ts +7 -0
- package/dist/models/newsletter-phone.js +8 -0
- package/dist/models/notification.d.ts +22 -0
- package/dist/models/notification.js +12 -0
- package/dist/models/page.d.ts +15 -0
- package/dist/models/page.js +15 -0
- package/dist/models/permalink.d.ts +14 -0
- package/dist/models/permalink.js +12 -0
- package/dist/models/permission.d.ts +9 -0
- package/dist/models/permission.js +10 -0
- package/dist/models/role.d.ts +11 -0
- package/dist/models/role.js +11 -0
- package/dist/models/section.d.ts +17 -0
- package/dist/models/section.js +12 -0
- package/dist/models/subscriber-profile.d.ts +7 -0
- package/dist/models/subscriber-profile.js +8 -0
- package/dist/models/user-session.d.ts +27 -0
- package/dist/models/user-session.js +12 -0
- package/dist/models/user.d.ts +36 -0
- package/dist/models/user.js +27 -0
- package/dist/models/video-item.d.ts +23 -0
- package/dist/models/video-item.js +22 -0
- package/dist/models/video-setting.d.ts +8 -0
- package/dist/models/video-setting.js +9 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/dist/types/app-information.d.ts +41 -0
- package/dist/types/app-information.js +1 -0
- package/dist/types/common.d.ts +11 -0
- package/dist/types/common.js +1 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/types/index.js +4 -0
- package/dist/types/pagination.d.ts +26 -0
- package/dist/types/pagination.js +1 -0
- package/dist/types/props.d.ts +42 -0
- package/dist/types/props.js +1 -0
- package/dist/utils/chronos.d.ts +97 -0
- package/dist/utils/chronos.js +97 -0
- package/dist/utils/functions.d.ts +312 -0
- package/dist/utils/functions.js +574 -0
- package/dist/utils/index.d.ts +2 -0
- package/dist/utils/index.js +2 -0
- package/package.json +48 -0
|
@@ -0,0 +1,574 @@
|
|
|
1
|
+
import imageCompression from 'browser-image-compression';
|
|
2
|
+
import _ from 'lodash';
|
|
3
|
+
/**
|
|
4
|
+
* Generates the initials of a given name.
|
|
5
|
+
*
|
|
6
|
+
* @param {string} name - The name to generate the initials from.
|
|
7
|
+
* @return {string} The initials of the given name.
|
|
8
|
+
*/
|
|
9
|
+
export function getNameInitials(name) {
|
|
10
|
+
const nameSplit = name.split(' '), initials = nameSplit.length > 1
|
|
11
|
+
? nameSplit[0].charAt(0).toUpperCase() +
|
|
12
|
+
nameSplit[1].charAt(0).toUpperCase()
|
|
13
|
+
: nameSplit[0].charAt(0).toUpperCase();
|
|
14
|
+
return initials;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Generates a random integer between the provided minimum and maximum values (inclusive).
|
|
18
|
+
*
|
|
19
|
+
* @param {number} min - The minimum value for the random integer.
|
|
20
|
+
* @param {number} max - The maximum value for the random integer.
|
|
21
|
+
* @return {number} The randomly generated integer.
|
|
22
|
+
*/
|
|
23
|
+
export function getRandomInteger(min, max) {
|
|
24
|
+
return Math.floor(Math.random() * (max - min + 1)) + min;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Removes the trailing slash from a given path.
|
|
28
|
+
*
|
|
29
|
+
* @param {string} path - The path to remove the trailing slash from.
|
|
30
|
+
* @return {string} The path without the trailing slash.
|
|
31
|
+
*/
|
|
32
|
+
export function removeTrailingSlash(path) {
|
|
33
|
+
if (path.length > 1 && path.charAt(path.length - 1) === '/') {
|
|
34
|
+
return path.substring(0, path.length - 1);
|
|
35
|
+
}
|
|
36
|
+
return path;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Removes the leading slash from a path if it starts with a slash.
|
|
40
|
+
*
|
|
41
|
+
* @param {string} path - The path from which to remove the leading slash.
|
|
42
|
+
* @return {string} The path without the leading slash.
|
|
43
|
+
*/
|
|
44
|
+
export function removeLeadingSlash(path) {
|
|
45
|
+
if (path.startsWith('/')) {
|
|
46
|
+
return path.slice(1);
|
|
47
|
+
}
|
|
48
|
+
return path;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Calculates the distance between two points on the Earth's surface using the Haversine formula.
|
|
52
|
+
*
|
|
53
|
+
* @param {number} lat1 - The latitude of the first point.
|
|
54
|
+
* @param {number} lon1 - The longitude of the first point.
|
|
55
|
+
* @param {number} lat2 - The latitude of the second point.
|
|
56
|
+
* @param {number} lon2 - The longitude of the second point.
|
|
57
|
+
* @return {number} The distance between the two points in kilometers.
|
|
58
|
+
*/
|
|
59
|
+
export function getDistanceFromLatLonInKm(lat1, lon1, lat2, lon2) {
|
|
60
|
+
var R = 6371;
|
|
61
|
+
var dLat = deg2rad(lat2 - lat1);
|
|
62
|
+
var dLon = deg2rad(lon2 - lon1);
|
|
63
|
+
var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
|
|
64
|
+
Math.cos(deg2rad(lat1)) *
|
|
65
|
+
Math.cos(deg2rad(lat2)) *
|
|
66
|
+
Math.sin(dLon / 2) *
|
|
67
|
+
Math.sin(dLon / 2);
|
|
68
|
+
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
|
|
69
|
+
var d = R * c;
|
|
70
|
+
return d;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Converts degrees to radians.
|
|
74
|
+
*
|
|
75
|
+
* @param {number} deg - The angle in degrees.
|
|
76
|
+
* @returns {number} The angle in radians.
|
|
77
|
+
*/
|
|
78
|
+
export function deg2rad(deg) {
|
|
79
|
+
return deg * (Math.PI / 180);
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Checks if the given path is an external URL, email address, or phone number.
|
|
83
|
+
*
|
|
84
|
+
* @param {string} path - The path to check.
|
|
85
|
+
* @return {boolean} Returns true if the path is external, false otherwise.
|
|
86
|
+
*/
|
|
87
|
+
export function isExternal(path) {
|
|
88
|
+
return /^(https?:|mailto:|tel:)/.test(path);
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Formats the given duration in seconds into a string representation of time.
|
|
92
|
+
*
|
|
93
|
+
* @param {number} duration - The duration in seconds to be formatted.
|
|
94
|
+
* @param {Object} options - Optional parameters for formatting.
|
|
95
|
+
* @param {string} options.separator - The separator used between hours, minutes, and seconds. Defaults to ':'.
|
|
96
|
+
* @return {string} - The formatted time string.
|
|
97
|
+
*/
|
|
98
|
+
export function formatTimeFromSeconds(duration, { separator = ':' } = {}) {
|
|
99
|
+
var hrs = ~~(duration / 3600);
|
|
100
|
+
var mins = ~~((duration % 3600) / 60);
|
|
101
|
+
var secs = ~~duration % 60;
|
|
102
|
+
var ret = '';
|
|
103
|
+
if (hrs > 0) {
|
|
104
|
+
ret +=
|
|
105
|
+
'' + strPadLeft(String(hrs), '0', 2) + separator + (mins < 10 ? '00' : '');
|
|
106
|
+
}
|
|
107
|
+
ret +=
|
|
108
|
+
'' + strPadLeft(String(mins), '0', 2) + separator + (secs < 10 ? '00' : '');
|
|
109
|
+
ret += '' + strPadLeft(String(secs), '0', 2);
|
|
110
|
+
return ret;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Pads the left side of a string with a specified character until it reaches a specified length.
|
|
114
|
+
*
|
|
115
|
+
* @param {string} string - The string to be padded.
|
|
116
|
+
* @param {string} pad - The character used for padding.
|
|
117
|
+
* @param {number} length - The desired length of the resulting string.
|
|
118
|
+
* @return {string} The padded string.
|
|
119
|
+
*/
|
|
120
|
+
export function strPadLeft(string, pad, length) {
|
|
121
|
+
return (new Array(length + 1).join(pad) + string).slice(-length);
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Capitalizes the first letter of a given string.
|
|
125
|
+
*
|
|
126
|
+
* @param {string} string - The string to capitalize.
|
|
127
|
+
* @return {string} The modified string with the first letter capitalized.
|
|
128
|
+
*/
|
|
129
|
+
export function capitalizeFirstLetter(string) {
|
|
130
|
+
return string.charAt(0).toUpperCase() + string.slice(1);
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Generates a password with the specified length and character options.
|
|
134
|
+
*
|
|
135
|
+
* @param {number} length - The length of the password to be generated. Default is 10.
|
|
136
|
+
* @param {object} options - The character options for the password. Default is { letters: true, numbers: true, symbols: false }.
|
|
137
|
+
* @param {boolean} options.letters - Specifies whether to include letters in the password. Default is true.
|
|
138
|
+
* @param {boolean} options.numbers - Specifies whether to include numbers in the password. Default is true.
|
|
139
|
+
* @param {boolean} options.symbols - Specifies whether to include symbols in the password. Default is false.
|
|
140
|
+
* @return {string} The generated password.
|
|
141
|
+
*/
|
|
142
|
+
export function genPassword(length = 10, { letters = true, numbers = true, symbols = false } = {}) {
|
|
143
|
+
if (!letters && !numbers && !symbols)
|
|
144
|
+
return '';
|
|
145
|
+
let base = [], password = '';
|
|
146
|
+
const numbersBase = '0123456789'.split(''), symbolsBase = '!@#$%^&*()'.split(''), lettersBase = 'abcdefghijklmnopqrstuvwxyz'.split('');
|
|
147
|
+
if (letters)
|
|
148
|
+
base.push(...lettersBase);
|
|
149
|
+
if (numbers)
|
|
150
|
+
base.push(...numbersBase);
|
|
151
|
+
if (symbols)
|
|
152
|
+
base.push(...symbolsBase);
|
|
153
|
+
base = _.shuffle(base);
|
|
154
|
+
for (let i = 0; i < length; i++) {
|
|
155
|
+
const randomNumber = Math.floor(Math.random() * base.length);
|
|
156
|
+
const char = lettersBase.includes(base[randomNumber]) &&
|
|
157
|
+
Math.floor(Math.random() * 2) === 0
|
|
158
|
+
? base[randomNumber].toUpperCase()
|
|
159
|
+
: base[randomNumber];
|
|
160
|
+
password += char;
|
|
161
|
+
}
|
|
162
|
+
return password;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Extracts uppercase words from the given input string.
|
|
166
|
+
*
|
|
167
|
+
* @param {string} input - The input string to extract uppercase words from.
|
|
168
|
+
* @return {Array<string>} An array of uppercase words found in the input string.
|
|
169
|
+
*/
|
|
170
|
+
export function extractUppercaseWords(input) {
|
|
171
|
+
return input.match(/(\b[A-Z]['A-Z]+|\b[A-Z]\b)/g);
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Extracts lowercase words from the input string.
|
|
175
|
+
*
|
|
176
|
+
* @param {string} input - The string to extract lowercase words from.
|
|
177
|
+
* @return {string[]} An array of lowercase words extracted from the input string.
|
|
178
|
+
*/
|
|
179
|
+
export function extractLowercaseWords(input) {
|
|
180
|
+
return input.match(/(\b[a-z]['a-z]+|\b[a-z]\b)/g);
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Generates a random color code in hexadecimal format.
|
|
184
|
+
*
|
|
185
|
+
* @return {string} The randomly generated color code.
|
|
186
|
+
*/
|
|
187
|
+
export function genRandomColor() {
|
|
188
|
+
const letters = '0123456789ABCDEF';
|
|
189
|
+
let color = '#';
|
|
190
|
+
for (let i = 0; i < 6; i++) {
|
|
191
|
+
color += letters[Math.floor(Math.random() * 16)];
|
|
192
|
+
}
|
|
193
|
+
return color;
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Converts a data URL to a Blob object.
|
|
197
|
+
*
|
|
198
|
+
* @param {string} dataUrl - The data URL to be converted.
|
|
199
|
+
* @return {Blob} The resulting Blob object.
|
|
200
|
+
*/
|
|
201
|
+
export function dataUrlToBlob(dataUrl) {
|
|
202
|
+
const [header, body] = dataUrl.split(',');
|
|
203
|
+
const [type] = header.split(';');
|
|
204
|
+
const decodedBody = atob(body);
|
|
205
|
+
const byteArray = new Uint8Array(decodedBody.length);
|
|
206
|
+
for (let i = 0; i < decodedBody.length; i++) {
|
|
207
|
+
byteArray[i] = decodedBody.charCodeAt(i);
|
|
208
|
+
}
|
|
209
|
+
return new Blob([byteArray], { type });
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Converts a data URL to a File object.
|
|
213
|
+
*
|
|
214
|
+
* @param {string} dataUrl - The data URL to convert.
|
|
215
|
+
* @param {Object} options - Optional parameters.
|
|
216
|
+
* @param {string} options.fileName - The name of the file. Default is 'file.png'.
|
|
217
|
+
* @param {string} options.type - The MIME type of the file. Default is 'image/png'.
|
|
218
|
+
* @returns {File} The converted File object.
|
|
219
|
+
*/
|
|
220
|
+
export function dataUrlToFile(dataUrl, { fileName = 'file.png', type = 'image/png', } = {}) {
|
|
221
|
+
const blob = dataUrlToBlob(dataUrl);
|
|
222
|
+
return new File([blob], fileName, {
|
|
223
|
+
type: type || blob.type,
|
|
224
|
+
lastModified: new Date().getTime(),
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Creates a new array of pure JavaScript objects by making a shallow copy of each object in the input array.
|
|
229
|
+
*
|
|
230
|
+
* @param {Array<object>} array - The input array containing objects to be copied.
|
|
231
|
+
* @return {Array<object>} A new array containing the copied objects.
|
|
232
|
+
*/
|
|
233
|
+
export function toArrayOfPureJsObjects(array) {
|
|
234
|
+
return array.map((obj) => {
|
|
235
|
+
return Object.assign({}, obj);
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Converts a number of bytes to a human-readable file size.
|
|
240
|
+
*
|
|
241
|
+
* @param {number} bytes - The number of bytes to convert.
|
|
242
|
+
* @param {boolean} si - Whether to use the SI units (1000) or binary units (1024).
|
|
243
|
+
* @param {number} dp - The number of decimal places to round the result to.
|
|
244
|
+
* @return {string} The human-readable file size.
|
|
245
|
+
*/
|
|
246
|
+
export function humanFileSize(bytes, si = false, dp = 1) {
|
|
247
|
+
const thresh = si ? 1000 : 1024;
|
|
248
|
+
if (Math.abs(bytes) < thresh) {
|
|
249
|
+
return bytes + ' B';
|
|
250
|
+
}
|
|
251
|
+
const units = si
|
|
252
|
+
? ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
|
|
253
|
+
: ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
|
|
254
|
+
let u = -1;
|
|
255
|
+
const r = 10 ** dp;
|
|
256
|
+
do {
|
|
257
|
+
bytes /= thresh;
|
|
258
|
+
++u;
|
|
259
|
+
} while (Math.round(Math.abs(bytes) * r) / r >= thresh &&
|
|
260
|
+
u < units.length - 1);
|
|
261
|
+
return bytes.toFixed(dp) + ' ' + units[u];
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Returns the filename from a given URL.
|
|
265
|
+
*
|
|
266
|
+
* @param {string} url - The URL from which to extract the filename.
|
|
267
|
+
* @return {string} The filename extracted from the URL.
|
|
268
|
+
*/
|
|
269
|
+
export function filenameFromUrl(url) {
|
|
270
|
+
return url.substring(url.lastIndexOf('/') + 1);
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Downloads a file by URL.
|
|
274
|
+
*
|
|
275
|
+
* @param {string} url - The URL of the file to be downloaded.
|
|
276
|
+
*/
|
|
277
|
+
export function downloadByUrl(url) {
|
|
278
|
+
var element = document.createElement('a');
|
|
279
|
+
element.setAttribute('href', url);
|
|
280
|
+
element.style.display = 'none';
|
|
281
|
+
document.body.appendChild(element);
|
|
282
|
+
element.click();
|
|
283
|
+
document.body.removeChild(element);
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Downloads the given object as a JSON file with the specified name.
|
|
287
|
+
*
|
|
288
|
+
* @param {object} exportObj - The object to be exported as JSON.
|
|
289
|
+
* @param {string} exportName - The name of the exported file.
|
|
290
|
+
* @return {void} This function does not return a value.
|
|
291
|
+
*/
|
|
292
|
+
export function downloadObjectAsJson(exportObj, exportName) {
|
|
293
|
+
var dataStr = 'data:text/json;charset=utf-8,' +
|
|
294
|
+
encodeURIComponent(JSON.stringify(exportObj, null, 2));
|
|
295
|
+
var element = document.createElement('a');
|
|
296
|
+
element.setAttribute('href', dataStr);
|
|
297
|
+
element.setAttribute('download', exportName + '.json');
|
|
298
|
+
element.style.display = 'none';
|
|
299
|
+
document.body.appendChild(element);
|
|
300
|
+
element.click();
|
|
301
|
+
document.body.removeChild(element);
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* Parses an INI string and returns an object containing the key-value pairs.
|
|
305
|
+
*
|
|
306
|
+
* @param {string} iniString - The INI string to parse.
|
|
307
|
+
* @return {{ [key: string]: string }} - An object containing the key-value pairs parsed from the INI string.
|
|
308
|
+
*/
|
|
309
|
+
export function parseINIString(iniString) {
|
|
310
|
+
const lines = iniString.split('\n');
|
|
311
|
+
const result = {};
|
|
312
|
+
let currentSection = null;
|
|
313
|
+
for (const line of lines) {
|
|
314
|
+
// eslint-disable-next-line no-useless-escape
|
|
315
|
+
const section = line.match(/^\s*\[([^\[\]]+)\]\s*$/);
|
|
316
|
+
if (section) {
|
|
317
|
+
currentSection = section[1];
|
|
318
|
+
continue;
|
|
319
|
+
}
|
|
320
|
+
const keyValue = line.match(/^\s*([^=]+?)\s*=\s*(.+?)\s*$/);
|
|
321
|
+
if (keyValue) {
|
|
322
|
+
const key = keyValue[1];
|
|
323
|
+
const value = keyValue[2];
|
|
324
|
+
if (currentSection) {
|
|
325
|
+
result[`${currentSection}.${key}`] = value;
|
|
326
|
+
}
|
|
327
|
+
else {
|
|
328
|
+
result[key] = value;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
return result;
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* Compresses an image file.
|
|
336
|
+
*
|
|
337
|
+
* @param {File} file - The image file to compress.
|
|
338
|
+
* @param {Object} options - Optional parameters for compression.
|
|
339
|
+
* @param {number} options.maxSizeMB - The maximum size of the compressed image file in megabytes (default is 1MB).
|
|
340
|
+
* @param {number} options.maxWidthOrHeight - The maximum width or height of the compressed image file in pixels (default is 1920).
|
|
341
|
+
* @return {Promise<File>} The compressed image file.
|
|
342
|
+
*/
|
|
343
|
+
export async function compressImage(file, { maxSizeMB = 1, maxWidthOrHeight = 1920 } = {}) {
|
|
344
|
+
const extensions = ['jpeg', 'jpg', 'png', 'gif', 'webp', 'svg'];
|
|
345
|
+
const fileExt = file.name.split('.').pop();
|
|
346
|
+
const options = {
|
|
347
|
+
maxSizeMB: maxSizeMB,
|
|
348
|
+
maxWidthOrHeight: maxWidthOrHeight,
|
|
349
|
+
};
|
|
350
|
+
let result = file;
|
|
351
|
+
if (fileExt != null && extensions.includes(fileExt.toLowerCase())) {
|
|
352
|
+
try {
|
|
353
|
+
result = await imageCompression(file, options);
|
|
354
|
+
}
|
|
355
|
+
catch (error) {
|
|
356
|
+
console.log(error);
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
if (result instanceof Blob)
|
|
360
|
+
return new File([result], file.name);
|
|
361
|
+
return result;
|
|
362
|
+
}
|
|
363
|
+
/**
|
|
364
|
+
* Returns the base URL of the current window location.
|
|
365
|
+
*
|
|
366
|
+
* @return {string} The base URL of the current window location.
|
|
367
|
+
*/
|
|
368
|
+
export function getBaseUrl() {
|
|
369
|
+
return window.location.protocol + '//' + window.location.hostname;
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* Returns the URL of the current page by appending the base URL with the pathname.
|
|
373
|
+
*
|
|
374
|
+
* @return {string} The URL of the current page.
|
|
375
|
+
*/
|
|
376
|
+
export function getPageUrl() {
|
|
377
|
+
return getBaseUrl() + window.location.pathname;
|
|
378
|
+
}
|
|
379
|
+
/**
|
|
380
|
+
* Generates a function comment for the given function body in a markdown code block with the correct language syntax.
|
|
381
|
+
*
|
|
382
|
+
* @param {string} url - the URL to open in the new window
|
|
383
|
+
* @param {Array<string>} data - an array of strings containing the data to be sent to the server
|
|
384
|
+
* @param {Object} options - an optional object containing target and method properties
|
|
385
|
+
* @param {string} options.target - the target attribute for the form submission (default: '_self')
|
|
386
|
+
* @param {string} options.method - the method attribute for the form submission (default: 'POST')
|
|
387
|
+
* @return {void} This function does not return anything
|
|
388
|
+
*/
|
|
389
|
+
export function openWindowWithJS(url, data, { target = '_self', method = 'POST' } = {}) {
|
|
390
|
+
var form = document.createElement('form');
|
|
391
|
+
form.target = target;
|
|
392
|
+
form.method = method;
|
|
393
|
+
form.action = url;
|
|
394
|
+
form.style.display = 'none';
|
|
395
|
+
for (var key in data) {
|
|
396
|
+
var input = document.createElement('input');
|
|
397
|
+
input.type = 'hidden';
|
|
398
|
+
input.name = key;
|
|
399
|
+
input.value = data[key];
|
|
400
|
+
form.appendChild(input);
|
|
401
|
+
}
|
|
402
|
+
document.body.appendChild(form);
|
|
403
|
+
form.submit();
|
|
404
|
+
document.body.removeChild(form);
|
|
405
|
+
}
|
|
406
|
+
/**
|
|
407
|
+
* Loads the metadata of an image from the specified URL.
|
|
408
|
+
*
|
|
409
|
+
* @param {string} url - The URL of the image.
|
|
410
|
+
* @return {Promise<Image>} A promise that resolves with the loaded image.
|
|
411
|
+
*/
|
|
412
|
+
export function loadImageMeta(url) {
|
|
413
|
+
return new Promise((resolve, reject) => {
|
|
414
|
+
const img = new Image();
|
|
415
|
+
img.onload = () => resolve(img);
|
|
416
|
+
img.onerror = reject;
|
|
417
|
+
img.src = url;
|
|
418
|
+
});
|
|
419
|
+
}
|
|
420
|
+
/**
|
|
421
|
+
* Abbreviates a number by adding a suffix representing the magnitude.
|
|
422
|
+
*
|
|
423
|
+
* @param {number} number - The number to be abbreviated.
|
|
424
|
+
* @return {string} The abbreviated number with the magnitude suffix.
|
|
425
|
+
*/
|
|
426
|
+
export function abbreviateNumber(number) {
|
|
427
|
+
var SI_SYMBOL = ['', 'k', 'M', 'G', 'T', 'P', 'E'];
|
|
428
|
+
var tier = (Math.log10(Math.abs(number)) / 3) | 0;
|
|
429
|
+
if (tier == 0)
|
|
430
|
+
return number;
|
|
431
|
+
var suffix = SI_SYMBOL[tier];
|
|
432
|
+
var scale = Math.pow(10, tier * 3);
|
|
433
|
+
var scaled = number / scale;
|
|
434
|
+
return scaled.toFixed(1) + suffix;
|
|
435
|
+
}
|
|
436
|
+
/**
|
|
437
|
+
* Extracts numbers from a given string.
|
|
438
|
+
*
|
|
439
|
+
* @param {string} string - The string from which to extract numbers.
|
|
440
|
+
* @param {object} options - Optional parameters for customization.
|
|
441
|
+
* @param {boolean} options.array - Whether to return an array of numbers. Defaults to false.
|
|
442
|
+
* @param {boolean} options.float - Whether to include floating-point numbers. Defaults to true.
|
|
443
|
+
* @return {number|array} - The extracted number(s) from the string. If options.array is true, an array will be returned. Otherwise, a single number will be returned.
|
|
444
|
+
*/
|
|
445
|
+
export function extractNumber(string, { array = false, float = true } = {}) {
|
|
446
|
+
const result = string.toString().match(float ? /[+-]?\d+(\.\d+)?/g : /\d/g);
|
|
447
|
+
if (result)
|
|
448
|
+
return array
|
|
449
|
+
? result.map((r) => parseFloat(r))
|
|
450
|
+
: parseFloat(result.join(''));
|
|
451
|
+
else
|
|
452
|
+
return array ? [0] : 0;
|
|
453
|
+
}
|
|
454
|
+
/**
|
|
455
|
+
* Extracts all alphabetic characters from a given string.
|
|
456
|
+
*
|
|
457
|
+
* @param {string} string - The input string.
|
|
458
|
+
* @param {Object} options - Optional parameters.
|
|
459
|
+
* @param {boolean} options.array - Whether to return the result as an array. Default is false.
|
|
460
|
+
* @return {string | string[]} - The extracted alphabetic characters, either as a string or an array.
|
|
461
|
+
*/
|
|
462
|
+
export function extractAlphabet(string, { array = false } = {}) {
|
|
463
|
+
const result = string.toString().match(/[a-zA-Z]/);
|
|
464
|
+
if (result)
|
|
465
|
+
return array ? result : result.join('');
|
|
466
|
+
else
|
|
467
|
+
return array ? [''] : '';
|
|
468
|
+
}
|
|
469
|
+
/**
|
|
470
|
+
* Removes the specified searchString from the given str.
|
|
471
|
+
*
|
|
472
|
+
* @param {string} str - The original string.
|
|
473
|
+
* @param {string} searchString - The string to be removed from str.
|
|
474
|
+
* @return {string} The modified string with the searchString removed.
|
|
475
|
+
*/
|
|
476
|
+
export function removeFromString(str, searchString) {
|
|
477
|
+
if (str.endsWith(searchString)) {
|
|
478
|
+
return str.slice(0, -searchString.length);
|
|
479
|
+
}
|
|
480
|
+
return str;
|
|
481
|
+
}
|
|
482
|
+
/**
|
|
483
|
+
* Replaces multiple parts of a string based on a given set of replacements.
|
|
484
|
+
*
|
|
485
|
+
* @param {string} inputString - The original string to be modified.
|
|
486
|
+
* @param {Record<string, string>} replacements - An object containing key-value pairs where the key is the part to be replaced and the value is the replacement string.
|
|
487
|
+
* @return {string} - The modified string with all occurrences of the specified parts replaced.
|
|
488
|
+
*/
|
|
489
|
+
export function replaceStringParts(inputString, replacements) {
|
|
490
|
+
if (Object.keys(replacements).length === 0) {
|
|
491
|
+
return inputString;
|
|
492
|
+
}
|
|
493
|
+
// Create a regular expression pattern by joining all keys of replacements with the "|" (OR) operator
|
|
494
|
+
const pattern = new RegExp(Object.keys(replacements).join('|'), 'g');
|
|
495
|
+
// Use the replace method with a callback function
|
|
496
|
+
const resultString = inputString.replace(pattern, (match) => {
|
|
497
|
+
// Use the match as a key to get the replacement value from the replacements object
|
|
498
|
+
return replacements[match];
|
|
499
|
+
});
|
|
500
|
+
return resultString;
|
|
501
|
+
}
|
|
502
|
+
/**
|
|
503
|
+
* Generates a custom ID of the specified length.
|
|
504
|
+
*
|
|
505
|
+
* @param {number} length - The length of the ID to be generated.
|
|
506
|
+
* @return {string} The custom ID.
|
|
507
|
+
*/
|
|
508
|
+
export function generateCustomId(length) {
|
|
509
|
+
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
|
510
|
+
let result = '';
|
|
511
|
+
for (let i = 0; i < length; i++) {
|
|
512
|
+
const randomIndex = Math.floor(Math.random() * chars.length);
|
|
513
|
+
result += chars.charAt(randomIndex);
|
|
514
|
+
}
|
|
515
|
+
return result;
|
|
516
|
+
}
|
|
517
|
+
/**
|
|
518
|
+
* Removes duplicate elements from an array based on a specified property.
|
|
519
|
+
*
|
|
520
|
+
* @param {T[]} arr - The array from which duplicate elements will be removed.
|
|
521
|
+
* @param {keyof T} property - The property used to determine duplicates.
|
|
522
|
+
* @returns {T[]} The array with duplicate elements removed.
|
|
523
|
+
*/
|
|
524
|
+
export function removeDuplicates(arr, property) {
|
|
525
|
+
const uniqueValues = new Set();
|
|
526
|
+
return arr.filter((obj) => {
|
|
527
|
+
const value = obj[property];
|
|
528
|
+
if (!uniqueValues.has(value)) {
|
|
529
|
+
uniqueValues.add(value);
|
|
530
|
+
return true;
|
|
531
|
+
}
|
|
532
|
+
return false;
|
|
533
|
+
});
|
|
534
|
+
}
|
|
535
|
+
/**
|
|
536
|
+
* Converts a hexadecimal color code to an RGBA color with the specified alpha value.
|
|
537
|
+
*
|
|
538
|
+
* @param {string} hex - the hexadecimal color code
|
|
539
|
+
* @param {number} alpha - the alpha value for the RGBA color
|
|
540
|
+
* @return {string} the RGBA color code
|
|
541
|
+
*/
|
|
542
|
+
export function hexToRgba(hex, alpha) {
|
|
543
|
+
const r = parseInt(hex.slice(1, 3), 16);
|
|
544
|
+
const g = parseInt(hex.slice(3, 5), 16);
|
|
545
|
+
const b = parseInt(hex.slice(5, 7), 16);
|
|
546
|
+
return `rgba(${r}, ${g}, ${b}, ${alpha})`;
|
|
547
|
+
}
|
|
548
|
+
/**
|
|
549
|
+
* Shuffles the elements in an array in place using the Fisher-Yates algorithm.
|
|
550
|
+
*
|
|
551
|
+
* @param {T[]} array - The array to be shuffled.
|
|
552
|
+
* @return {T[]} The shuffled array.
|
|
553
|
+
*/
|
|
554
|
+
export function shuffleArray(array) {
|
|
555
|
+
for (let i = array.length - 1; i > 0; i--) {
|
|
556
|
+
const j = Math.floor(Math.random() * (i + 1));
|
|
557
|
+
[array[i], array[j]] = [array[j], array[i]]; // Swap elements
|
|
558
|
+
}
|
|
559
|
+
return array;
|
|
560
|
+
}
|
|
561
|
+
/**
|
|
562
|
+
* Scrolls to the element with the specified ID, with a smooth animation.
|
|
563
|
+
*
|
|
564
|
+
* @param {string} id - The ID of the element to scroll to.
|
|
565
|
+
*/
|
|
566
|
+
export function slideToId(id) {
|
|
567
|
+
const element = document.getElementById(id);
|
|
568
|
+
if (element) {
|
|
569
|
+
element.scrollIntoView({ behavior: 'smooth', block: 'start' });
|
|
570
|
+
}
|
|
571
|
+
else {
|
|
572
|
+
console.error(`Element with ID "${id}" not found.`);
|
|
573
|
+
}
|
|
574
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@creopse/utils",
|
|
3
|
+
"description": "Creopse Utils Toolkit",
|
|
4
|
+
"version": "0.0.1",
|
|
5
|
+
"private": false,
|
|
6
|
+
"author": "Noé Gnanih <noegnanih@gmail.com>",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"type": "module",
|
|
9
|
+
"main": "dist/index.js",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"import": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"./*": "./*"
|
|
16
|
+
},
|
|
17
|
+
"types": "dist/index.d.ts",
|
|
18
|
+
"files": [
|
|
19
|
+
"dist",
|
|
20
|
+
"README.md"
|
|
21
|
+
],
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "tsc -b && tsc-alias"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@types/lodash": "^4.17.20",
|
|
27
|
+
"tsc-alias": "^1.8.16",
|
|
28
|
+
"typescript": "~5.8.3"
|
|
29
|
+
},
|
|
30
|
+
"repository": {
|
|
31
|
+
"type": "git",
|
|
32
|
+
"url": "git+https://github.com/creopse/toolkit.git"
|
|
33
|
+
},
|
|
34
|
+
"keywords": [],
|
|
35
|
+
"bugs": {
|
|
36
|
+
"url": "https://github.com/creopse/toolkit/issues"
|
|
37
|
+
},
|
|
38
|
+
"homepage": "https://github.com/creopse/toolkit#readme",
|
|
39
|
+
"publishConfig": {
|
|
40
|
+
"access": "public"
|
|
41
|
+
},
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"@inertiajs/core": "^1.0.15",
|
|
44
|
+
"browser-image-compression": "^2.0.2",
|
|
45
|
+
"lodash": "^4.17.21",
|
|
46
|
+
"moment": "^2.30.1"
|
|
47
|
+
}
|
|
48
|
+
}
|