Skip to content

Commit

Permalink
increased compatibility to 100%, prepared for README for npm, 5 bytes…
Browse files Browse the repository at this point in the history
… smaller
  • Loading branch information
10Nates committed Jan 3, 2022
1 parent 1835578 commit f9fae74
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 41 deletions.
84 changes: 68 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[About MicroCookie](#about-microcookie) | [Installing MicroCookie](#installing-microcookie) | [Using MicroCookie](#using-microcookie)

-----
---

<br>

Expand All @@ -12,7 +12,7 @@

### 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.
MicroCookie is a desert-bone-dry cookie management library designed to be so small you don't even notice it's there. It's also [100% compatible.](https://seedmanc.github.io/jscc/)

<br>

Expand All @@ -28,23 +28,71 @@ It boils down to compatibility. Not every browser supports max-age, and some bro

<br>

### NOTICE
### <span style="color:red">NOTICE</span>

MicroCookie does not currently support paths. It may or may not support paths in the future.
- MicroCookie does not currently support paths. It may or may not support paths in the future.
- npm is probably the least convenient way to use this library.
- All instructions assume you are running a Unix-based operating system. This probably won't matter if you aren't using npm.

<br>

## Installing MicroCookie

<br>

### Adding to your project
### Note for npm users

MicroCookie is not currently designed for npm (even though I have the package.json) so the only way to import it normally.
MicroCookie is not currently designed for browserify, webpack, or similar programs because it is already designed for the web. As such, you cannot import MicroCookie as a module.

<br>

### Standard

Either download microcookie-min.js in the [releases tab](https://github.com/10Nates/microcookie/releases), or use one of the external sources already provided.

[Including MicroCookie in your project](#including-microcookie-in-your-project)

<br>

### Using npm

```shell
npm install uglify-js -g
npm install microcookie
```

npm will automatically run the minimizing script, but if it doesn't, refer to [Minimizing MicroCookie](#minimizing-microcookie)

[Including MicroCookie in your project](#including-microcookie-in-your-project)

<br>

### Minimizing MicroCookie

The arguments used in `npm run-script ugly` are

> ```shell
> npm install uglify-js -g
> if [ ! -d export ]; then mkdir export
> uglifyjs microcookie.js -o export/microcookie-min.js -m reserved=\[k,v,p,e,d,w,m,y\] --comments -c passes=3
> ```
If you are using npm, it is recommended you simply run the script if it hasn't already.
[Conditional directory from pcambra](https://stackoverflow.com/questions/4906579/how-to-use-bash-to-create-a-folder-if-it-doesnt-already-exist)
<br>
### Including MicroCookie in your project
Using HTML
```html
<!-- Stored locally -->
<script src="microcookie-min.js"></script>
<script src="path/to/src/microcookie-min.js"></script>
<!-- Stored locally using npm -->
<script src="node_modules/microcookie/export/microcookie-min.js"></script>
<!-- Stored on my website (no garunteed reliability) -->
<script src="https://almostd.one/pkg/microcookie-min.js"></script>
Expand All @@ -53,13 +101,15 @@ MicroCookie is not currently designed for npm (even though I have the package.js
<script src="https://cdn.jsdelivr.net/gh/10Nates/microcookie@main/export/microcookie-min.js"></script>
```
<br>

### Minimize MicroCookie yourself
Using JavaScript
The arguments I used for uglifyjs were

> `uglifyjs microcookie.js -o export/microcookie-min.js -m reserved=\[key,value,path,exp,d,w,m,y\] --comments -c passes=3`
```js
//https://stackoverflow.com/questions/950087/how-do-i-include-a-javascript-file-in-another-javascript-file
//I am not using the ES6 module/CommonJS system because I intend for this package to be as compatible as possible.
var script = document.createElement("script"); // create a script DOM node
script.src = "whatever you picked for HTML but here in javascript because javascript is the future";
document.head.appendChild(script);
```
<br>
Expand Down Expand Up @@ -128,12 +178,14 @@ MicroCookie.set("test", "This is a test!", expiration);
* @description Remove a cookie
* @param {string} key key to be removed
*/
MicroCookie.remove(key)
MicroCookie.remove(key);
//example - remove cookie "test"
MicroCookie.remove("test")
MicroCookie.remove("test");
```
<br>
### Full examples
A the time of writing, there is one full example, which is available in the [`test/`](./test/) folder as [`test/testpage.html`](./test/testpage.html)
At the time of writing, there is one full example, which is available in the [`test/`](./test/) folder as [`test/testpage.html`](./test/testpage.html)
4 changes: 2 additions & 2 deletions export/microcookie-min.js

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

43 changes: 22 additions & 21 deletions microcookie.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
/** @preserve https://github.com/10Nates/microcookie */
/**@preserve https://github.com/10Nates/microcookie */

const MicroCookie = {
//Against my better judgement to use const or let, It doesn't recieve 100% compatibility on https://seedmanc.github.io/jscc/
var MicroCookie = {

/**
* @description Get a cookie
* @param {string} key
* @param {string} k key
* @returns {string|undefined} value of key
*/
get: function (key) {
get: function (k) {
//split cookie string into separate cookies
let cparse = document.cookie ? document.cookie.split(/; ?/g) : []
var cparse = document.cookie ? document.cookie.split(/; ?/g) : []
for (i=0; i<cparse.length; i++) {
//detect desired cookie
if (cparse[i].startsWith(key + '=')) {
if (cparse[i].startsWith(k + '=')) {
return decodeURIComponent(cparse[i].split('=')[1])
}
}
Expand All @@ -21,31 +22,31 @@ const MicroCookie = {

/**
* @description Set a cookie
* @param {string} key to prevent issues, only use alphanumeric characters
* @param {string} val the value the key will be set to
* @param {number} exp Unix timestamp (seconds)
* @param {string} k key - to prevent issues, only use alphanumeric characters
* @param {string} v value - what the key will be set to
* @param {number} e expiration - Unix timestamp (seconds)
* @returns {string} the encoded cookie string (does not need to be used)
*/
set: function (key, val, exp) {
set: function (k, v, e) {
//convert timestamp to RSC spec
let d = new Date()
d.setTime(exp * 1000)
let expString = d.toUTCString()
var d = new Date()
d.setTime(e * 1000)
var expString = d.toUTCString()
//encode value of key to prevent issues
let setval = encodeURIComponent(val)
var setval = encodeURIComponent(v)
//put it together
let cookiestring = key + "=" + setval + "; expires=" + expString
var cookiestring = k + "=" + setval + "; expires=" + expString
document.cookie = cookiestring
return cookiestring
},

/**
* @description Remove a cookie
* @param {string} key key to be removed
* @param {string} k key to be removed
*/
remove: function (key) {
remove: function (k) {
//set cookie to expired date
document.cookie = key + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC";
document.cookie = k + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC";
},

/**
Expand All @@ -57,10 +58,10 @@ const MicroCookie = {
* @returns {number} The calculated unix timestamp
*/
makeExpiration: function (d, w, m, y) {
//milliseconds -> seconds
let nowsecs = Math.floor(Date.now() / 1000)
//milliseconds -> seconds, not using Date.now() for compatibility
var nowsecs = Math.floor(new Date().getTime() / 1000)
// 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)
var newtime = nowsecs + (d ? d * 86400 : 0) + (w ? w * 604800 : 0) + (m ? m * 2629800 : 0) + (y ? y * 31557600 : 0)
return Math.floor(newtime)
}

Expand Down
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
{
"name": "microcookie",
"version": "1.0.1",
"version": "1.0.2",
"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"
},
"scripts": {
"ugly": "if [ ! -d export ]; then mkdir export; fi && uglifyjs microcookie.js -o export/microcookie-min.js -m reserved=\\[k,v,p,e,d,w,m,y\\] --comments -c passes=3",
"postinstall": "npm run-script ugly"
},
"files": ["LICENSE", "microcookie.js", "package.json", "README.md"],
"keywords": [
"cookie",
"cookies",
"manager",
"library",
"browser",
"js",
"client",
"simple",
"small",
Expand Down

0 comments on commit f9fae74

Please sign in to comment.