# tiny-cookie

> A tiny cookie manipulation plugin

Latest version **2.5.1** (published 2023-11-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install tiny-cookie
pnpm add tiny-cookie
yarn add tiny-cookie
bun add tiny-cookie
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.5.1 |
| Published | 2023-11-04 |
| First published | 2015-04-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 38.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 111 |
| Author | Alex Chao |
| Maintainers | alexchao |
| Keywords | cookie, browser cookie, tiny, tiny cookie |

## Links

- npm: https://www.npmjs.com/package/tiny-cookie
- Repository: https://github.com/Alex1990/tiny-cookie
- Homepage: https://github.com/Alex1990/tiny-cookie#readme
- Issues: https://github.com/Alex1990/tiny-cookie/issues
- npm.io page: https://npm.io/package/tiny-cookie

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 2.5.1 (latest) — 2023-11-04
- 2.5.0 — 2023-10-18
- 2.4.1 — 2023-03-07
- 2.4.0 — 2023-02-05
- 2.3.2 — 2020-03-14
- 2.3.1 — 2019-01-11
- 2.3.0 — 2018-11-22
- 2.2.0 — 2018-10-30
- 2.1.2 — 2018-04-24
- 2.1.1 — 2017-11-20
- 2.1.0 — 2017-11-18
- 2.0.2 — 2017-08-21
- 2.0.1 — 2017-07-11
- 2.0.0 — 2017-07-06
- 1.0.1 — 2016-11-07
- … 11 more at https://npm.io/package/tiny-cookie/versions

## README

# tiny-cookie

[![Node.js CI](https://github.com/Alex1990/tiny-cookie/actions/workflows/node.js.yml/badge.svg?branch=main)](https://github.com/Alex1990/tiny-cookie/actions/workflows/node.js.yml)
[![codecov](https://codecov.io/gh/Alex1990/tiny-cookie/branch/main/graph/badge.svg)](https://codecov.io/gh/Alex1990/tiny-cookie)
[![npm](https://img.shields.io/npm/dm/tiny-cookie.svg)](https://www.npmjs.com/package/tiny-cookie)
[![npm](https://img.shields.io/npm/v/tiny-cookie.svg)](https://www.npmjs.com/package/tiny-cookie)

**English | [简体中文](README_zh-CN.md)**

A tiny cookie manipulation plugin for browser.

**Upgrade from 1.x to 2.x**: You can check the [CHANGELOG.md](https://github.com/Alex1990/tiny-cookie/blob/master/CHANGELOG.md#v200)

> If you're used tiny-cookie, then you may be interest in [micell](https://micell.org), a collection of functions focusing on web development.

## Install

**NPM:**

```bash
npm install tiny-cookie
```

## Usage

**ES2015 (recommended)**

```js
// 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:

```js
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:**

```js
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:

```js
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.

---
_Source: https://npm.io/package/tiny-cookie · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
