{"id":16185305,"url":"https://github.com/jasonkarns/doc-cookies","last_synced_at":"2025-04-07T13:13:33.788Z","repository":{"id":32468362,"uuid":"36048258","full_name":"jasonkarns/doc-cookies","owner":"jasonkarns","description":"A little framework: a complete cookies reader/writer with full unicode support","archived":false,"fork":false,"pushed_at":"2015-06-10T13:53:18.000Z","size":232,"stargazers_count":2,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-17T01:36:24.926Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jasonkarns.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-05-22T02:35:06.000Z","updated_at":"2016-11-11T12:48:33.000Z","dependencies_parsed_at":"2022-08-24T22:41:05.221Z","dependency_job_id":null,"html_url":"https://github.com/jasonkarns/doc-cookies","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jasonkarns%2Fdoc-cookies","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jasonkarns%2Fdoc-cookies/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jasonkarns%2Fdoc-cookies/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jasonkarns%2Fdoc-cookies/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jasonkarns","download_url":"https://codeload.github.com/jasonkarns/doc-cookies/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247657270,"owners_count":20974345,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-10T07:13:58.584Z","updated_at":"2025-04-07T13:13:33.759Z","avatar_url":"https://github.com/jasonkarns.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003e *This module was taken from a [code snippet](https://developer.mozilla.org/en-US/docs/Web/API/document/cookie#A_little_framework.3A_a_complete_cookies_reader.2Fwriter_with_full_unicode_support) on the MDN docs for `document.cookie`. It has been published as an npm module to facilitate use.*\n\n# doc-cookies\nA little framework: a complete cookies reader/writer with full unicode support\n\nSometimes, cookies being formatted strings, it can be intricate to deal with them in a natural way. The following library aims to abstract the access to \u003ccode\u003edocument.cookie\u003c/code\u003e by defining an object (\u003cvar\u003edocCookies\u003c/var\u003e) that is partially consistent with a \u003ca href=\"https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Storage#Storage\"\u003e\u003ccode\u003eStorage\u003c/code\u003e object\u003c/a\u003e. It offers also a full unicode support.\n\n\u003e *Note: For never-expire-cookies we used the arbitrarily distant date \u003csamp\u003eFri, 31 Dec 9999 23:59:59 GMT\u003c/samp\u003e. If for any reason you are afraid of such a date, use the [conventional date of end of the world](http://en.wikipedia.org/wiki/Year_2038_problem) \u003csamp\u003eTue, 19 Jan 2038 03:14:07 GMT\u003c/samp\u003e – which is the maximum number of seconds elapsed since since \u003csamp\u003e1 January 1970 00:00:00 UTC\u003c/samp\u003e expressible by a [signed 32-bit integer](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/Bitwise_Operators#Signed_32-bit_integers) (i.e.: \u003csamp\u003e01111111111111111111111111111111\u003c/samp\u003e, which is `new Date(0x7fffffff * 1e3)`).*\n\n### Writing a cookie\n\n###### Syntax\n``` javascript\ndocCookies.setItem(name, value[, end[, path[, domain[, secure]]]])\n```\n\n###### Description\nCreate/overwrite a cookie.\n\n###### Parameters\n\u003cdl\u003e\n\u003cdt\u003e\u003cvar\u003ename\u003c/var\u003e\u003c/dt\u003e\n\u003cdd\u003eThe \u003ca href=\"https://developer.mozilla.org/en-US/docs/Web/API/document/cookie#new-cookie_syntax\"\u003ename\u003c/a\u003e of the cookie to create/overwrite (\u003ca href=\"https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String\"\u003e\u003ccode\u003estring\u003c/code\u003e\u003c/a\u003e).\u003c/dd\u003e\n\u003cdt\u003e\u003cvar\u003evalue\u003c/var\u003e\u003c/dt\u003e\n\u003cdd\u003eThe \u003ca href=\"https://developer.mozilla.org/en-US/docs/Web/API/document/cookie#new-cookie_syntax\"\u003evalue\u003c/a\u003e of the cookie (\u003ca href=\"https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String\"\u003e\u003ccode\u003estring\u003c/code\u003e\u003c/a\u003e).\u003c/dd\u003e\n\u003cdt\u003e\u003cvar\u003eend\u003c/var\u003e (optional)\u003c/dt\u003e\n\u003cdd\u003eThe \u003ca href=\"https://developer.mozilla.org/en-US/docs/Web/API/document/cookie#new-cookie_max-age\"\u003e\u003ccode\u003emax-age\u003c/code\u003e\u003c/a\u003e in seconds (e.g. \u003csamp\u003e31536e3\u003c/samp\u003e for a year, \u003ca href=\"https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Infinity\"\u003e\u003ccode\u003eInfinity\u003c/code\u003e\u003c/a\u003e for a \u003ccode\u003enever-expires\u003c/code\u003e cookie), or the \u003ca href=\"https://developer.mozilla.org/en-US/docs/Web/API/document/cookie#new-cookie_expires\"\u003e\u003ccode\u003eexpires\u003c/code\u003e\u003c/a\u003e date in \u003ca href=\"https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/toGMTString\"\u003e\u003ccode\u003eGMTString\u003c/code\u003e\u003c/a\u003e format or as \u003ca href=\"https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date\"\u003e\u003ccode\u003eDate\u003c/code\u003e object\u003c/a\u003e; if not specified the cookie will expire at the end of session (\u003ca href=\"https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Number\"\u003e\u003ccode\u003enumber\u003c/code\u003e\u003c/a\u003e – finite or \u003ca href=\"https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Infinity\"\u003e\u003ccode\u003eInfinity\u003c/code\u003e\u003c/a\u003e – \u003ca href=\"https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String\"\u003e\u003ccode\u003estring\u003c/code\u003e\u003c/a\u003e, \u003ca href=\"https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date\"\u003e\u003ccode\u003eDate\u003c/code\u003e object\u003c/a\u003e or \u003ca href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null\"\u003e\u003ccode\u003enull\u003c/code\u003e\u003c/a\u003e).\u003c/dd\u003e\n\u003cdt\u003e\u003cvar\u003epath\u003c/var\u003e (optional)\u003c/dt\u003e\n\u003cdd\u003eThe \u003ca href=\"https://developer.mozilla.org/en-US/docs/Web/API/document/cookie#new-cookie_path\"\u003epath\u003c/a\u003e from where the cookie will be readable. E.g., \u003ccode\u003e\"/\"\u003c/code\u003e, \u003ccode\u003e\"/mydir\"\u003c/code\u003e; if not specified, defaults to the current path of the current document location (\u003ca href=\"https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String\"\u003e\u003ccode\u003estring\u003c/code\u003e\u003c/a\u003e or \u003ca href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null\"\u003e\u003ccode\u003enull\u003c/code\u003e\u003c/a\u003e). The path must be \u003cem\u003eabsolute\u003c/em\u003e (see \u003ca href=\"http://www.ietf.org/rfc/rfc2965.txt\"\u003eRFC 2965\u003c/a\u003e). For more information on how to use relative paths in this argument, see \u003ca href=\"https://developer.mozilla.org/en-US/docs/Web/API/document/cookie#Using_relative_URLs_in_the_path_parameter\"\u003ethis paragraph.\u003c/a\u003e\u003c/dd\u003e\n\u003cdt\u003e\u003cvar\u003edomain\u003c/var\u003e (optional)\u003c/dt\u003e\n\u003cdd\u003eThe \u003ca href=\"https://developer.mozilla.org/en-US/docs/Web/API/document/cookie#new-cookie_domain\"\u003edomain\u003c/a\u003e from where the cookie will be readable. E.g., \u003ccode\u003e\"example.com\"\u003c/code\u003e, \u003ccode\u003e\".example.com\"\u003c/code\u003e (includes all subdomains) or \u003ccode\u003e\"subdomain.example.com\"\u003c/code\u003e; if not specified, defaults to the host portion of the current document location (\u003ca href=\"https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String\"\u003e\u003ccode\u003estring\u003c/code\u003e\u003c/a\u003e or \u003ca href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null\"\u003e\u003ccode\u003enull\u003c/code\u003e\u003c/a\u003e).\u003c/dd\u003e\n\u003cdt\u003e\u003cvar\u003esecure\u003c/var\u003e (optional)\u003c/dt\u003e\n\u003cdd\u003eThe cookie will be transmitted only over \u003ca href=\"https://developer.mozilla.org/en-US/docs/Web/API/document/cookie#new-cookie_secure\"\u003esecure\u003c/a\u003e protocol as https (\u003ca href=\"https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Boolean\"\u003e\u003ccode\u003eboolean\u003c/code\u003e\u003c/a\u003e or \u003ca href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null\"\u003e\u003ccode\u003enull\u003c/code\u003e\u003c/a\u003e).\u003c/dd\u003e\n\u003c/dl\u003e\n\n### Getting a cookie\n###### Syntax\n``` javascript\ndocCookies.getItem(name)\n```\n###### Description\nRead a cookie. If the cookie doesn't exist a [`null`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null) value will be returned.\n\n###### Parameters\n\u003cdl\u003e\n\u003cdt\u003ename\u003c/dt\u003e\n\u003cdd\u003eThe \u003ca href=\"https://developer.mozilla.org/en-US/docs/Web/API/document/cookie#new-cookie_syntax\"\u003ename\u003c/a\u003e of the cookie to read (\u003ca href=\"https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String\"\u003e\u003ccode\u003estring\u003c/code\u003e\u003c/a\u003e).\u003c/dd\u003e\n\u003c/dl\u003e\n\n### Removing a cookie\n\n###### Syntax\n``` javascript\ndocCookies.removeItem(name[, path[, domain]])\n```\n\n###### Description\nDelete a cookie.\n\n###### Parameters\n\u003cdl\u003e\n\u003cdt\u003e\u003cvar\u003ename\u003c/var\u003e\u003c/dt\u003e\n\u003cdd\u003eThe \u003ca href=\"https://developer.mozilla.org/en-US/docs/Web/API/document/cookie#new-cookie_syntax\"\u003ename\u003c/a\u003e of the cookie to remove (\u003ca href=\"https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String\"\u003e\u003ccode\u003estring\u003c/code\u003e\u003c/a\u003e).\u003c/dd\u003e\n\u003cdt\u003e\u003cvar\u003epath\u003c/var\u003e (optional)\u003c/dt\u003e\n\u003cdd\u003eE.g., \u003ccode\u003e\"/\"\u003c/code\u003e, \u003ccode\u003e\"/mydir\"\u003c/code\u003e; if not specified, defaults to the current path of the current document location (\u003ca href=\"https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String\"\u003e\u003ccode\u003estring\u003c/code\u003e\u003c/a\u003e or \u003ca href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null\"\u003e\u003ccode\u003enull\u003c/code\u003e\u003c/a\u003e). The path must be \u003cem\u003eabsolute\u003c/em\u003e (see \u003ca href=\"http://www.ietf.org/rfc/rfc2965.txt\"\u003eRFC 2965\u003c/a\u003e). For more information on how to use relative paths in this argument, see \u003ca href=\"https://developer.mozilla.org/en-US/docs/Web/API/document/cookie#Using_relative_URLs_in_the_path_parameter\"\u003ethis paragraph\u003c/a\u003e.\u003c/dd\u003e\n\u003cdt\u003e\u003cvar\u003edomain\u003c/var\u003e (optional)\u003c/dt\u003e\n\u003cdd\u003eE.g., \u003ccode\u003e\"example.com\"\u003c/code\u003e, \u003ccode\u003e\".example.com\"\u003c/code\u003e (includes all subdomains) or \u003ccode\u003e\"subdomain.example.com\"\u003c/code\u003e; if not specified, defaults to the host portion of the current document location (\u003ca href=\"https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String\"\u003e\u003ccode\u003estring\u003c/code\u003e\u003c/a\u003e or \u003ca href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null\"\u003e\u003ccode\u003enull\u003c/code\u003e\u003c/a\u003e).\n\u003cblockquote\u003e\n\u003cp\u003e\u003cem\u003eNote: To delete cookies that span over subdomains, you need to explicitate the domain attribute in \u003ccode\u003eremoveItem()\u003c/code\u003e as well as \u003ccode\u003esetItem()\u003c/code\u003e.\u003c/em\u003e\u003c/p\u003e\n\u003c/blockquote\u003e\n\u003c/dd\u003e\n\u003c/dl\u003e\n\n### Testing a cookie\n\n###### Syntax\n``` javascript\ndocCookies.hasItem(name)\n```\n\n###### Description\nCheck whether a cookie exists in the current position.\n\n###### Parameters\n\u003cdl\u003e\n\u003cdt\u003e\u003cvar\u003ename\u003c/var\u003e\u003c/dt\u003e\n\u003cdd\u003eThe \u003ca href=\"https://developer.mozilla.org/en-US/docs/Web/API/document/cookie#new-cookie_syntax\"\u003ename\u003c/a\u003e of the cookie to test (\u003ca href=\"https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String\"\u003e\u003ccode\u003estring\u003c/code\u003e\u003c/a\u003e).\u003c/dd\u003e\n\u003c/dl\u003e\n\n### Getting the list of all cookies\n###### Syntax\n``` javascript\ndocCookies.keys()\n```\n\n###### Description\nReturns an array of all readable cookies from this location.\n\n## Example usage:\n\n``` javascript\ndocCookies.setItem(\"test0\", \"Hello world!\");\ndocCookies.setItem(\"test1\", \"Unicode test: \\u00E0\\u00E8\\u00EC\\u00F2\\u00F9\", Infinity);\ndocCookies.setItem(\"test2\", \"Hello world!\", new Date(2020, 5, 12));\ndocCookies.setItem(\"test3\", \"Hello world!\", new Date(2027, 2, 3), \"/blog\");\ndocCookies.setItem(\"test4\", \"Hello world!\", \"Wed, 19 Feb 2127 01:04:55 GMT\");\ndocCookies.setItem(\"test5\", \"Hello world!\", \"Fri, 20 Aug 88354 14:07:15 GMT\", \"/home\");\ndocCookies.setItem(\"test6\", \"Hello world!\", 150);\ndocCookies.setItem(\"test7\", \"Hello world!\", 245, \"/content\");\ndocCookies.setItem(\"test8\", \"Hello world!\", null, null, \"example.com\");\ndocCookies.setItem(\"test9\", \"Hello world!\", null, null, null, true);\ndocCookies.setItem(\"test1;=\", \"Safe character test;=\", Infinity);\n \nalert(docCookies.keys().join(\"\\n\"));\nalert(docCookies.getItem(\"test1\"));\nalert(docCookies.getItem(\"test5\"));\ndocCookies.removeItem(\"test1\");\ndocCookies.removeItem(\"test5\", \"/home\");\nalert(docCookies.getItem(\"test1\"));\nalert(docCookies.getItem(\"test5\"));\nalert(docCookies.getItem(\"unexistingCookie\"));\nalert(docCookies.getItem());\nalert(docCookies.getItem(\"test1;=\"));\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjasonkarns%2Fdoc-cookies","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjasonkarns%2Fdoc-cookies","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjasonkarns%2Fdoc-cookies/lists"}