-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpico-cookie.js
36 lines (35 loc) · 967 Bytes
/
pico-cookie.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
const cookie = (() => {
const encode = encodeURIComponent
const decode = decodeURIComponent
return Object.defineProperty(
new Proxy(
Object.fromEntries(
document.cookie.split(';').map(
entry => entry.split('=').map(e => decode(e.trim()))
)
),
{
set(_, key, value) {
document.cookie = `${ encode(key) }=${ encode(value) }`
return Reflect.set(...arguments)
},
deleteProperty(_, key) {
document.cookie = `${ encode(key) }=; max-age=0`
return Reflect.deleteProperty(...arguments)
}
}
),
'put',
{
value(key, value, attributes) {
document.cookie = `${ encode(key) }=${ encode(value) }` + (
attributes ? '; ' + Object.entries(attributes).map(
pair => pair.map(p => encode(p)).join('=')
).join('; ') : ''
)
this[key] = value
return this
}
}
)
})()