Permalink
Browse files

Split api() into separate functions to get and set cookies (#454)

  • Loading branch information...
realityking authored and FagnerMartinsBrack committed Jul 3, 2018
1 parent 69f802b commit bf8bb9cb99ec9b1aa6e8ee4594823ebc6507d44c
Showing with 57 additions and 56 deletions.
  1. +57 −56 src/js.cookie.js
View
@@ -36,71 +36,74 @@
return result;
}
function decode (s) {
return s.replace(/(%[0-9A-Z]{2})+/g, decodeURIComponent);
}
function init (converter) {
function api (key, value, attributes) {
function api() {}
function set (key, value, attributes) {
if (typeof document === 'undefined') {
return;
}
// Write
if (arguments.length > 1) {
attributes = extend({
path: '/'
}, api.defaults, attributes);
if (typeof attributes.expires === 'number') {
attributes.expires = new Date(new Date() * 1 + attributes.expires * 864e+5);
}
attributes = extend({
path: '/'
}, api.defaults, attributes);
// We're using "expires" because "max-age" is not supported by IE
attributes.expires = attributes.expires ? attributes.expires.toUTCString() : '';
if (typeof attributes.expires === 'number') {
attributes.expires = new Date(new Date() * 1 + attributes.expires * 864e+5);
}
try {
var result = JSON.stringify(value);
if (/^[\{\[]/.test(result)) {
value = result;
}
} catch (e) {}
// We're using "expires" because "max-age" is not supported by IE
attributes.expires = attributes.expires ? attributes.expires.toUTCString() : '';
value = converter.write ?
converter.write(value, key) :
encodeURIComponent(String(value))
.replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g, decodeURIComponent);
try {
var result = JSON.stringify(value);
if (/^[\{\[]/.test(result)) {
value = result;
}
} catch (e) {}
key = encodeURIComponent(String(key))
.replace(/%(23|24|26|2B|5E|60|7C)/g, decodeURIComponent)
.replace(/[\(\)]/g, escape);
value = converter.write ?
converter.write(value, key) :
encodeURIComponent(String(value))
.replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g, decodeURIComponent);
var stringifiedAttributes = '';
for (var attributeName in attributes) {
if (!attributes[attributeName]) {
continue;
}
stringifiedAttributes += '; ' + attributeName;
if (attributes[attributeName] === true) {
continue;
}
key = encodeURIComponent(String(key))
.replace(/%(23|24|26|2B|5E|60|7C)/g, decodeURIComponent)
.replace(/[\(\)]/g, escape);
// Considers RFC 6265 section 5.2:
// ...
// 3. If the remaining unparsed-attributes contains a %x3B (";")
// character:
// Consume the characters of the unparsed-attributes up to,
// not including, the first %x3B (";") character.
// ...
stringifiedAttributes += '=' + attributes[attributeName].split(';')[0];
var stringifiedAttributes = '';
for (var attributeName in attributes) {
if (!attributes[attributeName]) {
continue;
}
stringifiedAttributes += '; ' + attributeName;
if (attributes[attributeName] === true) {
continue;
}
return (document.cookie = key + '=' + value + stringifiedAttributes);
// Considers RFC 6265 section 5.2:
// ...
// 3. If the remaining unparsed-attributes contains a %x3B (";")
// character:
// Consume the characters of the unparsed-attributes up to,
// not including, the first %x3B (";") character.
// ...
stringifiedAttributes += '=' + attributes[attributeName].split(';')[0];
}
// Read
return (document.cookie = key + '=' + value + stringifiedAttributes);
}
function get (key, json) {
if (typeof document === 'undefined') {
return;
}
var jar = {};
var decode = function (s) {
return s.replace(/(%[0-9A-Z]{2})+/g, decodeURIComponent);
};
// 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('; ') : [];
@@ -110,7 +113,7 @@
var parts = cookies[i].split('=');
var cookie = parts.slice(1).join('=');
if (!this.json && cookie.charAt(0) === '"') {
if (!json && cookie.charAt(0) === '"') {
cookie = cookie.slice(1, -1);
}
@@ -119,7 +122,7 @@
cookie = (converter.read || converter)(cookie, name) ||
decode(cookie);
if (this.json) {
if (json) {
try {
cookie = JSON.parse(cookie);
} catch (e) {}
@@ -136,17 +139,15 @@
return key ? jar[key] : jar;
}
api.set = api;
api.set = set;
api.get = function (key) {
return api.call(api, key);
return get(key, false /* read as raw */);
};
api.getJSON = function (key) {
return api.call({
json: true
}, key);
return get(key, true /* read as json */);
};
api.remove = function (key, attributes) {
api(key, '', extend(attributes, {
set(key, '', extend(attributes, {
expires: -1
}));
};

0 comments on commit bf8bb9c

Please sign in to comment.