Skip to content

Cookie mapper on browser by ECMAScript 2019+

License

Notifications You must be signed in to change notification settings

urin/pico-cookie

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pico-cookie - Cookie mapper on browser

Overview

pico-cookie provides the cookie object mapping to document.cookie.

Note that any direct operations to document.cookie are not reflected to the object cookie.

Keys and values are encoded and decoded by encodeURIComponent and decodeURIComponent.

Requirement

  • Chrome 73+
  • Edge 79+
  • Firefox 63+
  • Safari 12.1+
  • Opera 60+
  • Internet Explorer is not supported
  • Node.js 12.0.0+

Getting started

  • Using npm
npm install pico-cookie
  • Using ES6 modules on browser
<script type="module">
import cookie from './pico-cookie.mjs'

// ...

</script>
  • On browser
<script src="./pico-cookie.js"></script>

Usage

// document.cookie is mapped to cookie
document.cookie  // "SESSIONID=04ecb3d1b; APISID=ed724274d"
cookie           // { SESSIONID: "04ecb3d1b", APISID: "ed724274d" }

// Read
cookie.SESSIONID // "04ecb3d1b"
cookie.APISID    // "ed724274d"

// Update
cookie.SESSIONID = 'abcd'
cookie           // { SESSIONID: "abcd", APISID: "ed724274d" }
document.cookie  // "SESSIONID=abcd; APISID=ed724274d"

// Add
cookie.NEWKEY = 'NEWVALUE'
cookie           // { SESSIONID: "abcd", APISID: "ed724274d", NEWKEY: "NEWVALUE" }
document.cookie  // "SESSIONID=abcd; APISID=ed724274d; NEWKEY=NEWVALUE"

// Delete
delete cookie.NEWKEY
cookie           // { SESSIONID: "abcd", APISID: "ed724274d" }
document.cookie  // "SESSIONID=abcd; APISID=ed724274d"

// Attributes
cookie.put('KEY', 'VALUE', { samesite: 'strict' }) // returns cookie itself

// You can apply any methods same as plain object...
Object.keys(cookie) // ["SESSIONID", "APISID", "NEWKEY"]

for (const [key, value] of Object.entries(cookie)) {
  console.log(key, value)
}

License

MIT

Author

urin

About

Cookie mapper on browser by ECMAScript 2019+

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published