@sswahn/cookie
Advanced tools
Comparing version 1.0.0-beta.2 to 1.0.0-beta.3
{ | ||
"name": "@sswahn/cookie", | ||
"version": "1.0.0-beta.2", | ||
"version": "1.0.0-beta.3", | ||
"license": "MIT", | ||
"description": "A JavaScript utility for efficiently managing browser cookies.", | ||
"description": "A lightweight, easy-to-use utility for efficiently managing cookies in a web browser.", | ||
"type": "module", | ||
"main": "src/index.js", | ||
"keywords": [ | ||
"cookie" | ||
"cookie", | ||
"utility" | ||
], | ||
@@ -11,0 +12,0 @@ "repository": { |
# Cookie | ||
A JavaScript utility for efficiently managing browser cookies. | ||
A lightweight, easy-to-use utility for efficiently managing cookies in a web browser. | ||
## Features | ||
- **Get Cookie**: Retrieve the value of a cookie. | ||
- **Set Cookie**: Set a cookie with optional attributes. | ||
- **Remove Cookie**: Easily delete a cookie. | ||
## Installation | ||
Using npm. | ||
```bash | ||
npm install @sswahn/cookie | ||
``` | ||
## Usage | ||
Import library. | ||
```javascript | ||
@@ -13,5 +22,3 @@ import cookie from '@sswahn/cookie' | ||
### Get Cookie | ||
Retrieve a cookie by name. | ||
```javascript | ||
@@ -22,5 +29,3 @@ const token = cookie.get('name') | ||
### Set Cookie | ||
Set a cookie, with optional expiration argument (a numeric value representing the lifetime of the cookie in seconds). | ||
```javascript | ||
@@ -31,5 +36,3 @@ cookie.set('name', value, expiration) | ||
### Remove Cookie | ||
Remove a cookie. | ||
```javascript | ||
@@ -36,0 +39,0 @@ cookie.remove('name') |
@@ -6,14 +6,9 @@ const cookie = { | ||
} | ||
try { | ||
const cookies = document.cookie.split(';') | ||
for (let cookie of cookies) { | ||
const [cookieName, ...cookieValueParts] = cookie.trim().split('=') | ||
const cookieValue = cookieValueParts.join('=') | ||
if (cookieName === name) { | ||
return cookieValue | ||
} | ||
const cookies = document.cookie.split(';') | ||
for (let cookie of cookies) { | ||
const [cookieName, ...cookieValueParts] = cookie.trim().split('=') | ||
const cookieValue = cookieValueParts.join('=') | ||
if (cookieName === name) { | ||
return cookieValue | ||
} | ||
return undefined | ||
} catch (error) { | ||
throw new Error(`Error getting cookie: ${name}`) | ||
} | ||
@@ -25,3 +20,3 @@ }, | ||
} | ||
if (!value) { | ||
if (value === undefined || value == null) { | ||
throw new Error('Set cookie must have second argument.') | ||
@@ -28,0 +23,0 @@ } |
3778
41
45