-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.js
33 lines (27 loc) · 1008 Bytes
/
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
export const getRandomIntInclusive = (min, max) => {
min = Math.ceil(min)
max = Math.floor(max)
return Math.floor(Math.random() * (max - min + 1)) + min
}
export const setCookie = (name, value, daysToLive) => {
const date = new Date()
date.setTime(date.getTime() + daysToLive * 24 * 60 * 60 * 1000)
const expires = `expires=${date.toUTCString()}`
document.cookie = `${name}=${value};${expires};path=/`
}
export const getCookie = (name) => {
// Split cookie string and get all individual name=value pairs in an array
var cookieArr = document.cookie.split(';')
// Loop through the array elements
for (var i = 0; i < cookieArr.length; i++) {
var cookiePair = cookieArr[i].split('=')
/* Removing whitespace at the beginning of the cookie name
and compare it with the given string */
if (name == cookiePair[0].trim()) {
// Decode the cookie value and return
return decodeURIComponent(cookiePair[1])
}
}
// Return null if not found
return null
}