-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon-helpers.js
446 lines (378 loc) · 11.3 KB
/
common-helpers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/**
* Returns a random integer between min (inclusive) and max (inclusive)
* @param {number} min - The minimum value
* @param {number} max - The maximum value
*/
export function randomIntFromInterval(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
/**
* For browsers that do not support str.replaceAll function, replaces all occurrences of a substring in a string
* @param {string} str - The string to search in
* @param {string} find - The substring to search for
* @param {string} replace - The substring to replace find with
* @returns - The string with all occurrences of find replaced with replace
*/
export function replaceAll(str, find, replace) {
if(!str) {
return "";
}
if(str.indexOf(find) < 0) {
return str;
}
return str.replace(new RegExp(escapeRegExp(find), "g"), replace);
}
function escapeRegExp(str) {
return str.replace(/([.*+?^=!:${}()|[]\/\\])/g, "\\$1");
}
/**
* Generates a GUID
* @returns - A GUID
*/
export function guid() {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
return s4() + s4() + "-" + s4() + "-" + s4() + "-" + s4() + "-" + s4() + s4() + s4();
}
/**
* Returns a random element from an array
* @param {Array} arr - The array to get a random element from
* @returns - A random element from the array
*/
export function randomArrayElement(arr) {
if(!arr || arr.length === 0) {
return null;
}
return arr[randomIntFromInterval(0, arr.length - 1)];
}
/**
* Returns the file (or directory) name from a path, platform agnostic
* @param {string} path - The path to get the filename from
* @returns - The filename
*/
export function filenameFromPath(path) {
let sep = "/";
if(path.indexOf(sep) == -1) {
sep = "\\";
}
if(path.indexOf(sep) == -1) {
return path;
}
const parts = path.split(sep);
return parts[parts.length - 1];
}
/**
* Creates an element with optional classes, content, color, and callback
* @param {string} tag - The tag name of the element
* @param {string} classes - The class name to add to the element
* @param {string} content - The content of the element
* @param {string} color - The color of the element
* @param {function} callback - The callback function for the element
* @returns - The created element
* */
export function createClassedElement(tag, classes, content, color, callback) {
if(!tag) {
return null;
}
const ele = document.createElement(tag);
if(classes) {
ele.className = classes;
}
if(content) {
ele.innerHTML = content;
}
if(color) {
ele.style.color = color;
}
if(callback) {
ele.onclick = callback;
}
return ele;
}
/**
* Returns a color based on a percentage
* @param {number} pct - The percentage to get the color for (between 0 and 1)
* @param {array} percentColors - The colors to use for the percentage (optional)
* @returns {string} - The hex color for the percentage
*/
export function getColorForPercentage(pct, percentColors) {
if(!percentColors) {
percentColors = [
{ pct: 0.0, color: { r: 0xff, g: 0x00, b: 0 } },
{ pct: 0.5, color: { r: 0xff, g: 0xff, b: 0 } },
{ pct: 1.0, color: { r: 0x00, g: 0xff, b: 0 } }
];
}
let iVal = 2;
for (let i = 1; i < percentColors.length - 1; i++) {
if (pct < percentColors[i].pct) {
iVal = i;
break;
}
}
let lower = percentColors[iVal - 1];
let upper = percentColors[iVal];
let range = upper.pct - lower.pct;
let rangePct = (pct - lower.pct) / range;
let pctLower = 1 - rangePct;
let pctUpper = rangePct;
let color = {
r: Math.floor(lower.color.r * pctLower + upper.color.r * pctUpper),
g: Math.floor(lower.color.g * pctLower + upper.color.g * pctUpper),
b: Math.floor(lower.color.b * pctLower + upper.color.b * pctUpper)
};
return rgbToHex(color.r, color.g, color.b);
}
/**
* Converts an RGB color to a hex color
* @param {number} r - The red value
* @param {number} g - The green value
* @param {number} b - The blue value
* @returns {string} - The hex color
*/
export function rgbToHex(r, g, b) {
return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b);
}
function componentToHex(c) {
let hex = c.toString(16);
return hex.length == 1 ? "0" + hex : hex;
}
/**
* Returns a random hex color
* @param {number} maxLightness - The maximum lightness value
* @param {number} minLightness - The minimum lightness value
* @returns {string} - The random hex color
*/
export function randomHexColor(maxLightness,minLightness) {
if(!maxLightness) {
maxLightness = 255;
}
if(!minLightness) {
minLightness = 0;
}
let r = randomIntFromInterval(minLightness,maxLightness);
let g = randomIntFromInterval(minLightness,maxLightness);
let b = randomIntFromInterval(minLightness,maxLightness);
return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b);
}
/**
* Converts a hex color to an RGB color
* @param {string} hex - The hex color
* @returns {object} - The RGB color
*/
export function hexToRGB(hex) {
let result;
if (hex.length == 6 || hex.length == 7) {
result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
a: 255,
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : null;
}
if (hex.length == 8 || hex.length == 9) {
result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
a: parseInt(result[1], 16),
r: parseInt(result[2], 16),
g: parseInt(result[3], 16),
b: parseInt(result[4], 16)
} : null;
}
return {
a: 255,
r: 0,
g: 0,
b: 0
};
}
/**
* Abbreviates a number
* @param {number} number - The number to abbreviate
* @param {number} maxPlaces - The maximum number of decimal places
* @param {number} forcePlaces - The number of decimal places to
* @param {string} forceLetter - The letter to force the abbreviation to
* @returns {string} - The abbreviated number
* */
export function abbreviateNumber(number, maxPlaces, forcePlaces, forceLetter) {
number = Number(number);
forceLetter = forceLetter || false;
if(forceLetter !== false) {
return annotateNumber(number, maxPlaces, forcePlaces, forceLetter);
}
let abbr;
if(number >= 1e18) {
abbr = "Q";
}
else if(number >= 1e15) {
abbr = "q";
}
else if(number >= 1e12) {
abbr = "T";
}
else if(number >= 1e9) {
abbr = "B";
}
else if(number >= 1e6) {
abbr = "M";
}
else if(number >= 1e3) {
abbr = "K";
}
else {
abbr = "";
}
return annotateNumber(number, maxPlaces, forcePlaces, abbr);
}
/**
* Annotates a number
* @param {number} number - The number to annotate
* @param {number} maxPlaces - The maximum number of decimal places
* @param {number} forcePlaces - The number of decimal places to
* @param {string} abbr - The abbreviation to use
* @returns {string} - The annotated number
* */
export function annotateNumber(number, maxPlaces, forcePlaces, abbr) {
// set places to false to not round
let rounded = 0;
switch(abbr) {
case "Q":
rounded = number / 1e18;
break;
case "q":
rounded = number / 1e15;
break;
case "T":
rounded = number / 1e12;
break;
case "B":
rounded = number / 1e9;
break;
case "M":
rounded = number / 1e6;
break;
case "K":
rounded = number / 1e3;
break;
case "":
rounded = number;
break;
}
if(maxPlaces !== false) {
let test = new RegExp("\\.\\d{" + (maxPlaces + 1) + ",}$");
if(test.test(("" + rounded))) {
rounded = rounded.toFixed(maxPlaces);
}
}
if(forcePlaces !== false) {
rounded = Number(rounded).toFixed(forcePlaces);
}
return rounded + abbr;
}
/**
* Returns a human-readable time ago string
* @param {number} time - The time to convert to a time ago string
* @returns {string} - The time ago string
*/
export function getTimeAgo(time) {
const now = new Date();
const diff = now - new Date(time);
const seconds = Math.floor(diff / 1000);
const minutes = Math.floor(seconds / 60);
const hours = Math.floor(minutes / 60);
const days = Math.floor(hours / 24);
const weeks = Math.floor(days / 7);
const months = Math.floor(days / 30);
const years = Math.floor(months / 12);
if (years > 0) {
return years + " year" + (years > 1 ? "s" : "") + " ago";
}
if (months > 0) {
return months + " month" + (months > 1 ? "s" : "") + " ago";
}
if (weeks > 0) {
return weeks + " week" + (weeks > 1 ? "s" : "") + " ago";
}
if (days > 0) {
return days + " day" + (days > 1 ? "s" : "") + " ago";
}
if (hours > 0) {
return hours + " hour" + (hours > 1 ? "s" : "") + " ago";
}
if (minutes > 0) {
return minutes + " minute" + (minutes > 1 ? "s" : "") + " ago";
}
if (seconds > 0) {
return seconds + " second" + (seconds > 1 ? "s" : "") + " ago";
}
return "just now";
}
/**
* Returns the distance between two points
* @param {number} x1 - The x-coordinate of the first point
* @param {number} y1 - The y-coordinate of the first point
* @param {number} x2 - The x-coordinate of the second point
* @param {number} y2 - The y-coordinate of the second point
* @returns {number} - The distance between the two points
*/
export function distBetweenPoints(x1, y1, x2, y2) {
return Math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2);
}
/**
* Splits a string into chunks of a specified length
* @param {string} str - The string to split
* @param {number} length - The length of each chunk
* @returns {Array} - An array of chunks
*/
export function chunkString(str, length) {
return str.match(new RegExp(".{1," + length + "}", "g"));
}
/**
* Converts a data URL to a Blob
* @param {string} dataurl - The data URL to convert
* @returns {Blob} - The Blob
*/
export function dataURLtoBlob(dataurl) {
let arr = dataurl.split(","), mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
while(n--){
u8arr[n] = bstr.charCodeAt(n);
}
return new Blob([u8arr], {type:mime});
}
/**
* Removes a value from an array
* @param {Array} arr - The array to remove the value from
* @param {any} value - The value to remove
* @returns {Array} - The array with the value removed
*/
export function removeFromArray(arr, value) {
const index = arr.indexOf(value);
if (index > -1) {
arr.splice(index, 1);
}
return arr;
}
export default {
randomIntFromInterval,
replaceAll,
guid,
randomArrayElement,
filenameFromPath,
createClassedElement,
getColorForPercentage,
rgbToHex,
randomHexColor,
hexToRGB,
abbreviateNumber,
annotateNumber,
getTimeAgo,
distBetweenPoints,
chunkString,
dataURLtoBlob,
removeFromArray
};