An ultra-light (403 bytes) library for working with cookies in JavaScript
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
__tests__ Delete processing to false in the option value (#41) Jan 13, 2019
src Take out the document.cookie to variable (#46) Feb 13, 2019
.babelrc Initial commit Nov 16, 2018
.gitignore
.prettierrc Add prettier (#11) Nov 23, 2018
.travis.yml To move coveralls in the package.json (#18) Dec 3, 2018
CHANGELOG.md Add changelog for 4.1.0 Feb 13, 2019
LICENSE Create LICENSE Nov 18, 2018
README.md 4.1.0 Feb 13, 2019
package.json
rollup.config.js

README.md

hardtack

NPM Travis Coverage Status

An ultra-light library for working with cookies in JavaScript.

Features

  • Simple API
  • Ultra-light (403 bytes gzipped)
  • Encoding of forbidden characters
  • Use as a module or add to your browser

Example

hardtack.set('name', 'Ali', {
  path: '/',
  domain: 'alik0211.com',
  expires: 'Sun, 10 Jan 9999 00:00:00 GMT',
  samesite: 'lax'
});

hardtack.get(); // { name: 'Ali' };

hardtack.get('name'); // Ali;

hardtack.remove('name', {
  path: '/',
  domain: 'alik0211.com'
});

Table of contents

Quick start

Module

Installation

npm install hardtack

Usage

import hardtack from 'hardtack';

hardtack.set('name', 'Ali', {
  path: '/',
  domain: 'alik0211.com',
  expires: 'Sun, 10 Jan 9999 00:00:00 GMT',
  samesite: 'lax'
});

hardtack.get(); // { name: 'Ali' };

hardtack.get('name'); // Ali;

hardtack.remove('name', {
  path: '/',
  domain: 'alik0211.com'
});

Browser

Installation

<script src="https://cdn.jsdelivr.net/npm/hardtack@4.1.0/dist/hardtack.min.js"></script>

Usage

<script>
  hardtack.set('name', 'Ali', {
    path: '/',
    domain: 'alik0211.com',
    expires: 'Sun, 10 Jan 9999 00:00:00 GMT',
    samesite: 'lax'
  });

  hardtack.get(); // { name: 'Ali' };

  hardtack.get('name'); // Ali;

  hardtack.remove('name', {
    path: '/',
    domain: 'alik0211.com'
  });
</script>

Methods

set(name: string, value: string, options)

Create a cookie

options.path: string

For example: '/', '/mydir'

If not specified, defaults to the current path of the current document location.

options.domain: string

For example: example.com or subdomain.example.com

If not specified, this defaults to the host portion of the current document location. Contrary to earlier specifications, leading dots in domain names are ignored, but browsers may decline to set the cookie containing such dots. If a domain is specified, subdomains are always included.

options.maxAge: number

You can use options['max-age'], the result will be the same.

Max age in seconds. For example: 60*60*24*365 or 31536000 for a year

options.expires: string

Date in GMT format. See Date.toUTCString() for help formatting this value.

options.secure: boolean

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 for the flag are lax or strict.

More about the options of cookies on MDN.

get(name: string)

Get cookie value

remove(name: string, options)

Remove cookie. When you delete a cookie, you must pass the same options.path and options.domain that you passed when you created the cookie.