backendless-console-sdk
Version:
Backendless Console SDK for Node.js and browser
42 lines (30 loc) • 958 B
JavaScript
export const cookieEnabled = () => typeof window !== 'undefined' && navigator && navigator.cookieEnabled
export function getCookie(name) {
const matches = document.cookie.match(new RegExp(
'(?:^|; )' + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + '=([^;]*)'
))
return matches ? decodeURIComponent(matches[1]) : undefined
}
export function setCookie(name, value, options = {}) {
options = {
path: '/',
...options
}
if (options.expires instanceof Date) {
options.expires = options.expires.toUTCString()
}
let updatedCookie = encodeURIComponent(name) + '=' + encodeURIComponent(value)
for (const optionKey in options) {
updatedCookie += `; ${optionKey}`
const optionValue = options[optionKey]
if (optionValue !== true) {
updatedCookie += `=${optionValue}`
}
}
document.cookie = updatedCookie
}
export function deleteCookie(name) {
setCookie(name, '', {
'max-age': -1
})
}