npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tiny-cookie

v2.5.1

Published

A tiny cookie manipulation plugin

Downloads

208,661

Readme

tiny-cookie

Node.js CI codecov npm npm

English | 简体中文

A tiny cookie manipulation plugin for browser.

Upgrade from 1.x to 2.x: You can check the CHANGELOG.md

If you're used tiny-cookie, then you may be interest in micell, a collection of functions focusing on web development.

Install

NPM:

npm install tiny-cookie

Usage

ES2015 (recommended)

// You can import all methods.
import * as Cookies from 'tiny-cookie'

// Or, you can import the methods as needed.
import { isEnabled, get, set, remove } from 'tiny-cookie'

// No alias required, just imports.
import { isCookieEnabled, getCookie, setCookie, removeCookie } from 'tiny-cookie'

The tiny-cookie will expose an object Cookie on the global scope. Also, it can be as a CommonJS/AMD module (recommended).

APIs

isEnabled()

Alias: isCookieEnabled

Check if the cookie is enabled.

get(key)

Alias: getCookie

Get the cookie value with decoding, using decodeURIComponent.

getRaw(key)

Alias: getRawCookie

Get the cookie value without decoding.

getAll()

Alias: getAllCookies

Get all cookies with decoding, using decodeURIComponent.

set(key, value, options)

Alias: setCookie

Set a cookie with encoding the value, using encodeURIComponent. The options parameter is an object. And its property can be a valid cookie option, such as path(default: root path /), domain, expires/max-age, samesite or secure (Note: the secure flag will be set if it is an truthy value, such as true, or it will be not set). For example, you can set the expiration:

import { setCookie } from 'tiny-cookie';

const now = new Date;
now.setMonth(now.getMonth() + 1);

setCookie('foo', 'Foo', { expires: now.toGMTString() });

The expires property value can accept a Date object, a parsable date string (parsed by Date.parse()), an integer (unit: day) or a numeric string with a suffix character which specifies the time unit.

| Unit suffix | Representation | | ----------- | -------------- | | Y | One year | | M | One month | | D | One day | | h | One hour | | m | One minute | | s | One second |

Examples:

import { setCookie } from 'tiny-cookie';
const date = new Date;

date.setDate(date.getDate() + 21);

setCookie('dateObject', 'A date object', { expires: date });
setCookie('dateString', 'A parsable date string', { expires: date.toGMTString() });
setCookie('integer', 'Seven days later', { expires: 7 });
setCookie('stringSuffixY', 'One year later', { expires: '1Y' });
setCookie('stringSuffixM', 'One month later', { expires: '1M' });
setCookie('stringSuffixD', 'One day later', { expires: '1D' });
setCookie('stringSuffixh', 'One hour later', { expires: '1h' });
setCookie('stringSuffixm', 'Ten minutes later', { expires: '10m' });
setCookie('stringSuffixs', 'Thirty seconds later', { expires: '30s' });

setRaw(key, value, options)

Alias: setRawCookie

Set a cookie without encoding.

remove(key, options)

Alias: removeCookie

Remove a cookie on the current domain. If you want to remove the parent domain's cookie, you can use the options parameter, such as remove('cookieName', { domain: 'parentdomain.com' }).

FAQ

  1. How to use JSON as the encoder/decoder?

You can write your cookie get and set methods with JSON support easily:

import { getCookie, setCookie } from 'tiny-cookie';

export const getJSON = (key) => getCookie(key, JSON.parse);
export const setJSON = (key, value, options) => setCookie(key, value, JSON.stringify, options);

License

MIT.