@creopse/utils 0.0.15 → 0.0.16
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/LICENSE +21 -0
- package/dist/enums/index.d.ts +3 -0
- package/dist/enums/index.js +3 -0
- package/dist/enums/notification-type.d.ts +1 -1
- package/dist/enums/notification-type.js +1 -1
- package/dist/enums/permission.d.ts +28 -0
- package/dist/enums/permission.js +38 -0
- package/dist/enums/profile-type.d.ts +1 -0
- package/dist/enums/profile-type.js +1 -0
- package/dist/{utils → helpers}/chronos.d.ts +1 -1
- package/dist/{utils → helpers}/chronos.js +1 -1
- package/dist/helpers/constants/constants.d.ts +2 -0
- package/dist/helpers/constants/constants.js +3 -0
- package/dist/helpers/constants/index.d.ts +1 -0
- package/dist/helpers/constants/index.js +1 -0
- package/dist/helpers/constants.d.ts +2 -0
- package/dist/helpers/constants.js +3 -0
- package/dist/helpers/functions/array.d.ts +22 -0
- package/dist/helpers/functions/array.js +42 -0
- package/dist/helpers/functions/browser.d.ts +40 -0
- package/dist/helpers/functions/browser.js +72 -0
- package/dist/helpers/functions/color.d.ts +14 -0
- package/dist/helpers/functions/color.js +26 -0
- package/dist/helpers/functions/file.d.ts +64 -0
- package/dist/helpers/functions/file.js +210 -0
- package/dist/helpers/functions/geo.d.ts +17 -0
- package/dist/helpers/functions/geo.js +31 -0
- package/dist/helpers/functions/image.d.ts +20 -0
- package/dist/helpers/functions/image.js +44 -0
- package/dist/helpers/functions/index.d.ts +13 -0
- package/dist/helpers/functions/index.js +13 -0
- package/dist/helpers/functions/misc.d.ts +55 -0
- package/dist/helpers/functions/misc.js +125 -0
- package/dist/helpers/functions/number.d.ts +15 -0
- package/dist/helpers/functions/number.js +26 -0
- package/dist/helpers/functions/object.d.ts +79 -0
- package/dist/helpers/functions/object.js +141 -0
- package/dist/helpers/functions/string.d.ts +110 -0
- package/dist/helpers/functions/string.js +211 -0
- package/dist/helpers/functions/svg.d.ts +7 -0
- package/dist/helpers/functions/svg.js +25 -0
- package/dist/helpers/functions/time.d.ts +97 -0
- package/dist/helpers/functions/time.js +97 -0
- package/dist/helpers/functions/url.d.ts +46 -0
- package/dist/helpers/functions/url.js +120 -0
- package/dist/{utils → helpers}/functions.d.ts +14 -0
- package/dist/{utils → helpers}/functions.js +26 -1
- package/dist/{utils → helpers}/index.d.ts +1 -1
- package/dist/{utils → helpers}/index.js +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/models/page-section.d.ts +2 -1
- package/dist/models/page-section.js +2 -1
- package/dist/types/core-bridge/api.d.ts +31 -0
- package/dist/types/core-bridge/api.js +1 -0
- package/dist/types/core-bridge/i18n.d.ts +6 -0
- package/dist/types/core-bridge/i18n.js +1 -0
- package/dist/types/core-bridge/index.d.ts +4 -0
- package/dist/types/core-bridge/index.js +4 -0
- package/dist/types/core-bridge/shared-data.d.ts +22 -0
- package/dist/types/core-bridge/shared-data.js +1 -0
- package/dist/types/core-bridge/stores.d.ts +31 -0
- package/dist/types/core-bridge/stores.js +1 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.js +2 -0
- package/dist/types/plugin.d.ts +30 -0
- package/dist/types/plugin.js +1 -0
- package/package.json +23 -5
- package/dist/tsconfig.tsbuildinfo +0 -1
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import { MediaFileType } from '../../enums/media-file-type';
|
|
2
|
+
/**
|
|
3
|
+
* Converts a number of bytes to a human-readable file size.
|
|
4
|
+
*
|
|
5
|
+
* @param {number} bytes - The number of bytes to convert.
|
|
6
|
+
* @param {boolean} si - Whether to use the SI units (1000) or binary units (1024).
|
|
7
|
+
* @param {number} dp - The number of decimal places to round the result to.
|
|
8
|
+
* @return {string} The human-readable file size.
|
|
9
|
+
*/
|
|
10
|
+
export function humanFileSize(bytes, si = false, dp = 1) {
|
|
11
|
+
const thresh = si ? 1000 : 1024;
|
|
12
|
+
if (Math.abs(bytes) < thresh) {
|
|
13
|
+
return bytes + ' B';
|
|
14
|
+
}
|
|
15
|
+
const units = si
|
|
16
|
+
? ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
|
|
17
|
+
: ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
|
|
18
|
+
let u = -1;
|
|
19
|
+
const r = 10 ** dp;
|
|
20
|
+
do {
|
|
21
|
+
bytes /= thresh;
|
|
22
|
+
++u;
|
|
23
|
+
} while (Math.round(Math.abs(bytes) * r) / r >= thresh &&
|
|
24
|
+
u < units.length - 1);
|
|
25
|
+
return bytes.toFixed(dp) + ' ' + units[u];
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Returns the filename from a given URL.
|
|
29
|
+
*
|
|
30
|
+
* @param {string} url - The URL from which to extract the filename.
|
|
31
|
+
* @return {string} The filename extracted from the URL.
|
|
32
|
+
*/
|
|
33
|
+
export function filenameFromUrl(url) {
|
|
34
|
+
return url.substring(url.lastIndexOf('/') + 1);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Converts a binary string to a Uint8Array.
|
|
38
|
+
*
|
|
39
|
+
* @param {string} binary - The binary string to convert.
|
|
40
|
+
* @returns {Uint8Array} The Uint8Array representation of the binary string.
|
|
41
|
+
*/
|
|
42
|
+
function binaryStringToUint8Array(binary) {
|
|
43
|
+
const binaryLength = binary.length;
|
|
44
|
+
const bytes = new Uint8Array(binaryLength);
|
|
45
|
+
for (let i = 0; i < binaryLength; i++) {
|
|
46
|
+
bytes[i] = binary.charCodeAt(i);
|
|
47
|
+
}
|
|
48
|
+
return bytes;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Convert binary string content to a JavaScript File object and optionally trigger a download.
|
|
52
|
+
*
|
|
53
|
+
* @param {string} binaryContent - The binary string content of the file.
|
|
54
|
+
* @param {string} fileName - The name for the generated file.
|
|
55
|
+
* @param {string} mimeType - The MIME type of the file.
|
|
56
|
+
* @param {boolean} download - Whether to trigger a download of the file.
|
|
57
|
+
* @returns {File} - The generated File object.
|
|
58
|
+
*/
|
|
59
|
+
export function binaryContentToFile(binaryContent, fileName = 'file.png', mimeType = 'image/png', download = false) {
|
|
60
|
+
// Convert the binary string to Uint8Array
|
|
61
|
+
const byteArray = binaryStringToUint8Array(binaryContent);
|
|
62
|
+
// Create a Blob from the Uint8Array
|
|
63
|
+
const blob = new Blob([byteArray], { type: mimeType });
|
|
64
|
+
// Create a File from the Blob
|
|
65
|
+
const file = new File([blob], fileName, { type: mimeType });
|
|
66
|
+
if (download) {
|
|
67
|
+
// If download is true, trigger a download of the file
|
|
68
|
+
const url = URL.createObjectURL(blob);
|
|
69
|
+
const a = document.createElement('a');
|
|
70
|
+
a.href = url;
|
|
71
|
+
a.download = fileName;
|
|
72
|
+
document.body.appendChild(a);
|
|
73
|
+
a.click();
|
|
74
|
+
setTimeout(() => {
|
|
75
|
+
document.body.removeChild(a);
|
|
76
|
+
window.URL.revokeObjectURL(url);
|
|
77
|
+
}, 0);
|
|
78
|
+
}
|
|
79
|
+
// Return the File object
|
|
80
|
+
return file;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Convert binary string content to a Data URL.
|
|
84
|
+
*
|
|
85
|
+
* @param {string} binaryContent - The binary string content of the file.
|
|
86
|
+
* @param {string} mimeType - The MIME type of the file.
|
|
87
|
+
* @returns {Promise<string>} - The Data URL representation of the file.
|
|
88
|
+
*/
|
|
89
|
+
export function binaryContentToDataURL(binaryContent, mimeType) {
|
|
90
|
+
// Convert the binary string to Uint8Array
|
|
91
|
+
const byteArray = binaryStringToUint8Array(binaryContent);
|
|
92
|
+
// Create a Blob from the Uint8Array
|
|
93
|
+
const blob = new Blob([byteArray], { type: mimeType });
|
|
94
|
+
// Create a FileReader to read the Blob as a Data URL
|
|
95
|
+
const reader = new FileReader();
|
|
96
|
+
return new Promise((resolve, reject) => {
|
|
97
|
+
reader.onloadend = () => {
|
|
98
|
+
resolve(reader.result);
|
|
99
|
+
};
|
|
100
|
+
reader.onerror = reject;
|
|
101
|
+
// Read the Blob as Data URL
|
|
102
|
+
reader.readAsDataURL(blob);
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Convert a Blob to a Data URL.
|
|
107
|
+
*
|
|
108
|
+
* @param {Blob} blob - The Blob to convert.
|
|
109
|
+
* @returns {Promise<string>} - A promise that resolves to the Data URL.
|
|
110
|
+
*/
|
|
111
|
+
export function blobToDataURL(blob) {
|
|
112
|
+
return new Promise((resolve, reject) => {
|
|
113
|
+
const reader = new FileReader();
|
|
114
|
+
reader.onloadend = () => {
|
|
115
|
+
resolve(reader.result);
|
|
116
|
+
};
|
|
117
|
+
reader.onerror = reject;
|
|
118
|
+
reader.readAsDataURL(blob);
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Determines the media file type based on the MIME type of the provided file.
|
|
123
|
+
*
|
|
124
|
+
* @param {File} file - The file whose type is to be determined.
|
|
125
|
+
* @returns {MediaFileType} - The determined media file type, which can be IMAGE, VIDEO, AUDIO, DOCUMENT, or OTHER.
|
|
126
|
+
*/
|
|
127
|
+
export function determineFileType(file) {
|
|
128
|
+
const mimeType = file.type.toLowerCase();
|
|
129
|
+
// Define MIME types and extensions for each MediaFileType
|
|
130
|
+
const imageMimes = [
|
|
131
|
+
'image/jpeg',
|
|
132
|
+
'image/png',
|
|
133
|
+
'image/gif',
|
|
134
|
+
'image/bmp',
|
|
135
|
+
'image/webp',
|
|
136
|
+
'image/svg+xml',
|
|
137
|
+
];
|
|
138
|
+
const videoMimes = [
|
|
139
|
+
'video/mp4',
|
|
140
|
+
'video/mpeg',
|
|
141
|
+
'video/quicktime',
|
|
142
|
+
'video/x-ms-wmv',
|
|
143
|
+
'video/x-msvideo',
|
|
144
|
+
'video/x-flv',
|
|
145
|
+
];
|
|
146
|
+
const audioMimes = [
|
|
147
|
+
'audio/mpeg',
|
|
148
|
+
'audio/wav',
|
|
149
|
+
'audio/x-wav',
|
|
150
|
+
'audio/ogg',
|
|
151
|
+
'audio/midi',
|
|
152
|
+
'audio/x-ms-wma',
|
|
153
|
+
];
|
|
154
|
+
const documentMimes = [
|
|
155
|
+
'application/pdf',
|
|
156
|
+
'application/msword',
|
|
157
|
+
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
|
158
|
+
'application/vnd.ms-excel',
|
|
159
|
+
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
160
|
+
'application/vnd.ms-powerpoint',
|
|
161
|
+
'application/vnd.openxmlformats-officedocument.presentationml.presentation',
|
|
162
|
+
'text/plain',
|
|
163
|
+
'application/rtf',
|
|
164
|
+
];
|
|
165
|
+
if (imageMimes.includes(mimeType)) {
|
|
166
|
+
return MediaFileType.IMAGE;
|
|
167
|
+
}
|
|
168
|
+
if (videoMimes.includes(mimeType)) {
|
|
169
|
+
return MediaFileType.VIDEO;
|
|
170
|
+
}
|
|
171
|
+
if (audioMimes.includes(mimeType)) {
|
|
172
|
+
return MediaFileType.AUDIO;
|
|
173
|
+
}
|
|
174
|
+
if (documentMimes.includes(mimeType)) {
|
|
175
|
+
return MediaFileType.DOCUMENT;
|
|
176
|
+
}
|
|
177
|
+
// If the MIME type doesn't match any of the above, return OTHER
|
|
178
|
+
return MediaFileType.OTHER;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Gets the file extension from a given path.
|
|
182
|
+
*
|
|
183
|
+
* @param {string} path - The path of the file.
|
|
184
|
+
* @returns {string} The file extension, or an empty string if none is found.
|
|
185
|
+
*/
|
|
186
|
+
export const getFileExtension = (path) => {
|
|
187
|
+
const base = path.split('/').pop() || '';
|
|
188
|
+
const lastDot = base.lastIndexOf('.');
|
|
189
|
+
if (lastDot <= 0 || lastDot === base.length - 1)
|
|
190
|
+
return '';
|
|
191
|
+
return base.slice(lastDot);
|
|
192
|
+
};
|
|
193
|
+
/**
|
|
194
|
+
* Downloads the given object as a JSON file with the specified name.
|
|
195
|
+
*
|
|
196
|
+
* @param {object} exportObj - The object to be exported as JSON.
|
|
197
|
+
* @param {string} exportName - The name of the exported file.
|
|
198
|
+
* @return {void} This function does not return a value.
|
|
199
|
+
*/
|
|
200
|
+
export function downloadObjectAsJson(exportObj, exportName) {
|
|
201
|
+
var dataStr = 'data:text/json;charset=utf-8,' +
|
|
202
|
+
encodeURIComponent(JSON.stringify(exportObj, null, 2));
|
|
203
|
+
var element = document.createElement('a');
|
|
204
|
+
element.setAttribute('href', dataStr);
|
|
205
|
+
element.setAttribute('download', exportName + '.json');
|
|
206
|
+
element.style.display = 'none';
|
|
207
|
+
document.body.appendChild(element);
|
|
208
|
+
element.click();
|
|
209
|
+
document.body.removeChild(element);
|
|
210
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Calculates the distance between two points on the Earth's surface using the Haversine formula.
|
|
3
|
+
*
|
|
4
|
+
* @param {number} lat1 - The latitude of the first point.
|
|
5
|
+
* @param {number} lon1 - The longitude of the first point.
|
|
6
|
+
* @param {number} lat2 - The latitude of the second point.
|
|
7
|
+
* @param {number} lon2 - The longitude of the second point.
|
|
8
|
+
* @return {number} The distance between the two points in kilometers.
|
|
9
|
+
*/
|
|
10
|
+
export declare function getDistanceFromLatLonInKm(lat1: number, lon1: number, lat2: number, lon2: number): number;
|
|
11
|
+
/**
|
|
12
|
+
* Converts degrees to radians.
|
|
13
|
+
*
|
|
14
|
+
* @param {number} deg - The angle in degrees.
|
|
15
|
+
* @returns {number} The angle in radians.
|
|
16
|
+
*/
|
|
17
|
+
export declare function deg2rad(deg: number): number;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Calculates the distance between two points on the Earth's surface using the Haversine formula.
|
|
3
|
+
*
|
|
4
|
+
* @param {number} lat1 - The latitude of the first point.
|
|
5
|
+
* @param {number} lon1 - The longitude of the first point.
|
|
6
|
+
* @param {number} lat2 - The latitude of the second point.
|
|
7
|
+
* @param {number} lon2 - The longitude of the second point.
|
|
8
|
+
* @return {number} The distance between the two points in kilometers.
|
|
9
|
+
*/
|
|
10
|
+
export function getDistanceFromLatLonInKm(lat1, lon1, lat2, lon2) {
|
|
11
|
+
var R = 6371;
|
|
12
|
+
var dLat = deg2rad(lat2 - lat1);
|
|
13
|
+
var dLon = deg2rad(lon2 - lon1);
|
|
14
|
+
var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
|
|
15
|
+
Math.cos(deg2rad(lat1)) *
|
|
16
|
+
Math.cos(deg2rad(lat2)) *
|
|
17
|
+
Math.sin(dLon / 2) *
|
|
18
|
+
Math.sin(dLon / 2);
|
|
19
|
+
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
|
|
20
|
+
var d = R * c;
|
|
21
|
+
return d;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Converts degrees to radians.
|
|
25
|
+
*
|
|
26
|
+
* @param {number} deg - The angle in degrees.
|
|
27
|
+
* @returns {number} The angle in radians.
|
|
28
|
+
*/
|
|
29
|
+
export function deg2rad(deg) {
|
|
30
|
+
return deg * (Math.PI / 180);
|
|
31
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Compresses an image file.
|
|
3
|
+
*
|
|
4
|
+
* @param {File} file - The image file to compress.
|
|
5
|
+
* @param {Object} options - Optional parameters for compression.
|
|
6
|
+
* @param {number} options.maxSizeMB - The maximum size of the compressed image file in megabytes (default is 1MB).
|
|
7
|
+
* @param {number} options.maxWidthOrHeight - The maximum width or height of the compressed image file in pixels (default is 1920).
|
|
8
|
+
* @return {Promise<File>} The compressed image file.
|
|
9
|
+
*/
|
|
10
|
+
export declare function compressImage(file: File, { maxSizeMB, maxWidthOrHeight, }?: {
|
|
11
|
+
maxSizeMB?: number;
|
|
12
|
+
maxWidthOrHeight?: number;
|
|
13
|
+
}): Promise<File>;
|
|
14
|
+
/**
|
|
15
|
+
* Loads the metadata of an image from the specified URL.
|
|
16
|
+
*
|
|
17
|
+
* @param {string} url - The URL of the image.
|
|
18
|
+
* @return {Promise<HTMLImageElement>} A promise that resolves with the loaded image.
|
|
19
|
+
*/
|
|
20
|
+
export declare function loadImageMeta(url: string): Promise<HTMLImageElement>;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import imageCompression from 'browser-image-compression';
|
|
2
|
+
/**
|
|
3
|
+
* Compresses an image file.
|
|
4
|
+
*
|
|
5
|
+
* @param {File} file - The image file to compress.
|
|
6
|
+
* @param {Object} options - Optional parameters for compression.
|
|
7
|
+
* @param {number} options.maxSizeMB - The maximum size of the compressed image file in megabytes (default is 1MB).
|
|
8
|
+
* @param {number} options.maxWidthOrHeight - The maximum width or height of the compressed image file in pixels (default is 1920).
|
|
9
|
+
* @return {Promise<File>} The compressed image file.
|
|
10
|
+
*/
|
|
11
|
+
export async function compressImage(file, { maxSizeMB = 1, maxWidthOrHeight = 1920, } = {}) {
|
|
12
|
+
const extensions = ['jpeg', 'jpg', 'png', 'gif', 'webp'];
|
|
13
|
+
const fileExt = file.name.split('.').pop();
|
|
14
|
+
const options = {
|
|
15
|
+
maxSizeMB: maxSizeMB,
|
|
16
|
+
maxWidthOrHeight: maxWidthOrHeight,
|
|
17
|
+
};
|
|
18
|
+
let result = file;
|
|
19
|
+
if (fileExt != null && extensions.includes(fileExt.toLowerCase())) {
|
|
20
|
+
try {
|
|
21
|
+
result = await imageCompression(file, options);
|
|
22
|
+
}
|
|
23
|
+
catch (error) {
|
|
24
|
+
console.log(error);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
if (result instanceof Blob)
|
|
28
|
+
return new File([result], file.name);
|
|
29
|
+
return result;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Loads the metadata of an image from the specified URL.
|
|
33
|
+
*
|
|
34
|
+
* @param {string} url - The URL of the image.
|
|
35
|
+
* @return {Promise<HTMLImageElement>} A promise that resolves with the loaded image.
|
|
36
|
+
*/
|
|
37
|
+
export function loadImageMeta(url) {
|
|
38
|
+
return new Promise((resolve, reject) => {
|
|
39
|
+
const img = new Image();
|
|
40
|
+
img.onload = () => resolve(img);
|
|
41
|
+
img.onerror = reject;
|
|
42
|
+
img.src = url;
|
|
43
|
+
});
|
|
44
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export * from './string';
|
|
2
|
+
export * from './number';
|
|
3
|
+
export * from './array';
|
|
4
|
+
export * from './color';
|
|
5
|
+
export * from './object';
|
|
6
|
+
export * from './file';
|
|
7
|
+
export * from './image';
|
|
8
|
+
export * from './url';
|
|
9
|
+
export * from './browser';
|
|
10
|
+
export * from './time';
|
|
11
|
+
export * from './geo';
|
|
12
|
+
export * from './misc';
|
|
13
|
+
export * from './svg';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export * from './string';
|
|
2
|
+
export * from './number';
|
|
3
|
+
export * from './array';
|
|
4
|
+
export * from './color';
|
|
5
|
+
export * from './object';
|
|
6
|
+
export * from './file';
|
|
7
|
+
export * from './image';
|
|
8
|
+
export * from './url';
|
|
9
|
+
export * from './browser';
|
|
10
|
+
export * from './time';
|
|
11
|
+
export * from './geo';
|
|
12
|
+
export * from './misc';
|
|
13
|
+
export * from './svg';
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Formats the given duration in seconds into a string representation of time.
|
|
3
|
+
*
|
|
4
|
+
* @param {number} duration - The duration in seconds to be formatted.
|
|
5
|
+
* @param {Object} options - Optional parameters for formatting.
|
|
6
|
+
* @param {string} options.separator - The separator used between hours, minutes, and seconds. Defaults to ':'.
|
|
7
|
+
* @return {string} - The formatted time string.
|
|
8
|
+
*/
|
|
9
|
+
export declare function formatTimeFromSeconds(duration: number, { separator }?: {
|
|
10
|
+
separator?: string | undefined;
|
|
11
|
+
}): string;
|
|
12
|
+
/**
|
|
13
|
+
* Generates a password with the specified length and character options.
|
|
14
|
+
*
|
|
15
|
+
* @param {number} length - The length of the password to be generated. Default is 10.
|
|
16
|
+
* @param {object} options - The character options for the password. Default is { letters: true, numbers: true, symbols: false }.
|
|
17
|
+
* @param {boolean} options.letters - Specifies whether to include letters in the password. Default is true.
|
|
18
|
+
* @param {boolean} options.numbers - Specifies whether to include numbers in the password. Default is true.
|
|
19
|
+
* @param {boolean} options.symbols - Specifies whether to include symbols in the password. Default is false.
|
|
20
|
+
* @return {string} The generated password.
|
|
21
|
+
*/
|
|
22
|
+
export declare function genPassword(length?: number, { letters, numbers, symbols }?: {
|
|
23
|
+
letters?: boolean | undefined;
|
|
24
|
+
numbers?: boolean | undefined;
|
|
25
|
+
symbols?: boolean | undefined;
|
|
26
|
+
}): string;
|
|
27
|
+
/**
|
|
28
|
+
* Parses an INI string and returns an object containing the key-value pairs.
|
|
29
|
+
*
|
|
30
|
+
* @param {string} iniString - The INI string to parse.
|
|
31
|
+
* @return {{ [key: string]: string }} - An object containing the key-value pairs parsed from the INI string.
|
|
32
|
+
*/
|
|
33
|
+
export declare function parseINIString(iniString: string): {
|
|
34
|
+
[key: string]: string;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Generates a custom ID of the specified length.
|
|
38
|
+
*
|
|
39
|
+
* @param {number} length - The length of the ID to be generated.
|
|
40
|
+
* @return {string} The custom ID.
|
|
41
|
+
*/
|
|
42
|
+
export declare function generateCustomId(length: number): string;
|
|
43
|
+
/**
|
|
44
|
+
* Sanitizes an ID by removing any characters that are not a letter, number, slash, or dash and replacing any slashes with dashes.
|
|
45
|
+
*
|
|
46
|
+
* @param {string} id - The ID to be sanitized.
|
|
47
|
+
* @return {string} The sanitized ID.
|
|
48
|
+
*/
|
|
49
|
+
export declare function sanitizeId(id: string): string;
|
|
50
|
+
/**
|
|
51
|
+
* Scrolls to the element with the specified ID, with a smooth animation.
|
|
52
|
+
*
|
|
53
|
+
* @param {string} id - The ID of the element to scroll to.
|
|
54
|
+
*/
|
|
55
|
+
export declare function slideToId(id: string): void;
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import _ from 'lodash';
|
|
2
|
+
import { strPadLeft } from './string';
|
|
3
|
+
/**
|
|
4
|
+
* Formats the given duration in seconds into a string representation of time.
|
|
5
|
+
*
|
|
6
|
+
* @param {number} duration - The duration in seconds to be formatted.
|
|
7
|
+
* @param {Object} options - Optional parameters for formatting.
|
|
8
|
+
* @param {string} options.separator - The separator used between hours, minutes, and seconds. Defaults to ':'.
|
|
9
|
+
* @return {string} - The formatted time string.
|
|
10
|
+
*/
|
|
11
|
+
export function formatTimeFromSeconds(duration, { separator = ':' } = {}) {
|
|
12
|
+
var hrs = ~~(duration / 3600);
|
|
13
|
+
var mins = ~~((duration % 3600) / 60);
|
|
14
|
+
var secs = ~~duration % 60;
|
|
15
|
+
var ret = '';
|
|
16
|
+
if (hrs > 0) {
|
|
17
|
+
ret +=
|
|
18
|
+
'' + strPadLeft(String(hrs), '0', 2) + separator + (mins < 10 ? '00' : '');
|
|
19
|
+
}
|
|
20
|
+
ret +=
|
|
21
|
+
'' + strPadLeft(String(mins), '0', 2) + separator + (secs < 10 ? '00' : '');
|
|
22
|
+
ret += '' + strPadLeft(String(secs), '0', 2);
|
|
23
|
+
return ret;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Generates a password with the specified length and character options.
|
|
27
|
+
*
|
|
28
|
+
* @param {number} length - The length of the password to be generated. Default is 10.
|
|
29
|
+
* @param {object} options - The character options for the password. Default is { letters: true, numbers: true, symbols: false }.
|
|
30
|
+
* @param {boolean} options.letters - Specifies whether to include letters in the password. Default is true.
|
|
31
|
+
* @param {boolean} options.numbers - Specifies whether to include numbers in the password. Default is true.
|
|
32
|
+
* @param {boolean} options.symbols - Specifies whether to include symbols in the password. Default is false.
|
|
33
|
+
* @return {string} The generated password.
|
|
34
|
+
*/
|
|
35
|
+
export function genPassword(length = 10, { letters = true, numbers = true, symbols = false } = {}) {
|
|
36
|
+
if (!letters && !numbers && !symbols)
|
|
37
|
+
return '';
|
|
38
|
+
let base = [], password = '';
|
|
39
|
+
const numbersBase = '0123456789'.split(''), symbolsBase = '!@#$%^&*()'.split(''), lettersBase = 'abcdefghijklmnopqrstuvwxyz'.split('');
|
|
40
|
+
if (letters)
|
|
41
|
+
base.push(...lettersBase);
|
|
42
|
+
if (numbers)
|
|
43
|
+
base.push(...numbersBase);
|
|
44
|
+
if (symbols)
|
|
45
|
+
base.push(...symbolsBase);
|
|
46
|
+
base = _.shuffle(base);
|
|
47
|
+
for (let i = 0; i < length; i++) {
|
|
48
|
+
const randomNumber = Math.floor(Math.random() * base.length);
|
|
49
|
+
const char = lettersBase.includes(base[randomNumber]) &&
|
|
50
|
+
Math.floor(Math.random() * 2) === 0
|
|
51
|
+
? base[randomNumber].toUpperCase()
|
|
52
|
+
: base[randomNumber];
|
|
53
|
+
password += char;
|
|
54
|
+
}
|
|
55
|
+
return password;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Parses an INI string and returns an object containing the key-value pairs.
|
|
59
|
+
*
|
|
60
|
+
* @param {string} iniString - The INI string to parse.
|
|
61
|
+
* @return {{ [key: string]: string }} - An object containing the key-value pairs parsed from the INI string.
|
|
62
|
+
*/
|
|
63
|
+
export function parseINIString(iniString) {
|
|
64
|
+
const lines = iniString.split('\n');
|
|
65
|
+
const result = {};
|
|
66
|
+
let currentSection = null;
|
|
67
|
+
for (const line of lines) {
|
|
68
|
+
// eslint-disable-next-line no-useless-escape
|
|
69
|
+
const section = line.match(/^\s*\[([^\[\]]+)\]\s*$/);
|
|
70
|
+
if (section) {
|
|
71
|
+
currentSection = section[1];
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
const keyValue = line.match(/^\s*([^=]+?)\s*=\s*(.+?)\s*$/);
|
|
75
|
+
if (keyValue) {
|
|
76
|
+
const key = keyValue[1];
|
|
77
|
+
const value = keyValue[2];
|
|
78
|
+
if (currentSection) {
|
|
79
|
+
result[`${currentSection}.${key}`] = value;
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
result[key] = value;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return result;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Generates a custom ID of the specified length.
|
|
90
|
+
*
|
|
91
|
+
* @param {number} length - The length of the ID to be generated.
|
|
92
|
+
* @return {string} The custom ID.
|
|
93
|
+
*/
|
|
94
|
+
export function generateCustomId(length) {
|
|
95
|
+
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
|
96
|
+
let result = '';
|
|
97
|
+
for (let i = 0; i < length; i++) {
|
|
98
|
+
const randomIndex = Math.floor(Math.random() * chars.length);
|
|
99
|
+
result += chars.charAt(randomIndex);
|
|
100
|
+
}
|
|
101
|
+
return result;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Sanitizes an ID by removing any characters that are not a letter, number, slash, or dash and replacing any slashes with dashes.
|
|
105
|
+
*
|
|
106
|
+
* @param {string} id - The ID to be sanitized.
|
|
107
|
+
* @return {string} The sanitized ID.
|
|
108
|
+
*/
|
|
109
|
+
export function sanitizeId(id) {
|
|
110
|
+
return id.replace(/[^a-z0-9\/-]/g, '').replace(/\//g, '-');
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Scrolls to the element with the specified ID, with a smooth animation.
|
|
114
|
+
*
|
|
115
|
+
* @param {string} id - The ID of the element to scroll to.
|
|
116
|
+
*/
|
|
117
|
+
export function slideToId(id) {
|
|
118
|
+
const element = document.getElementById(id);
|
|
119
|
+
if (element) {
|
|
120
|
+
element.scrollIntoView({ behavior: 'smooth', block: 'start' });
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
console.error(`Element with ID "${id}" not found.`);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generates a random integer between the provided minimum and maximum values (inclusive).
|
|
3
|
+
*
|
|
4
|
+
* @param {number} min - The minimum value for the random integer.
|
|
5
|
+
* @param {number} max - The maximum value for the random integer.
|
|
6
|
+
* @return {number} The randomly generated integer.
|
|
7
|
+
*/
|
|
8
|
+
export declare function getRandomInteger(min: number, max: number): number;
|
|
9
|
+
/**
|
|
10
|
+
* Abbreviates a number by adding a suffix representing the magnitude.
|
|
11
|
+
*
|
|
12
|
+
* @param {number} number - The number to be abbreviated.
|
|
13
|
+
* @return {string|number} The abbreviated number with the magnitude suffix.
|
|
14
|
+
*/
|
|
15
|
+
export declare function abbreviateNumber(number: number): string | number;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generates a random integer between the provided minimum and maximum values (inclusive).
|
|
3
|
+
*
|
|
4
|
+
* @param {number} min - The minimum value for the random integer.
|
|
5
|
+
* @param {number} max - The maximum value for the random integer.
|
|
6
|
+
* @return {number} The randomly generated integer.
|
|
7
|
+
*/
|
|
8
|
+
export function getRandomInteger(min, max) {
|
|
9
|
+
return Math.floor(Math.random() * (max - min + 1)) + min;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Abbreviates a number by adding a suffix representing the magnitude.
|
|
13
|
+
*
|
|
14
|
+
* @param {number} number - The number to be abbreviated.
|
|
15
|
+
* @return {string|number} The abbreviated number with the magnitude suffix.
|
|
16
|
+
*/
|
|
17
|
+
export function abbreviateNumber(number) {
|
|
18
|
+
var SI_SYMBOL = ['', 'k', 'M', 'G', 'T', 'P', 'E'];
|
|
19
|
+
var tier = (Math.log10(Math.abs(number)) / 3) | 0;
|
|
20
|
+
if (tier == 0)
|
|
21
|
+
return number;
|
|
22
|
+
var suffix = SI_SYMBOL[tier];
|
|
23
|
+
var scale = Math.pow(10, tier * 3);
|
|
24
|
+
var scaled = number / scale;
|
|
25
|
+
return scaled.toFixed(1) + suffix;
|
|
26
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks if the given value is an object and not an array or null.
|
|
3
|
+
*
|
|
4
|
+
* @param {any} value - The value to check.
|
|
5
|
+
* @return {boolean} Returns true if the value is an object and not an array or null, false otherwise.
|
|
6
|
+
*/
|
|
7
|
+
export declare function isRealObject(value: any): boolean;
|
|
8
|
+
/**
|
|
9
|
+
* Determines if a given string is a valid JSON string representing an object.
|
|
10
|
+
*
|
|
11
|
+
* @param {string} str - The string to be checked.
|
|
12
|
+
* @return {boolean} Returns true if the string is a valid JSON string of an object, false otherwise.
|
|
13
|
+
*/
|
|
14
|
+
export declare const isStringifiedObject: (str: string) => boolean;
|
|
15
|
+
/**
|
|
16
|
+
* Recursively flattens a nested object into a single-level object with
|
|
17
|
+
* dot-separated property names.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* const obj = {
|
|
21
|
+
* a: {
|
|
22
|
+
* b: {
|
|
23
|
+
* c: 1
|
|
24
|
+
* }
|
|
25
|
+
* }
|
|
26
|
+
* }
|
|
27
|
+
* const flattened = flattenObject(obj) // { 'a.b.c': 1 }
|
|
28
|
+
*
|
|
29
|
+
* @param {object} obj The object to flatten.
|
|
30
|
+
* @param {string} [prefix=''] The prefix to use for the flattened property names.
|
|
31
|
+
* @returns {object} The flattened object.
|
|
32
|
+
*/
|
|
33
|
+
export declare const flattenObject: (obj: any, prefix?: string) => Record<string, any>;
|
|
34
|
+
/**
|
|
35
|
+
* Recursively unflattens a single-level object with dot-separated property
|
|
36
|
+
* names into a nested object.
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* const obj = { 'a.b.c': 1 }
|
|
40
|
+
* const unflattened = unflattenObject(obj) // { a: { b: { c: 1 } } }
|
|
41
|
+
*
|
|
42
|
+
* @param {object} obj The object to unflatten.
|
|
43
|
+
* @returns {object} The unflattened object.
|
|
44
|
+
*/
|
|
45
|
+
export declare const unflattenObject: (obj: Record<string, any>) => any;
|
|
46
|
+
/**
|
|
47
|
+
* Renames a key in an object.
|
|
48
|
+
*
|
|
49
|
+
* @param {Object} obj - The object that contains the key to rename.
|
|
50
|
+
* @param {string} oldKey - The old key name.
|
|
51
|
+
* @param {string} newKey - The new key name.
|
|
52
|
+
*/
|
|
53
|
+
export declare const renameKey: (obj: Record<string, any>, oldKey: string, newKey: string) => void;
|
|
54
|
+
/**
|
|
55
|
+
* Creates a new object with a specified key renamed, leaving the original object unchanged.
|
|
56
|
+
*
|
|
57
|
+
* @param {Record<string, any>} obj - The original object containing the key to rename.
|
|
58
|
+
* @param {string} oldKey - The name of the key to rename.
|
|
59
|
+
* @param {string} newKey - The new name for the key.
|
|
60
|
+
* @returns {Record<string, any>} A new object with the key renamed.
|
|
61
|
+
*/
|
|
62
|
+
export declare const renameKeyImmutable: (obj: Record<string, any>, oldKey: string, newKey: string) => Record<string, any>;
|
|
63
|
+
/**
|
|
64
|
+
* Omits the specified keys from the given object.
|
|
65
|
+
*
|
|
66
|
+
* @param {T} obj The object to omit keys from.
|
|
67
|
+
* @param {K[]} keys The keys to omit.
|
|
68
|
+
* @returns {Omit<T, K>} A new object with the specified keys omitted.
|
|
69
|
+
*/
|
|
70
|
+
export declare const omitKeys: <T, K extends keyof T>(obj: T, keys: K[]) => Omit<T, K>;
|
|
71
|
+
/**
|
|
72
|
+
* Checks if the given object is empty.
|
|
73
|
+
*
|
|
74
|
+
* An object is considered empty if it is an object instance (i.e. `obj.constructor === Object`), and if it has no own enumerable property (i.e. `Object.keys(obj).length === 0`).
|
|
75
|
+
*
|
|
76
|
+
* @param {any} obj - The object to check.
|
|
77
|
+
* @returns {boolean} Returns true if the object is empty, false otherwise.
|
|
78
|
+
*/
|
|
79
|
+
export declare const isObjectEmpty: (obj: any) => boolean;
|