cookiestorage
v1.0.0
Published
Browserify-compatible module to get and set cookies in the browser using Storage API
Downloads
229
Readme
cookiestorage
Manage document.cookie
using the Storage API.
NOTE: originally a fork of cookie-monster, itself a fork of cookie-cutter.
install
npm install cookiestorage
api
var cookieStorage = require('cookiestorage');
var cookie = cookieStorage(document);
cookieStorage(document)
Return a new cookie object with .getItem()
and .setItem()
operating on document
.
The provided document
should have a non-referentially transparent cookie
property
like the DOM's variant where assignment with optional path and expiry creates a
new cookie in the getter as a key=value pair.
If document is not provided, uses the global document
if it exists. Otherwise, creates a new plain object {cookie:''}
.
If given a string, uses { cookie: givenString }
.
#length
Returns number of key/value pairs present in document.cookie
#getItem(key)
Returns the cookie value for key
.
#setItem(key, value, options={})
Sets the cookie at key
to value
with additional options:
expires
: Will be parsed bynew Date(expires)
which acceptsString
formatted per RFC 2822 or ISO-8601,Number
of milliseconds since 1 January 1970 00:00:00 UTC (Unix Epoch), or literalDate
objects.domain
: see cookie documentationpath
: see cookie documentationsecure
: see cookie documentation
#key(index)
Returns the key (name) of the nth key/value pair in document.cookie
. Order is dependent on browser implementation. May not be alphabetical, or even consistent. Use this with caution. (It is added solely for consistency with the Storage API.)
#removeItem(key)
Removes the given key from document.cookie
.
#clear()
Clears document.cookie
of all key/value pairs.