-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrowser.js
More file actions
142 lines (134 loc) · 3.6 KB
/
browser.js
File metadata and controls
142 lines (134 loc) · 3.6 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
/**
*
* @param {string} string
* @param {string} storage
* @param {any} value
* @returns
*/
const validation = (string, storage = "local", value = undefined) => {
if (storage === "local") {
if (
localStorage.getItem(string) !== undefined &&
localStorage.getItem(string) !== "undefined" &&
localStorage.getItem(string) !== null
) {
if (
value === undefined ||
(value !== undefined && localStorage.getItem(string) === value)
)
return true;
return false;
}
} else if (storage === "session") {
if (
sessionStorage.getItem(string) !== undefined &&
sessionStorage.getItem(string) !== "undefined" &&
sessionStorage.getItem(string) !== null
) {
if (
value === undefined ||
(value !== undefined && sessionStorage.getItem(string) === value)
)
return true;
return false;
}
}
return false;
};
/**
*
* @param {string} queries
*/
const parseQueries = (queries) => {
const toReturn = {};
const queryParams = queries.substring(1).split("&");
queryParams.forEach((item) => {
const [param, value] = item.split("=");
toReturn[param] = value;
});
return toReturn;
};
/**
*
* @param {string} cookie
* @returns
*/
const getUserLanguage = (cookie = "") => {
if (getCookie(cookie) && cookie.length) return getCookie(cookie);
else {
let userLang = navigator.language || navigator.userLanguage;
if (userLang.indexOf("en") < 0 && userLang.indexOf("es") < 0)
userLang = "en-US";
userLang = userLang.split("-")[0];
if (userLang) {
if (cookie.length) createCookie(cookie, 730, userLang);
return userLang;
}
}
return "en";
};
/**
* Scroll to a target position, default the top of the page.
* @param {number} [targetY=0] - The target top position to scroll to.
* @param {number} [targetX=0] - The target left position to scroll to.
* @param {Node} [dealer=window] - The element who triggers the scroll.
* @param {string} [behavior="smooth"] - Scroll behavior
*/
const scrollTo = (
targetY = 0,
targetX = 0,
dealer = window,
behavior = "smooth"
) =>
dealer.scroll({
top: targetY,
left: targetX,
behavior: behavior,
});
/**
* Takes a name, expiration, and value, and creates a cookie with those values
* @param {string} name - The name of the cookie
* @param {any} days - number of days
* @param {any} value - The value of the cookie.
*/
const createCookie = (name, days, value, path ="/", sameSite = "Lax") => {
var date = new Date();
date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
const expires = "; expires=" + date.toUTCString();
document.cookie = `${name}=${value || ""}${expires};path=${path};SameSite=${sameSite}`;
};
/**
* Takes a cookie name as an argument and returns the value of that cookie
* @param {string} cname - The name of the cookie you want to get.
* @returns {any} The value of the cookie with the name cname.
*/
const getCookie = (cname) => {
const name = `${cname}=`;
const decodedCookie = decodeURIComponent(document.cookie);
const ca = decodedCookie.split(";");
for (let i = 0; i < ca.length; i += 1) {
let c = ca[i];
while (c.charAt(0) === " ") {
c = c.substring(1);
}
if (c.indexOf(name) === 0) {
return c.substring(name.length, c.length);
}
}
return "";
};
/**
* Removes a cookie
* @param {string} name
*/
const deleteCookie = (name) =>
(document.cookie = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;`);
module.exports = {
getCookie,
createCookie,
deleteCookie,
getUserLanguage,
scrollTo,
parseQueries,
validation
};