Skip to content

Commit

Permalink
fixed issue with multiple cookies, made evene smaller, updated README
Browse files Browse the repository at this point in the history
  • Loading branch information
10Nates committed Jan 3, 2022
1 parent cce13bf commit 9e65d68
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 31 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ Every "simple" cookie manager I could find was kilobytes in size with a bunch of

<br>

### Why do you use "expires" instead of "max-age"?

It boils down to compatibility. Not every browser supports max-age, and some browsers throw a fit if both are given. Does it really matter in this day and age? Not really, but I've already made my decision unless something big happens.

<br>

### NOTICE

MicroCookie does not currently support paths. It may or may not support paths in the future.
Expand Down Expand Up @@ -50,7 +56,7 @@ MicroCookie is not currently designed for npm (even though I have the package.js

The arguments I used for uglifyjs were

> `uglifyjs microcookie.js -o export/microcookie-min.js -m reserved=\[key,value,path,expiration,days,weeks,months,years\]`
> `uglifyjs microcookie.js -o export/microcookie-min.js -m reserved=\[key,value,path,exp,d,w,m,y\] --comments -c passes=3`
<br>

Expand Down
2 changes: 2 additions & 0 deletions export/microcookie-min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 17 additions & 16 deletions microcookie.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/** @preserve https://github.com/10Nates/microcookie */

const MicroCookie = {

/**
Expand All @@ -7,7 +9,7 @@ const MicroCookie = {
*/
get: function (key) {
//split cookie string into separate cookies
let cparse = document.cookie ? document.cookie.split(';') : []
let cparse = document.cookie ? document.cookie.split(/; ?/g) : []
for (i=0; i<cparse.length; i++) {
//detect desired cookie
if (cparse[i].startsWith(key + '=')) {
Expand All @@ -20,19 +22,19 @@ const MicroCookie = {
/**
* @description Set a cookie
* @param {string} key to prevent issues, only use alphanumeric characters
* @param {string} value the value the key will be set to
* @param {number} expiration Unix timestamp (seconds)
* @param {string} val the value the key will be set to
* @param {number} exp Unix timestamp (seconds)
* @returns {string} the encoded cookie string (does not need to be used)
*/
set: function (key, value, expiration) {
set: function (key, val, exp) {
//convert timestamp to RSC spec
let d = new Date()
d.setTime(expiration * 1000)
let exp = d.toUTCString()
d.setTime(exp * 1000)
let expString = d.toUTCString()
//encode value of key to prevent issues
let setval = encodeURIComponent(value)
let setval = encodeURIComponent(val)
//put it together
let cookiestring = key + "=" + setval + "; expires=" + exp
let cookiestring = key + "=" + setval + "; expires=" + expString
document.cookie = cookiestring
return cookiestring
},
Expand All @@ -44,22 +46,21 @@ const MicroCookie = {
remove: function (key) {
//set cookie to expired date
document.cookie = key + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC";
return true
},

/**
* @description craft a unix timestamp usable with the add function
* @param {number} days from current date
* @param {number} weeks from current date (7 days)
* @param {number} months from current date (30.4375 days)
* @param {number} years from current date (365.25 days) (going beyond 2038 is incompatible with 32 bit devices)
* @param {number} d days from current date
* @param {number} w weeks from current date (7 days)
* @param {number} m months from current date (30.4375 days)
* @param {number} y years from current date (365.25 days) (going beyond 2038 is incompatible with 32 bit devices)
* @returns {number} The calculated unix timestamp
*/
makeExpiration: function (days, weeks, months, years) {
makeExpiration: function (d, w, m, y) {
//milliseconds -> seconds
let nowsecs = Math.floor(Date.now() / 1000)
// secs in a day secs in a week secs in 30.4375 days secs in 365.25 days
let newtime = nowsecs + (days ? days * 86400 : 0) + (weeks ? weeks * 604800 : 0) + (months ? months * 2629800 : 0) + (years ? years * 31557600 : 0)
// secs in a day secs in a week secs in 30.4375 days secs in 365.25 days
let newtime = nowsecs + (d ? d * 86400 : 0) + (w ? w * 604800 : 0) + (m ? m * 2629800 : 0) + (y ? y * 31557600 : 0)
return Math.floor(newtime)
}

Expand Down
25 changes: 11 additions & 14 deletions test/testpage.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,11 @@
<script src="../export/microcookie-min.js"></script>
<script>

function showCookie() {
let cookie = 'test'

function showCookie(cookie) {
alert(MicroCookie.get(cookie))
}

function addCookie() {
let cookie = 'test'
let value = 'this is a test!'
let days = 1

function addCookie(cookie, value, days) {
let expiration = MicroCookie.makeExpiration(days)
MicroCookie.set(cookie, value, expiration)

Expand All @@ -28,9 +22,7 @@
console.log(document.cookie)
}

function deleteCookie() {
let cookie = 'test'

function deleteCookie(cookie) {
MicroCookie.remove(cookie)
}

Expand All @@ -39,9 +31,14 @@

<body>
<p>This page is an test integration for the MicroCookie JS library.</p>
<button onclick="showCookie()">Show "test" cookie</button>
<button onclick="addCookie()">Add "test" cookie expiring in 1 day</button>
<button onclick="deleteCookie()">Delete "test" cookie</button>
<button onclick="showCookie('test')">Show "test" cookie</button>
<button onclick="showCookie('test2')">Show "test2" cookie</button>
<br>
<button onclick="addCookie('test', 'this is a test!', 1)">Add "test" cookie expiring in 1 day</button>
<button onclick="addCookie('test2', 'this is another test!', 2)">Add "test2" cookie expiring in 2 days</button>
<br>
<button onclick="deleteCookie('test')">Delete "test" cookie</button>
<button onclick="deleteCookie('test2')">Delete "test2" cookie</button>
</body>

</html>

0 comments on commit 9e65d68

Please sign in to comment.