Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added UI/assets/.DS_Store
Binary file not shown.
Binary file added UI/assets/js/.DS_Store
Binary file not shown.
91 changes: 91 additions & 0 deletions UI/assets/js/cookies_lib/api.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import assign from './assign.mjs'
import defaultConverter from './converter.mjs'

function init (converter, defaultAttributes) {
function set (key, value, attributes) {
if (typeof document === 'undefined') {
return
}

attributes = assign({}, defaultAttributes, attributes)

if (typeof attributes.expires === 'number') {
attributes.expires = new Date(Date.now() + attributes.expires * 864e5)
}
if (attributes.expires) {
attributes.expires = attributes.expires.toUTCString()
}

key = defaultConverter.write(key).replace(/=/g, '%3D')

value = converter.write(String(value), key)

var stringifiedAttributes = ''
for (var attributeName in attributes) {
if (!attributes[attributeName]) {
continue
}

stringifiedAttributes += '; ' + attributeName

if (attributes[attributeName] === true) {
continue
}

stringifiedAttributes += '=' + attributes[attributeName].split(';')[0]
}

return (document.cookie = key + '=' + value + stringifiedAttributes)
}

function get (key) {
if (typeof document === 'undefined' || (arguments.length && !key)) {
return
}

// To prevent the for loop in the first place assign an empty array
// in case there are no cookies at all.
var cookies = document.cookie ? document.cookie.split('; ') : []
var jar = {}
for (var i = 0; i < cookies.length; i++) {
var parts = cookies[i].split('=')
var value = parts.slice(1).join('=')
var foundKey = defaultConverter.read(parts[0]).replace(/%3D/g, '=')
jar[foundKey] = converter.read(value, foundKey)

if (key === foundKey) {
break
}
}

return key ? jar[key] : jar
}

return Object.create(
{
set: set,
get: get,
remove: function (key, attributes) {
set(
key,
'',
assign({}, attributes, {
expires: -1
})
)
},
withAttributes: function (attributes) {
return init(this.converter, assign({}, this.attributes, attributes))
},
withConverter: function (converter) {
return init(assign({}, this.converter, converter), this.attributes)
}
},
{
attributes: { value: Object.freeze(defaultAttributes) },
converter: { value: Object.freeze(converter) }
}
)
}

export default init(defaultConverter, { path: '/' })
9 changes: 9 additions & 0 deletions UI/assets/js/cookies_lib/assign.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default function (target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i]
for (var key in source) {
target[key] = source[key]
}
}
return target
}
8 changes: 8 additions & 0 deletions UI/assets/js/cookies_lib/converter.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default {
read: function (value) {
return value.replace(/%3B/g, ';')
},
write: function (value) {
return value.replace(/;/g, '%3B')
}
}
Loading