-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 5aa5e77
Showing
5 changed files
with
297 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2021 Nathan B. (10Nates) | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
# MicroCookie | ||
|
||
[About MicroCookie](#about-microcookie) | [Installing MicroCookie](#installing-microcookie) | [Using MicroCookie](#using-microcookie) | ||
|
||
----- | ||
|
||
<br> | ||
|
||
## About MicroCookie | ||
|
||
<br> | ||
|
||
### What is MicroCookie? | ||
|
||
MicroCookie is a desert-bone-dry cookie management library designed to be so small you don't even notice it's there. | ||
|
||
<br> | ||
|
||
### Why another cookie manager? | ||
|
||
Every "simple" cookie manager I could find was kilobytes in size with a bunch of quality of life features that seem to miss the mark. The hardest part about managing cookies is the jump from string to storage. If that's taken care of, everything else is a breeze. | ||
|
||
<br> | ||
|
||
### NOTICE | ||
|
||
MicroCookie does not currently support paths. It may or may not support paths in the future. | ||
|
||
<br> | ||
|
||
## Installing MicroCookie | ||
|
||
<br> | ||
|
||
### Adding to your project | ||
|
||
MicroCookie is not currently designed for npm (even though I have the package.json) so the only way to import it normally. | ||
|
||
```html | ||
<!-- Stored locally --> | ||
<script src="microcookie.js"></script> | ||
|
||
<!-- Stored on my website (no garunteed reliability) --> | ||
<script src="https://almostd.one/pkg/microcookie-min.js"></script> | ||
|
||
<!-- jsDelivr (if I set it up properly) --> | ||
<script src="https://cdn.jsdelivr.net/gh/10Nates/[email protected]/microcookie-min.js"></script> | ||
``` | ||
|
||
<br> | ||
|
||
### Minimize MicroCookie yourself | ||
|
||
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\]` | ||
<br> | ||
|
||
## Using MicroCookie | ||
|
||
<br> | ||
|
||
### Fetching a cookie | ||
|
||
```js | ||
/** | ||
* @description Get a cookie | ||
* @param {string} key the cookie's identifier | ||
* @returns {string|undefined} value of key | ||
*/ | ||
MicroCookie.get(key); | ||
|
||
//example - get cookie "test" | ||
MicroCookie.get("test"); | ||
``` | ||
|
||
<br> | ||
|
||
### Making an expiration timestamp | ||
|
||
```js | ||
/** | ||
* @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) | ||
* @returns {number} The calculated unix timestamp | ||
*/ | ||
MicroCookie.makeExpiration(days, weeks, months, years); | ||
|
||
//example - expiration date for 1 month and 2 weeks | ||
MicroCookie.makeExpiration(undefined, 2, 1); | ||
``` | ||
|
||
<br> | ||
|
||
### Setting a cookie | ||
|
||
```js | ||
/** | ||
* @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) | ||
* @returns {string} the encoded cookie string (does not need to be used) | ||
*/ | ||
MicroCookie.set(key, value, expiration); | ||
|
||
//example - set cookie "test" with value "This is a test!" expiring in 1 day | ||
let expiration = MicroCookie.makeExpiration(1); | ||
MicroCookie.set("test", "This is a test!", expiration); | ||
``` | ||
|
||
<br> | ||
|
||
### Removing a cookie | ||
|
||
```js | ||
/** | ||
* @description Remove a cookie | ||
* @param {string} key key to be removed | ||
*/ | ||
MicroCookie.remove(key) | ||
|
||
//example - remove cookie "test" | ||
MicroCookie.remove("test") | ||
``` | ||
|
||
### Full examples | ||
|
||
A the time of writing, there is one full example, which is available in the [`test/`](./test/testpage.html) folder as [`test/testpage.html`](./test/testpage.html) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
const MicroCookie = { | ||
|
||
/** | ||
* @description Get a cookie | ||
* @param {string} key | ||
* @returns {string|undefined} value of key | ||
*/ | ||
get: function (key) { | ||
//split cookie string into separate cookies | ||
let cparse = document.cookie ? document.cookie.split(';') : [] | ||
for (i=0; i<cparse.length; i++) { | ||
//detect desired cookie | ||
if (cparse[i].startsWith(key + '=')) { | ||
return decodeURIComponent(cparse[i].split('=')[1]) | ||
} | ||
} | ||
return undefined | ||
}, | ||
|
||
/** | ||
* @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) | ||
* @returns {string} the encoded cookie string (does not need to be used) | ||
*/ | ||
set: function (key, value, expiration) { | ||
//convert timestamp to RSC spec | ||
let d = new Date() | ||
d.setTime(expiration * 1000) | ||
let exp = d.toUTCString() | ||
//encode value of key to prevent issues | ||
let setval = encodeURIComponent(value) | ||
//put it together | ||
let cookiestring = key + "=" + setval + "; expires=" + exp | ||
document.cookie = cookiestring | ||
return cookiestring | ||
}, | ||
|
||
/** | ||
* @description Remove a cookie | ||
* @param {string} key key to be removed | ||
*/ | ||
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) | ||
* @returns {number} The calculated unix timestamp | ||
*/ | ||
makeExpiration: function (days, weeks, months, years) { | ||
//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) | ||
return Math.floor(newtime) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"name": "microcookie", | ||
"version": "1.0.0", | ||
"description": "A minuscule cookie library. Nothing but bones. Does not currently support paths.", | ||
"main": "microcookie.js", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/10Nates/microcookie.git" | ||
}, | ||
"keywords": [ | ||
"cookie", | ||
"cookies", | ||
"manager", | ||
"library", | ||
"browser", | ||
"js", | ||
"client", | ||
"simple", | ||
"small", | ||
"tiny", | ||
"micro", | ||
"mini" | ||
], | ||
"author": "Nathan Hedge (10Nates)", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/10Nates/microcookie/issues" | ||
}, | ||
"homepage": "https://github.com/10Nates/microcookie#readme" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="UTF-8"> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>MicroCookie Test</title> | ||
<script src="../export/microcookie-min.js"></script> | ||
<script> | ||
|
||
function showCookie() { | ||
let cookie = 'test' | ||
|
||
alert(MicroCookie.get(cookie)) | ||
} | ||
|
||
function addCookie() { | ||
let cookie = 'test' | ||
let value = 'this is a test!' | ||
let days = 1 | ||
|
||
let expiration = MicroCookie.makeExpiration(days) | ||
MicroCookie.set(cookie, value, expiration) | ||
|
||
//show timestamp & new cookie values. | ||
console.log(expiration) | ||
console.log(document.cookie) | ||
} | ||
|
||
function deleteCookie() { | ||
let cookie = 'test' | ||
|
||
MicroCookie.remove(cookie) | ||
} | ||
|
||
</script> | ||
</head> | ||
|
||
<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> | ||
</body> | ||
|
||
</html> |