├── .gitignore
├── Readme.md
├── test
├── index.html
└── tests.js
├── History.md
├── package.json
└── index.js
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
2 | dist
3 | *.log
--------------------------------------------------------------------------------
/Readme.md:
--------------------------------------------------------------------------------
1 | # @stereobooster/cookie
2 |
3 | Minimal cookie library (< 550B compressed) for browser. Fork of [Cookie component](https://github.com/component/cookie).
4 |
5 | ## Installation
6 |
7 | ```sh
8 | yarn add @stereobooster/cookie
9 | ```
10 |
11 | ## Example
12 |
13 | ```js
14 | // set
15 | cookie("name", "tobi");
16 | cookie("name", "tobi", { path: "/" });
17 | cookie("name", "tobi", { maxage: 60000 }); // in milliseconds
18 | cookie("species", "ferret");
19 |
20 | // get
21 | var name = cookie("name");
22 | // => "tobi"
23 |
24 | var cookies = cookie();
25 | // => { name: "tobi", species: "ferret" }
26 |
27 | // clear
28 | cookie("name", null);
29 | ```
30 |
31 | ## License
32 |
33 | MIT
34 |
--------------------------------------------------------------------------------
/test/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 | Tests
8 |
9 |
10 |
11 |
12 |
13 |
14 |
17 |
18 |
19 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/History.md:
--------------------------------------------------------------------------------
1 | 1.1.5 / 2019-06-13
2 | ==================
3 |
4 | * smaller size
5 | * remove debug dependency
6 |
7 | 1.1.3 / 2016-11-09
8 | ==================
9 |
10 | * component: pin `visionmedia/debug` at 2.2.0
11 |
12 | 1.1.1 / 2014-10-24
13 | ==================
14 |
15 | * deps: move debug to dependencies
16 |
17 | 1.1.0 / 2014-10-24
18 | ==================
19 |
20 | * add debugs
21 | * Ignore URIError
22 | * toGMTString is deprecated. replace with toUTCString
23 | * Specify unit for maxage
24 |
25 | 1.0.1 / 2012-12-13
26 | ==================
27 |
28 | * fix returning of empty cookies on `cookie()` when cleared
29 | * make IE <=8 happy
30 |
31 | 1.0.0 / 2012-10-27
32 | ==================
33 |
34 | * add `make test`
35 | * add `cookie() => object` support [uniba]
36 |
37 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "@stereobooster/cookie",
3 | "description": "Tiny cookie library",
4 | "version": "1.1.5",
5 | "license": "MIT",
6 | "devDependencies": {
7 | "chai": "^4.2.0",
8 | "microbundle": "^0.11.0",
9 | "mocha": "^6.1.4",
10 | "mocha-headless-chrome": "^2.0.2",
11 | "npm-run-all": "^4.1.5",
12 | "serve": "^11.0.1",
13 | "size-limit": "^1.3.6"
14 | },
15 | "files": [
16 | "index.js"
17 | ],
18 | "source": "index.js",
19 | "main": "dist/index.js",
20 | "module": "dist/index.mjs",
21 | "unpkg": "dist/index.umd.js",
22 | "amdName": "cookie",
23 | "scripts": {
24 | "build": "microbundle",
25 | "build:watch": "microbundle watch",
26 | "serve": "serve .",
27 | "start": "run-p serve build:watch",
28 | "test": "yarn mocha-headless-chrome -f http://localhost:5000/test",
29 | "size": "yarn build && size-limit",
30 | "prePublish": "yarn build"
31 | },
32 | "size-limit": [
33 | {
34 | "limit": "400b",
35 | "path": "dist/*.{mjs,js}"
36 | }
37 | ]
38 | }
39 |
--------------------------------------------------------------------------------
/index.js:
--------------------------------------------------------------------------------
1 | const debug = err => console.warn(err);
2 |
3 | const encode = value => {
4 | try {
5 | return encodeURIComponent(value);
6 | } catch (e) {
7 | debug(e);
8 | }
9 | };
10 |
11 | const decode = value => {
12 | try {
13 | return decodeURIComponent(value);
14 | } catch (e) {
15 | debug(e);
16 | }
17 | };
18 |
19 | /**
20 | * Set cookie `name` to `value`.
21 | *
22 | * @param {String} name
23 | * @param {String} value
24 | * @param {Object} options
25 | */
26 | const set = (name, value, options = {}) => {
27 | let str = encode(name) + "=" + encode(value);
28 |
29 | if (null === value) options.maxage = -1;
30 |
31 | if (options.maxage) {
32 | options.expires = new Date(+new Date() + options.maxage);
33 | }
34 |
35 | if (options.path) str += "; path=" + options.path;
36 | if (options.domain) str += "; domain=" + options.domain;
37 | if (options.expires) str += "; expires=" + options.expires.toUTCString();
38 | if (options.secure) str += "; secure";
39 |
40 | document.cookie = str;
41 | };
42 |
43 | /**
44 | * Parse cookie `str`.
45 | *
46 | * @param {String} str
47 | * @return {Object}
48 | */
49 | const parse = str =>
50 | str.split(/ *; */).reduce((obj, pair) => {
51 | pair = pair.split("=");
52 | obj[decode(pair[0])] = decode(pair[1]);
53 | return obj;
54 | }, {});
55 |
56 | /**
57 | * Return all cookies.
58 | *
59 | * @return {Object}
60 | */
61 | const all = () => parse(document.cookie);
62 |
63 | /**
64 | * Set or get cookie `name` with `value` and `options` object.
65 | *
66 | * @param {String} name
67 | * @param {String} value
68 | * @param {Object} options
69 | * @return {Mixed}
70 | * @api public
71 | */
72 | export default (name, value, options) => {
73 | if (name === undefined) return all();
74 | if (value === undefined) return all()[name];
75 | set(name, value, options);
76 | };
77 |
--------------------------------------------------------------------------------
/test/tests.js:
--------------------------------------------------------------------------------
1 | "use strict";
2 | var assert = window.chai.assert;
3 |
4 | describe("cookie(name, value)", function() {
5 | it("should set a cookie", function() {
6 | cookie("name", "tobi");
7 | assert("tobi" == cookie("name"));
8 |
9 | cookie("species", "ferret");
10 | assert("ferret" == cookie("species"));
11 | });
12 |
13 | it("should escape", function() {
14 | cookie("name", "tobi ferret");
15 | assert(~document.cookie.indexOf("name=tobi%20ferret"));
16 | });
17 |
18 | it("should unescape", function() {
19 | cookie("full name", "tobi ferret");
20 | assert("tobi ferret" == cookie("full name"));
21 | });
22 |
23 | it("should ignore URIError", function() {
24 | cookie("bad", "%");
25 | cookie("bad", null);
26 | });
27 |
28 | describe("when undefined", function() {
29 | it("should return undefined", function() {
30 | assert(undefined === cookie("whatever"));
31 | });
32 | });
33 | });
34 |
35 | describe("cookie(name, null)", function() {
36 | it("should clear the cookie", function() {
37 | cookie("type", "ferret");
38 | cookie("type", null);
39 | assert(undefined === cookie("type"));
40 | });
41 |
42 | it("should not be returned in the cookie() object", function() {
43 | cookie("full name", null);
44 | cookie("mydb", null);
45 | cookie("species", null);
46 | cookie("name", "0");
47 | var obj = cookie();
48 | assert(1 == Object.keys(obj).length);
49 | assert("0" == obj.name);
50 | });
51 |
52 | it("should ignore URIError and return null", function() {
53 | document.cookie = "bad=%";
54 | assert(null == cookie("bad"));
55 | });
56 | });
57 |
58 | describe("cookie()", function() {
59 | it("should return all cookies", function() {
60 | cookie("name", "loki");
61 | cookie("species", "ferret");
62 | var obj = cookie();
63 | assert(obj, "object was not returned");
64 | assert("loki" == obj.name, ".name failed");
65 | assert("ferret" == obj.species, ".species failed");
66 | });
67 |
68 | it("should return all cookies and ignore URIErrors", function() {
69 | cookie("name", "loki");
70 | cookie("species", "ferret");
71 | document.cookie = "bad=%";
72 | var obj = cookie();
73 | assert("loki" == obj.name, ".name failed");
74 | assert("ferret" == obj.species, ".species failed");
75 | assert(null == obj.bad);
76 | });
77 | });
78 |
--------------------------------------------------------------------------------