cookmate v1.0.0
Cookmate
A pure Javascript Cookie management controller Library.
How to build Cookmate
Clone a copy of the main Cookmate git repo by running:
git clone git://github.com/jqrony/cookmate.git
In the cookmate/lib
folder you will find build version of cookmate along with the minified copy and associated map file.
npm install
# install locally (recomended)
npm install cookmate --save
Including Cookmate
Below are some of the most common ways to include Cookmate
Browser
Script tag
<!--including Cookmate (recomended) HTML document in head section -->
<script src="https://cdn.jsdelivr.net/npm/cookmate/lib/cookmate.min.js"></script>
API
new Cookmate.set(
String name, // ex: token, first-name etc. (required)
Mixed value, // Mixed ex: 4, foo, true etc. (required)
Timestamp expires, // 3600, Thu, 13 Jan 2024 (required)
String path, // Allow: /path (optional)
String domain, // Allow: example.com (optional)
Boolean secure, // Allow: true/false (optional)
Boolean HttpOnly, // Allow: true/false (optional)
String sameSite, // Allow: Strict, Lax, None (optional)
String priority // Allow: High, Medium, Low (optional)
);
/**
* @param {key} required
* @returns Boolean true/false
*/
new Cookmate.has(String key);
/**
* @param {key} required
* @returns matched value
*/
new Cookmate.get(String key);
/**
* @param {key} String (required)
* @param {path} "/path" (optional)
* @param {domain} "example.com" (optional)
*/
new Cookmate.remove(String key, String path, String domain);
Usage
Webpack / Browserify / Babel
There are several ways to use Webpack, Browserify or Babel. For more information on using these tools, please refer to the corresponding project's documentation. In the script, including Cookmate will usually look like this:
import cookmate from "cookmate";
If you need to use Cookmate in a file that's not an ECMAScript module, you can use the CommonJS syntax:
const cookmate = require("cookmate");
AMD (Asynchronous Module Definition)
AMD is a module format built for the browser. For more information, we recommend
define(["cookmate"], function(cookmate) {
});
if include Cookmate library or CDN Link in document file. then you use pure javascript syntax:
const cookmate = new Cookmate();
Code Example Syntax
How to switch cookie in JSON Format
cookmate.toJson();
// Output: {"id": 1, "user": "foo"}
How to parse cookie
cookmate.parse();
// Output: {id: 1, user: "foo"}
How to serialize cookie
cookmate.serialize();
// Output: id=1&user=foo
How to get All cookie Data
cookmate.getAll();
// Output: {...} returns all cookie data
How to clear All cookie
// Clearing all cookie on active location
cookmate.clear();
How to set cookie
cookmate.set("id", 1, 3600, "/");
cookmate.set("user", "foo", "Thu, 13 Jan 2024", "/");
How to remove cookie
cookmate.remove("id");
// if in case change path and domain then
cookmate.remove("key", "/root", "example.com");
How to get cookie with specific key
cookmate.get("user");
// Output: foo
How to check exist or not-exist cookie
cookmate.has("user");
// Output: true
Event Listener
fire cookmate change
event when cookie set, remove, clear
cookmate.on("change", fucntion(event) {
console.log(event.deleted, event.changed);
});