From cda497311076eb99a32a4438871cbcad1fbc10b0 Mon Sep 17 00:00:00 2001 From: georapbox Date: Wed, 4 Nov 2020 18:27:44 +0200 Subject: [PATCH 1/6] Update cookie utilities implementation --- README.md | 2 +- packages/dom/cookie/README.md | 89 +++++++++++++++++++++++ packages/dom/cookie/cookie.js | 122 +++++++++++++++++++++++++++++++ packages/dom/cookie/test.js | 13 ++++ packages/misc/cookie/README.md | 94 ------------------------ packages/misc/cookie/cookie.js | 129 --------------------------------- packages/misc/cookie/test.js | 11 --- 7 files changed, 225 insertions(+), 235 deletions(-) create mode 100644 packages/dom/cookie/README.md create mode 100644 packages/dom/cookie/cookie.js create mode 100644 packages/dom/cookie/test.js delete mode 100644 packages/misc/cookie/README.md delete mode 100644 packages/misc/cookie/cookie.js delete mode 100644 packages/misc/cookie/test.js diff --git a/README.md b/README.md index 1489e0a2..e3a6e289 100755 --- a/README.md +++ b/README.md @@ -180,6 +180,7 @@ A collection of standalone javascript utility functions. |--------|-----------| |[captureMouse](https://github.com/georapbox/js-utils/tree/master/packages/dom/captureMouse)|Captures the mouse position on a specific HTML element.| |[captureTouch](https://github.com/georapbox/js-utils/tree/master/packages/dom/captureTouch)|Captures the touch position on a specific HTML element.| +|[cookie](https://github.com/georapbox/js-utils/tree/master/packages/dom/cookie)|Create, read and delete cookies.| |[preloadImages](https://github.com/georapbox/js-utils/tree/master/packages/dom/preloadImages)|Asynchronously load images to browser so that can be cached.| |[isEventSupported](https://github.com/georapbox/js-utils/tree/master/packages/dom/isEventSupported)|Checks if an event is supported in a browser environment.| |[scroll](https://github.com/georapbox/js-utils/tree/master/packages/dom/scroll)|Easing based scrolling to a specified y point inside page.| @@ -198,7 +199,6 @@ A collection of standalone javascript utility functions. |Name|Description| |--------|-----------| -|[cookie](https://github.com/georapbox/js-utils/tree/master/packages/misc/cookie)|Create, read and delete cookies.| |[hexToRGB](https://github.com/georapbox/js-utils/tree/master/packages/misc/hexToRGB)|Converts a color value (number or hexadecimal string) to RGB(A) format.| |[parseColor](https://github.com/georapbox/js-utils/tree/master/packages/misc/parseColor)|Converts a color number value to a hexadecimal formatted string, or a hexadecimal formatted string to a number.| diff --git a/packages/dom/cookie/README.md b/packages/dom/cookie/README.md new file mode 100644 index 00000000..4b39fc0b --- /dev/null +++ b/packages/dom/cookie/README.md @@ -0,0 +1,89 @@ + + +## cookie +**Category**: DOM + +* [cookie](#module_cookie) + * [~set(name, value, [options], [options['max-age']])](#module_cookie..set) ⇒ void + * [~get([name])](#module_cookie..get) ⇒ String + * [~delete(name)](#module_cookie..delete) ⇒ void + + + +### cookie~set(name, value, [options], [options['max-age']]) ⇒ void +Creates a new cookie. + +**Kind**: inner method of [cookie](#module_cookie) +**Throws**: + +- TypeError If `name` is not string. +- TypeError If `value` is not string. + + +| Param | Type | Default | Description | +| --- | --- | --- | --- | +| name | String | | The name of the cookie to create. | +| value | String | | The value of the cookie to create. | +| [options] | Object | | | +| [options.path] | String | "/" | The path where cookie is visible. If not specified, defaults to the current path of the current document location. | +| [options.domain] | String | | The domain where cookie is visible. If not specified, this defaults to the host portion of the current document location. If a domain is specified, subdomains are always included. | +| [options.expires] | String | | A date in GMTString format that tells when cookie expires. If not specified it will expire at the end of session. If date is in the past, then the cookie is deleted. Use `Date.prototype.toUTCString()` to properly format it. | +| [options['max-age']] | Number | | Max age in seconds from the time the cookie is set; alternative to `expires`. If not specified it will expire at the end of session. If zero or negative, then the cookie is deleted. | +| [options.secure] | String | | Cookie to only be transmitted over secure protocol as https. | +| [options.samesite] | String | | SameSite prevents the browser from sending this cookie along with cross-site requests. Possible values are "lax", "strict" or "none". | + +**Example** +```js +cookie.set('foo', 'bar', { + path: '/', + domain: 'example.com', + 'max-age': 3600, // value in seconds; expires after one hour from the current time + secure: true, + samesite: 'strict' +}); +// -> undefined +``` + + +### cookie~get([name]) ⇒ String +Get a cookie by its name. + +**Kind**: inner method of [cookie](#module_cookie) +**Returns**: String - Returns the value of the cookie if exists; otherwise an empty string. +**Throws**: + +- TypeError If `name` is not string. + + +| Param | Type | Description | +| --- | --- | --- | +| [name] | String | The name of the cookie to get. If not provided the whole cookie string is returned. | + +**Example** +```js +cookie.get('foo'); +// -> 'bar' + +cookie.get('cookie-that-does-not-exist'); +// -> '' +``` + + +### cookie~delete(name) ⇒ void +Deletes a cookie by its name. + +**Kind**: inner method of [cookie](#module_cookie) +**Throws**: + +- TypeError If `name` is not string. + + +| Param | Type | Description | +| --- | --- | --- | +| name | String | The name of the cookie to delete. | + +**Example** +```js +cookie.delete('foo'); +// -> undefined +``` \ No newline at end of file diff --git a/packages/dom/cookie/cookie.js b/packages/dom/cookie/cookie.js new file mode 100644 index 00000000..dffd7cdd --- /dev/null +++ b/packages/dom/cookie/cookie.js @@ -0,0 +1,122 @@ +/** + * @module cookie + * @category DOM + */ +module.exports = (function () { + /** + * Creates a new cookie. + * + * @function set + * @param {String} name The name of the cookie to create. + * @param {String} value The value of the cookie to create. + * @param {Object} [options] + * @param {String} [options.path="/"] The path where cookie is visible. If not specified, defaults to the current path of the current document location. + * @param {String} [options.domain] The domain where cookie is visible. If not specified, this defaults to the host portion of the current document location. If a domain is specified, subdomains are always included. + * @param {String} [options.expires] A date in GMTString format that tells when cookie expires. If not specified it will expire at the end of session. If date is in the past, then the cookie is deleted. Use `Date.prototype.toUTCString()` to properly format it. + * @param {Number} [options['max-age']] Max age in seconds from the time the cookie is set; alternative to `expires`. If not specified it will expire at the end of session. If zero or negative, then the cookie is deleted. + * @param {String} [options.secure] Cookie to only be transmitted over secure protocol as https. + * @param {String} [options.samesite] SameSite prevents the browser from sending this cookie along with cross-site requests. Possible values are "lax", "strict" or "none". + * @throws {TypeError} If `name` is not string. + * @throws {TypeError} If `value` is not string. + * @returns {void} + * @example + * + * cookie.set('foo', 'bar', { + * path: '/', + * domain: 'example.com', + * 'max-age': 3600, // value in seconds; expires after one hour from the current time + * secure: true, + * samesite: 'strict' + * }); + * // -> undefined + */ + function setCookie(name, value, options) { + var cookie, optionKey, optionValue; + + if (typeof name !== 'string' || typeof value !== 'string') { + throw new TypeError('Expected a string for first and second argument'); + } + + if (!options || typeof options !== 'object') { + options = {}; + } + + options.path = options.path || '/'; + + cookie = encodeURIComponent(name) + '=' + encodeURIComponent(value); + + for (optionKey in options) { + if (Object.prototype.hasOwnProperty.call(options, optionKey)) { + optionValue = options[optionKey]; + cookie += '; ' + optionKey; + + if (optionValue !== true) { + cookie += '=' + optionValue; + } + } + } + + document.cookie = cookie; + } + + /** + * Get a cookie by its name. + * + * @function get + * @param {String} [name] The name of the cookie to get. If not provided the whole cookie string is returned. + * @throws {TypeError} If `name` is not string. + * @returns {String} Returns the value of the cookie if exists; otherwise an empty string. + * @example + * + * cookie.get('foo'); + * // -> 'bar' + * + * cookie.get('cookie-that-does-not-exist'); + * // -> '' + */ + function getCookie(name) { + var matches; + + if (typeof name !== 'string' && typeof name !== 'undefined') { + throw new TypeError('Expected a string for first argument'); + } + + if (typeof name === 'undefined') { + return document.cookie; + } + + matches = document.cookie.match( + new RegExp('(?:^|; )' + name.replace(/([.$?*|{}()[\]\\/+^])/g, '\\$1') + '=([^;]*)') + ); + + return matches ? decodeURIComponent(matches[1]) : ''; + } + + /** + * Deletes a cookie by its name. + * + * @function delete + * @param {String} name The name of the cookie to delete. + * @throws {TypeError} If `name` is not string. + * @returns {void} + * @example + * + * cookie.delete('foo'); + * // -> undefined + */ + function deleteCookie(name) { + if (typeof name !== 'string') { + throw new TypeError('Expected a string for first argument'); + } + + setCookie(name, '', { + 'max-age': -1 + }); + } + + return { + set: setCookie, + get: getCookie, + delete: deleteCookie + }; +}()); \ No newline at end of file diff --git a/packages/dom/cookie/test.js b/packages/dom/cookie/test.js new file mode 100644 index 00000000..838e2d73 --- /dev/null +++ b/packages/dom/cookie/test.js @@ -0,0 +1,13 @@ +var cookie = require('./cookie'); + +describe('misc/cookie', function () { + it('should create, read and delete cookies', function () { + cookie.set('my-cookie', 'test1'); + + expect(cookie.get('my-cookie')).toBe('test1'); + + cookie.delete('my-cookie'); + + expect(cookie.get('my-cookie')).toBe(''); + }); +}); diff --git a/packages/misc/cookie/README.md b/packages/misc/cookie/README.md deleted file mode 100644 index 13cfc1dd..00000000 --- a/packages/misc/cookie/README.md +++ /dev/null @@ -1,94 +0,0 @@ - - -## cookie -**Category**: Misc - -* [cookie](#module_cookie) - * [~create(name, value, [expiresOn], [path])](#module_cookie..create) - * [~read([name])](#module_cookie..read) ⇒ String - * [~remove(name)](#module_cookie..remove) - - - -### cookie~create(name, value, [expiresOn], [path]) -Creates a new cookie. - -**Kind**: inner method of [cookie](#module_cookie) - -| Param | Type | Default | Description | -| --- | --- | --- | --- | -| name | String | | The name of the cookie to create. | -| value | String | | The value of the cookie to create. | -| [expiresOn] | String | '' | Denotes when the cookie expires. If is of type `number`, denotes the number of days the cookie is active. If is set to 0 cookie's expiration is set to Session. If is of type `string` the provided string must be of UTC/GMT format eg. "Mon, 25 Apr 2016 02:47:11 UTC", else expirations is set to Session. | -| [path] | String | '/' | Optional. The directory where the cookie is active. | - -**Example** -```js -// Create a cookie with name "my-cookie1" and value "test1" that expires 3 days from now. -cookie.create('my-cookie1', 'test1', 3); - -// Create a cookie with name "my-cookie2" and value "test2" that expires with session. -cookie.create('my-cookie2', 'test2'); - -// Create a cookie with name "my-cookie3" and value "test3" that expires with session. -cookie.create('my-cookie3', 'test3', 0); - -// Create a cookie with name "my-cookie4" and value "test4" that expires on "Mon, 25 Apr 2016 02:47:11 UTC". -cookie.create('my-cookie4', 'test4', 'Mon, 25 Apr 2016 02:47:11 UTC'); - -// Create a cookie with name "my-cookie5" and value "test5" that expires 2 days from now and the path is "/my-page". -cookie.create('my-cookie5', 'test5', 2, '/my-page'); - -// Create a cookie with name "my-cookie6" and value "test6" that expires on "Mon, 20 Jun 2016 00:30:20 UTC" and the path is "/my-page". -cookie.create('my-cookie6', 'test6', new Date(2016, 5, 20, 3, 30, 20, 20).toUTCString(), '/my-page'); -``` - - -### cookie~read([name]) ⇒ String -Reads a cookie by its name. - -**Kind**: inner method of [cookie](#module_cookie) -**Returns**: String - Returns the value of the cookie if exists, else `null`. - -| Param | Type | Description | -| --- | --- | --- | -| [name] | String | The name of the cookie to read. If not provided or is not of type `string` the whole cookie string is returned. | - -**Example** -```js -// From the example above and supposing we are on root directory. - -cookie.read('my-cookie1'); -// -> "test1" - -cookie.read('my-cookie2'); -// -> "test2" - -cookie.read('my-cookie5'); -// -> null - -cookie.read('non-existent-cookie'); - -cookie.read(); -// -> "my-cookie1=test1; my-cookie2=test2; my-cookie3=test3; my-cookie4=test4" -``` - - -### cookie~remove(name) -Deletes a cookie by its name. - -**Kind**: inner method of [cookie](#module_cookie) - -| Param | Type | Description | -| --- | --- | --- | -| name | String | The name of the cookie to delete. | - -**Example** -```js -// From the examples above and supposing we are on root directory. - -cookie.remove('my-cookie1'); - -cookie.read(); -// -> "my-cookie2=test2; my-cookie3=test3; my-cookie4=test4" -``` diff --git a/packages/misc/cookie/cookie.js b/packages/misc/cookie/cookie.js deleted file mode 100644 index cb0e428d..00000000 --- a/packages/misc/cookie/cookie.js +++ /dev/null @@ -1,129 +0,0 @@ -/** - * @module cookie - * @category Misc - */ -module.exports = (function () { - /** - * Creates a new cookie. - * - * @function create - * @param {String} name The name of the cookie to create. - * @param {String} value The value of the cookie to create. - * @param {String} [expiresOn=''] Denotes when the cookie expires. - * If is of type `number`, denotes the number of days the cookie is active. If is set to 0 cookie's expiration is set to Session. - * If is of type `string` the provided string must be of UTC/GMT format - * eg. "Mon, 25 Apr 2016 02:47:11 UTC", else expirations is set to Session. - * @param {String} [path='/'] Optional. The directory where the cookie is active. - * @return {undefined} - * @example - * - * // Create a cookie with name "my-cookie1" and value "test1" that expires 3 days from now. - * cookie.create('my-cookie1', 'test1', 3); - * - * // Create a cookie with name "my-cookie2" and value "test2" that expires with session. - * cookie.create('my-cookie2', 'test2'); - * - * // Create a cookie with name "my-cookie3" and value "test3" that expires with session. - * cookie.create('my-cookie3', 'test3', 0); - * - * // Create a cookie with name "my-cookie4" and value "test4" that expires on "Mon, 25 Apr 2016 02:47:11 UTC". - * cookie.create('my-cookie4', 'test4', 'Mon, 25 Apr 2016 02:47:11 UTC'); - * - * // Create a cookie with name "my-cookie5" and value "test5" that expires 2 days from now and the path is "/my-page". - * cookie.create('my-cookie5', 'test5', 2, '/my-page'); - * - * // Create a cookie with name "my-cookie6" and value "test6" that expires on "Mon, 20 Jun 2016 00:30:20 UTC" and the path is "/my-page". - * cookie.create('my-cookie6', 'test6', new Date(2016, 5, 20, 3, 30, 20, 20).toUTCString(), '/my-page'); - */ - function createCookie(name, value, expiresOn, path) { - var date, expires; - - if (typeof expiresOn === 'number' && expiresOn !== 0) { - date = new Date(); - date.setTime(date.getTime() + Math.ceil(expiresOn) * 24 * 60 * 60 * 1000); - expires = '; expires=' + date.toUTCString(); - } else if (typeof expiresOn === 'string') { - expires = '; expires=' + expiresOn; - } else { - expires = ''; - } - - document.cookie = name + '=' + value + expires + '; path=' + (path || '/'); - } - - /** - * Reads a cookie by its name. - * - * @function read - * @param {String} [name] The name of the cookie to read. - * If not provided or is not of type `string` the whole cookie string is returned. - * @return {String} Returns the value of the cookie if exists, else `null`. - * @example - * - * // From the example above and supposing we are on root directory. - * - * cookie.read('my-cookie1'); - * // -> "test1" - * - * cookie.read('my-cookie2'); - * // -> "test2" - * - * cookie.read('my-cookie5'); - * // -> null - * - * cookie.read('non-existent-cookie'); - * - * cookie.read(); - * // -> "my-cookie1=test1; my-cookie2=test2; my-cookie3=test3; my-cookie4=test4" - */ - function readCookie(name) { - var cookie = document.cookie, - nameEQ, ca, c, i, len; - - if (typeof name !== 'string') { - return cookie; - } - - nameEQ = name + '='; - ca = cookie.split(';'); - len = ca.length; - i = 0; - - for (i; i < len; i += 1) { - c = ca[i]; - while (c.charAt(0) === ' ') { - c = c.substring(1, c.length); - } - if (c.indexOf(nameEQ) === 0) { - return c.substring(nameEQ.length, c.length); - } - } - - return null; - } - - /** - * Deletes a cookie by its name. - * - * @function remove - * @param {String} name The name of the cookie to delete. - * @return {undefined} - * @example - * - * // From the examples above and supposing we are on root directory. - * - * cookie.remove('my-cookie1'); - * - * cookie.read(); - * // -> "my-cookie2=test2; my-cookie3=test3; my-cookie4=test4" - */ - function removeCookie(name) { - createCookie(name, '', -1); - } - - return { - create: createCookie, - read: readCookie, - remove: removeCookie - }; -}()); \ No newline at end of file diff --git a/packages/misc/cookie/test.js b/packages/misc/cookie/test.js deleted file mode 100644 index 9eb63524..00000000 --- a/packages/misc/cookie/test.js +++ /dev/null @@ -1,11 +0,0 @@ -var cookie = require('./cookie'); - -describe('misc/cookie', function () { - it('should create a new cookie with name "my-cookie" and value "test1", read it and finally remove it', function () { - cookie.create('my-cookie', 'test1'); - expect(cookie.read('my-cookie')).toEqual('test1'); - - cookie.remove('my-cookie'); - expect(cookie.read('my-cookie')).toBeNull(); - }); -}); From fee6ae750251b7d7e860847b449e223a16173d92 Mon Sep 17 00:00:00 2001 From: georapbox Date: Thu, 5 Nov 2020 11:09:13 +0200 Subject: [PATCH 2/6] Make name parameter required --- packages/dom/cookie/cookie.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/dom/cookie/cookie.js b/packages/dom/cookie/cookie.js index dffd7cdd..b747ea73 100644 --- a/packages/dom/cookie/cookie.js +++ b/packages/dom/cookie/cookie.js @@ -77,7 +77,7 @@ module.exports = (function () { function getCookie(name) { var matches; - if (typeof name !== 'string' && typeof name !== 'undefined') { + if (typeof name !== 'string') { throw new TypeError('Expected a string for first argument'); } From ba101b0363e63cb1fb847c06993ca64d90807b85 Mon Sep 17 00:00:00 2001 From: georapbox Date: Thu, 5 Nov 2020 11:09:51 +0200 Subject: [PATCH 3/6] Update parameter documentation --- packages/dom/cookie/cookie.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/dom/cookie/cookie.js b/packages/dom/cookie/cookie.js index b747ea73..9d055a0e 100644 --- a/packages/dom/cookie/cookie.js +++ b/packages/dom/cookie/cookie.js @@ -63,7 +63,7 @@ module.exports = (function () { * Get a cookie by its name. * * @function get - * @param {String} [name] The name of the cookie to get. If not provided the whole cookie string is returned. + * @param {String} [name] The name of the cookie to get. * @throws {TypeError} If `name` is not string. * @returns {String} Returns the value of the cookie if exists; otherwise an empty string. * @example From 4265eef20198eb0b876d87de884dc261d9b6b61d Mon Sep 17 00:00:00 2001 From: georapbox Date: Thu, 5 Nov 2020 11:10:40 +0200 Subject: [PATCH 4/6] Rename delete method to remove --- packages/dom/cookie/cookie.js | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/packages/dom/cookie/cookie.js b/packages/dom/cookie/cookie.js index 9d055a0e..f265b724 100644 --- a/packages/dom/cookie/cookie.js +++ b/packages/dom/cookie/cookie.js @@ -81,10 +81,6 @@ module.exports = (function () { throw new TypeError('Expected a string for first argument'); } - if (typeof name === 'undefined') { - return document.cookie; - } - matches = document.cookie.match( new RegExp('(?:^|; )' + name.replace(/([.$?*|{}()[\]\\/+^])/g, '\\$1') + '=([^;]*)') ); @@ -95,16 +91,16 @@ module.exports = (function () { /** * Deletes a cookie by its name. * - * @function delete + * @function remove * @param {String} name The name of the cookie to delete. * @throws {TypeError} If `name` is not string. * @returns {void} * @example * - * cookie.delete('foo'); + * cookie.remove('foo'); * // -> undefined */ - function deleteCookie(name) { + function removeCookie(name) { if (typeof name !== 'string') { throw new TypeError('Expected a string for first argument'); } @@ -117,6 +113,6 @@ module.exports = (function () { return { set: setCookie, get: getCookie, - delete: deleteCookie + remove: removeCookie }; }()); \ No newline at end of file From f87b47425903d7b89d7807f5f5f7d63c1af3ab0e Mon Sep 17 00:00:00 2001 From: georapbox Date: Thu, 5 Nov 2020 11:10:51 +0200 Subject: [PATCH 5/6] Update documentation --- packages/dom/cookie/README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/dom/cookie/README.md b/packages/dom/cookie/README.md index 4b39fc0b..0170dbe4 100644 --- a/packages/dom/cookie/README.md +++ b/packages/dom/cookie/README.md @@ -6,7 +6,7 @@ * [cookie](#module_cookie) * [~set(name, value, [options], [options['max-age']])](#module_cookie..set) ⇒ void * [~get([name])](#module_cookie..get) ⇒ String - * [~delete(name)](#module_cookie..delete) ⇒ void + * [~remove(name)](#module_cookie..remove) ⇒ void @@ -57,7 +57,7 @@ Get a cookie by its name. | Param | Type | Description | | --- | --- | --- | -| [name] | String | The name of the cookie to get. If not provided the whole cookie string is returned. | +| [name] | String | The name of the cookie to get. | **Example** ```js @@ -67,9 +67,9 @@ cookie.get('foo'); cookie.get('cookie-that-does-not-exist'); // -> '' ``` - + -### cookie~delete(name) ⇒ void +### cookie~remove(name) ⇒ void Deletes a cookie by its name. **Kind**: inner method of [cookie](#module_cookie) @@ -84,6 +84,6 @@ Deletes a cookie by its name. **Example** ```js -cookie.delete('foo'); +cookie.remove('foo'); // -> undefined ``` \ No newline at end of file From 91f02fae4f7024aa12fd68e2aebba573bb3c4f9d Mon Sep 17 00:00:00 2001 From: georapbox Date: Thu, 5 Nov 2020 11:10:56 +0200 Subject: [PATCH 6/6] Add tests --- packages/dom/cookie/test.js | 58 ++++++++++++++++++++++++++++++++++--- 1 file changed, 54 insertions(+), 4 deletions(-) diff --git a/packages/dom/cookie/test.js b/packages/dom/cookie/test.js index 838e2d73..a3bf2fae 100644 --- a/packages/dom/cookie/test.js +++ b/packages/dom/cookie/test.js @@ -2,12 +2,62 @@ var cookie = require('./cookie'); describe('misc/cookie', function () { it('should create, read and delete cookies', function () { - cookie.set('my-cookie', 'test1'); + cookie.set('cookie-1', 'value-1'); + expect(cookie.get('cookie-1')).toBe('value-1'); - expect(cookie.get('my-cookie')).toBe('test1'); + cookie.set('cookie-2', 'val ue-2'); + expect(cookie.get('cookie-2')).toBe('val ue-2'); - cookie.delete('my-cookie'); + cookie.set('cookie-3', 'val;ue-3'); + expect(cookie.get('cookie-3')).toBe('val;ue-3'); - expect(cookie.get('my-cookie')).toBe(''); + cookie.set('cookie-4', 'val,ue-4'); + expect(cookie.get('cookie-4')).toBe('val,ue-4'); + + cookie.set('cookie-5', 'val\tue-5'); + expect(cookie.get('cookie-5')).toBe('val\tue-5'); + + cookie.set('cookie-6', 'val\nue-6'); + expect(cookie.get('cookie-6')).toBe('val\nue-6'); + + expect(document.cookie).toBe('cookie-1=value-1; cookie-2=val%20ue-2; cookie-3=val%3Bue-3; cookie-4=val%2Cue-4; cookie-5=val%09ue-5; cookie-6=val%0Aue-6'); + + expect(decodeURIComponent(document.cookie)).toBe('cookie-1=value-1; cookie-2=val ue-2; cookie-3=val;ue-3; cookie-4=val,ue-4; cookie-5=val\tue-5; cookie-6=val\nue-6'); + + cookie.remove('cookie-1'); + expect(cookie.get('cookie-1')).toBe(''); + + cookie.remove('cookie-2'); + expect(cookie.get('cookie-2')).toBe(''); + + cookie.remove('cookie-3'); + expect(cookie.get('cookie-3')).toBe(''); + + cookie.remove('cookie-4'); + expect(cookie.get('cookie-4')).toBe(''); + + cookie.remove('cookie-5'); + expect(cookie.get('cookie-5')).toBe(''); + + cookie.remove('cookie-6'); + expect(cookie.get('cookie-6')).toBe(''); + + expect(document.cookie).toBe(''); + + expect(function () { + return cookie.set(null, 'value'); + }).toThrow('Expected a string for first and second argument'); + + expect(function () { + return cookie.set('name', null); + }).toThrow('Expected a string for first and second argument'); + + expect(function () { + return cookie.get(null); + }).toThrow('Expected a string for first argument'); + + expect(function () { + return cookie.remove(null); + }).toThrow('Expected a string for first argument'); }); });