forked from sendbird/sendbird-javascript-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
179 lines (154 loc) · 4.49 KB
/
Copy pathutils.js
File metadata and controls
179 lines (154 loc) · 4.49 KB
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
import moment from 'moment';
export const timestampToTime = timestamp => {
const now = new Date().getTime();
const nowDate = moment.unix(now.toString().length === 13 ? now / 1000 : now).format('MM/DD');
let date = moment.unix(timestamp.toString().length === 13 ? timestamp / 1000 : timestamp).format('MM/DD');
if (date === 'Invalid date') {
date = '';
}
return nowDate === date
? moment.unix(timestamp.toString().length === 13 ? timestamp / 1000 : timestamp).format('HH:mm')
: date;
};
export const timestampToDateString = timestamp => {
return moment.unix(timestamp.toString().length === 13 ? timestamp / 1000 : timestamp).format('LL');
};
export const timestampFromNow = timestamp => {
return moment(timestamp).fromNow();
};
export const isUrl = urlString => {
const regex = /^(http|https):\/\/[^ "]+$/;
return regex.test(urlString);
};
export const isImage = fileType => {
const regex = /^image\/.+$/;
return regex.test(fileType);
};
export const isEmpty = value => {
return value === null || value === undefined || value.length === 0;
};
export const isNull = value => {
try {
return value === null;
} catch (e) {
return false;
}
};
export const setCookie = (key, value) => {
document.cookie = `${key}=${value}; expires=Fri, 31 Dec 9999 23:59:59 GMT`;
};
export const getCookie = key => {
let name = `${key}=`;
let ca = document.cookie.split(';');
for (let i = 0; i < ca.length; i++) {
let c = ca[i];
if (!c) continue;
while (c.charAt(0) === ' ') {
c = c.substring(1);
}
if (c.indexOf(name) === 0) {
return c.substring(name.length, c.length);
}
}
return '';
};
export const getVariableFromUrl = () => {
let vars = {};
let hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for (let i = 0; i < hashes.length; i++) {
let hash = hashes[i].split('=');
vars[hash[0]] = hash[1];
}
return vars;
};
export const errorAlert = (message, reload = true) => {
alert(message);
if (reload) {
location.reload(true);
}
};
export const redirectToIndex = message => {
if (message) {
errorAlert(message, false);
}
window.location.href = 'index.html';
};
export const setDataInElement = (target, key, data) => {
target.dataset[`${key}`] = data;
};
export const getDataInElement = (target, key) => {
return target.dataset[`${key}`];
};
export const createDivEl = ({ id, className, content, background }) => {
const el = document.createElement('div');
if (id) {
el.id = id;
}
if (className) {
el.className = Array.isArray(className) ? className.join(' ') : className;
}
if (content) {
el.innerHTML = content;
}
if (background) {
el.style.backgroundImage = `url(${background})`;
}
return el;
};
export const isScrollBottom = target => {
return target.scrollTop + target.offsetHeight >= target.scrollHeight;
};
export const appendToFirst = (target, newElement) => {
if (target.childNodes.length > 0) {
target.insertBefore(newElement, target.childNodes[0]);
} else {
target.appendChild(newElement);
}
};
const hasClass = (target, className) => {
return target.classList
? target.classList.contains(className)
: new RegExp('(^| )' + className + '( |$)', 'gi').test(target.className);
};
export const addClass = (target, className) => {
if (target.classList) {
if (!(className in target.classList)) {
target.classList.add(className);
}
} else {
if (target.className.indexOf(className) < 0) {
target.className += ` ${className}`;
}
}
};
export const removeClass = (target, className) => {
if (target.classList) {
target.classList.remove(className);
} else {
target.className = target.className.replace(
new RegExp('(^|\\b)' + className.split(' ').join('|') + '(\\b|$)', 'gi'),
''
);
}
};
export const toggleClass = (target, className) => {
hasClass(target, className) ? removeClass(target, className) : addClass(target, className);
};
export const uuid4 = () => {
let d = new Date().getTime();
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
const r = ((d + Math.random() * 16) % 16) | 0;
d = Math.floor(d / 16);
return (c === 'x' ? r : (r & 0x3) | 0x8).toString(16);
});
};
export const protectFromXSS = text => {
return typeof text === 'string'
? text
.replace(/\&/g, '&')
.replace(/\</g, '<')
.replace(/\>/g, '>')
.replace(/\"/g, '"')
.replace(/\'/g, ''')
: text;
};