Skip to content

A cookie utility for creating, reading, adjusting and purging JavaScript cookies.

Notifications You must be signed in to change notification settings

briancribb/crap-cookie

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

crap-cookie

A cookie utility for creating, reading, adjusting and purging JavaScript cookies.

Installation

There aren't any tricks to installing this. You don't have to create an instance or mess with prototypes. As soon as the file loads, a global variable called crapCookie is defined on the window object.

Create

To bake a crap cookie, you just call a method on the new global variable like this: crapCookie.create(options). The options object has a few properties that you should know about. They are:

  • key: the name of the cookie. It's called a key because it's part of a key/value pair. If you do not pass in a key, then this method will do absolutely nothing.
  • value: the string value that will be stored in the cookie.
  • days: defaults to 1. If you want the cookie to last less than a day, then pass in zero along with some kind of number for hours and/or minutes.
  • hours: defaults to 0. This number will be added to the lifespan of the cookie in hours.
  • minutes: defaults to 0. If a number is assigned here, then that number will be added to the cookie's total lifespan in minutes.

So if you want to create a cookie which will last for three days, you would do it like this:

crapCookie.create({
  key: "myCookieName",
  value:"Stuff I'm storing in this cookie.",
  days:3
});

Days, hours and minutes will be added together an applied to a brand new date object, so you can pick any lifespan you want. Just remember that the day option defaults to one. If you want a cookie to last for less than a day then you need to set days to zero. For example, the following example will create a cookie which lasts for six hours and 32 minutes.

crapCookie.create({
  key: "shortLivedCookie",
  value:"This cookie last for less than a day.",
  days:0,
  hours: 6,
  minutes: 32
});

Read

This method returns the value of the cookie you name. That's it. It's not trying to be fancy. Pass in a string, get a sting in return.

crapCookie.read('shortLivedCookie');
// "This cookie last for less than a day."

Adjust

Okay, so... you don't really edit cookies. You just create a new cookie with the same name and overwrite the stuff that's in there. So this method works exactly like the create method.

Of course, this means that you can pass a brand new name into the read method to make a new cookie and pass an existing name into the create function to edit an existing cookie. I realize that this may seem redundant to you, but in my defense I must remind you that this entire repo is based upon a poop joke and I cannot deliver said poop joke without the letter "A".

crapCookie.adjust({
  key: "shortLivedCookie",
  value:"This is my new info.",
  days:0,
  hours: 6,
  minutes: 32
});
// Yes you have to enter the time info again 
// or you get the default of one day.

Purge

This method will set an existing cookie's value to an empty string and it's expiration date to the current time. That's how you get rid of cookies in JavaScript, assuming that you're not mucking about in the dev tools.

crapCookie.purge("shortLivedCookie");

This method works by calling the adjust method with a pre-defined options object. And before you say anything, yes I am aware that the adjust method is just going to call the create method with whatever options object it uses. You could easily use the create method to do this, but I happen to think it's funny to pass that options object along like water in a bucket brigade and this is all a big poop joke anyway so what the hell do you want from me?

About

A cookie utility for creating, reading, adjusting and purging JavaScript cookies.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages