/
utils.js
80 lines (70 loc) · 2.37 KB
/
utils.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
export function resizeInterval() {
// Resize very 200ms
function resizeWindow() {
window.dispatchEvent(new Event("resize"));
}
var interval = setInterval(resizeWindow, 200);
// Clear interval after 2 seconds
setTimeout(function () {
clearInterval(interval);
}, 2000);
}
export function getTagValue(tags, tag, join = "") {
if (!tags) {
return;
}
let exists = tags.filter((tag_) => tag_.startsWith(`${tag}=`));
if (exists.length > 0) {
return exists.map((cls) => cls.replace(`${tag}=`, "")).join(join);
}
return "";
}
export function slugify(string) {
const a =
"àáâäæãåāăąçćčđďèéêëēėęěğǵḧîïíīįìłḿñńǹňôöòóœøōõőṕŕřßśšşșťțûüùúūǘůűųẃẍÿýžźż·/_,:;";
const b =
"aaaaaaaaaacccddeeeeeeeegghiiiiiilmnnnnoooooooooprrsssssttuuuuuuuuuwxyyzzz------";
const p = new RegExp(a.split("").join("|"), "g");
if (string) {
return string
.toString()
.toLowerCase()
.replace(/\s+/g, "-") // Replace spaces with -
.replace(p, (c) => b.charAt(a.indexOf(c))) // Replace special characters
.replace(/&/g, "-and-") // Replace & with 'and'
.replace(/[^\w-]+/g, "") // Remove all non-word characters
.replace(/--+/g, "-") // Replace multiple - with single -
.replace(/^-+/, "") // Trim - from start of text
.replace(/-+$/, ""); // Trim - from end of text
}
return "";
}
export function createMarkup(html) {
return { __html: html };
}
export function uuidv4() {
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (
c
) {
var r = (Math.random() * 16) | 0,
v = c == "x" ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
}
export function onNextFrame(callback) {
setTimeout(function () {
window.requestAnimationFrame(callback);
});
}
export function insertItemInArray(array, func) {
return array.flatMap((value, index, array) =>
array.length - 1 !== index // check for the last item
? [value, func(index)]
: value
);
// return array.flatMap((value, index, array) =>
// array.length - 1 !== index // check for the last item
// ? [value, item]
// : value
// );
}