mirror of https://github.com/freeCodeCamp/devdocs
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
1.1 KiB
43 lines
1.1 KiB
6 years ago
|
class @CookiesStore
|
||
|
# Intentionally called CookiesStore instead of CookieStore
|
||
|
# Calling it CookieStore causes issues when the Experimental Web Platform features flag is enabled in Chrome
|
||
|
# Related issue: https://github.com/freeCodeCamp/devdocs/issues/932
|
||
|
|
||
9 years ago
|
INT = /^\d+$/
|
||
|
|
||
8 years ago
|
@onBlocked: ->
|
||
|
|
||
9 years ago
|
get: (key) ->
|
||
9 years ago
|
value = Cookies.get(key)
|
||
|
value = parseInt(value, 10) if value? and INT.test(value)
|
||
|
value
|
||
9 years ago
|
|
||
|
set: (key, value) ->
|
||
|
if value == false
|
||
9 years ago
|
@del(key)
|
||
|
return
|
||
9 years ago
|
|
||
9 years ago
|
value = 1 if value == true
|
||
7 years ago
|
value = parseInt(value, 10) if value and INT.test?(value)
|
||
9 years ago
|
Cookies.set(key, '' + value, path: '/', expires: 1e8)
|
||
8 years ago
|
@constructor.onBlocked(key, value, @get(key)) if @get(key) != value
|
||
9 years ago
|
return
|
||
9 years ago
|
|
||
|
del: (key) ->
|
||
9 years ago
|
Cookies.expire(key)
|
||
|
return
|
||
9 years ago
|
|
||
|
reset: ->
|
||
|
try
|
||
|
for cookie in document.cookie.split(/;\s?/)
|
||
|
Cookies.expire(cookie.split('=')[0])
|
||
|
return
|
||
|
catch
|
||
|
|
||
|
dump: ->
|
||
|
result = {}
|
||
9 years ago
|
for cookie in document.cookie.split(/;\s?/) when cookie[0] isnt '_'
|
||
9 years ago
|
cookie = cookie.split('=')
|
||
|
result[cookie[0]] = cookie[1]
|
||
|
result
|