Git Product home page Git Product logo

hardtack's Introduction

hardtack

NPM Travis Coverage Status

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

Features

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

Example

hardtack.set('name', 'Ali', {
  path: '/',
  domain: 'gasymov.com',
  maxAge: 31536000, // one year
  samesite: 'lax'
});

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

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

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

Table of contents

Quick start

Module

Installation

npm install hardtack

Usage

import hardtack from 'hardtack';

hardtack.set('name', 'Ali', {
  path: '/',
  domain: 'gasymov.com',
  maxAge: 31536000, // 1 year
  samesite: 'lax'
});

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

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

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

Browser

Installation

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/hardtack.min.js"></script>

Usage

<script>
  hardtack.set('name', 'Ali', {
    path: '/',
    domain: 'gasymov.com',
    maxAge: 31536000, // 1 year
    samesite: 'lax'
  });

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

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

  hardtack.remove('name', {
    path: '/',
    domain: 'gasymov.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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.