-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
49 lines (40 loc) · 1.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
export function isObject(value) {
return value?.constructor === Object;
}
export function setCookie(name, value) {
const expires = new Date();
expires.setMonth(expires.getMonth() + 13);
document.cookie = `${name}=${value}; path=/; expires=${expires.toUTCString()}`;
}
export function getCookie(name) {
const value = `; ${document.cookie}`;
const parts = value.split(`; ${name}=`);
if (parts.length === 2) {
return parts.pop().split(';').shift();
}
return null;
}
export function getAllCookies() {
const cookies = document.cookie.split(';')
.map(cookie => cookie.trim())
.map(cookie => cookie.split(';')[0].split('=')[0]);
return cookies;
}
export function removeCookie(cookie) {
const expires = new Date(0).toUTCString();
const domains = window.location.hostname.split('.')
.map((part, idx, parts) => parts.slice(idx).join('.'));
const paths = window.location.pathname.split('/')
.map((part, idx, parts) => parts.slice(0, idx + 1).join('/') || '/');
for (const domain of domains) {
for (const path of paths) {
const cookieRemovalString = `${encodeURIComponent(cookie)}=; expires=${expires}; domain=${domain}; path=${path}`
document.cookie = cookieRemovalString;
}
}
}
export function gtag() {
window.dataLayer = window.dataLayer || [];
window.dataLayer.push(arguments);
console.debug('gtag', ...arguments);
}