ts-cookie-lite 2.0.0 → 2.0.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +41 -0
- package/dist/cookie.d.ts +8 -0
- package/dist/cookie.js +40 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/package.json +7 -2
package/README.md
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# ts-cookie-lite
|
|
2
|
+
|
|
3
|
+
Lightweight, type-safe cookie utility library for web developers.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install ts-cookie-lite
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { Injectable } from '@angular/core';
|
|
15
|
+
import { CookieUtils } from 'ts-cookie-lite';
|
|
16
|
+
|
|
17
|
+
@Injectable({ providedIn: 'root' })
|
|
18
|
+
export class MyService {
|
|
19
|
+
constructor(private cookie: CookieUtils) { }
|
|
20
|
+
|
|
21
|
+
myMethod(): void {
|
|
22
|
+
this.cookie.set('theme', 'dark', 7); // Set cookie for 7 days
|
|
23
|
+
const theme = this.cookie.get('theme'); // Get cookie
|
|
24
|
+
const has = this.cookie.has('theme'); // Check if exists
|
|
25
|
+
this.cookie.delete('theme'); // Delete cookie
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Methods
|
|
31
|
+
|
|
32
|
+
| Method | Description | Example |
|
|
33
|
+
|--------|-------------|---------|
|
|
34
|
+
| `set` | Set cookie | `set('theme', 'dark', 7)` |
|
|
35
|
+
| `get` | Get cookie | `get('theme')` → `'dark'` |
|
|
36
|
+
| `has` | Check if exists | `has('theme')` → `true/false` |
|
|
37
|
+
| `delete` | Delete cookie | `delete('theme')` |
|
|
38
|
+
|
|
39
|
+
## License
|
|
40
|
+
|
|
41
|
+
MIT
|
package/dist/cookie.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export declare class CookieUtils {
|
|
2
|
+
set(key: string, value: string, days?: number, path?: string): void;
|
|
3
|
+
get(key: string): string | null;
|
|
4
|
+
has(key: string): boolean;
|
|
5
|
+
delete(key: string, path?: string): void;
|
|
6
|
+
clear(): void;
|
|
7
|
+
all(): Record<string, string>;
|
|
8
|
+
}
|
package/dist/cookie.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export class CookieUtils {
|
|
2
|
+
set(key, value, days = 7, path = '/') {
|
|
3
|
+
const expires = new Date(Date.now() + days * 864e5).toUTCString();
|
|
4
|
+
document.cookie = `${encodeURIComponent(key)}=${encodeURIComponent(value)};expires=${expires};path=${path}`;
|
|
5
|
+
}
|
|
6
|
+
get(key) {
|
|
7
|
+
const cookies = document.cookie.split(';');
|
|
8
|
+
for (const cookie of cookies) {
|
|
9
|
+
const [cookieKey, ...valParts] = cookie.trim().split('=');
|
|
10
|
+
if (decodeURIComponent(cookieKey) === key) {
|
|
11
|
+
return decodeURIComponent(valParts.join('='));
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
has(key) {
|
|
17
|
+
return this.get(key) !== null;
|
|
18
|
+
}
|
|
19
|
+
delete(key, path = '/') {
|
|
20
|
+
document.cookie = `${encodeURIComponent(key)}=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=${path}`;
|
|
21
|
+
}
|
|
22
|
+
clear() {
|
|
23
|
+
const cookies = document.cookie.split(';');
|
|
24
|
+
for (const cookie of cookies) {
|
|
25
|
+
const key = cookie.trim().split('=')[0];
|
|
26
|
+
this.delete(decodeURIComponent(key));
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
all() {
|
|
30
|
+
const result = {};
|
|
31
|
+
const cookies = document.cookie.split(';');
|
|
32
|
+
for (const cookie of cookies) {
|
|
33
|
+
const [cookieKey, ...valParts] = cookie.trim().split('=');
|
|
34
|
+
const key = decodeURIComponent(cookieKey);
|
|
35
|
+
const value = decodeURIComponent(valParts.join('='));
|
|
36
|
+
result[key] = value;
|
|
37
|
+
}
|
|
38
|
+
return result;
|
|
39
|
+
}
|
|
40
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './cookie';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './cookie';
|
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ts-cookie-lite",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.16",
|
|
4
|
+
"deprecated": "Previous versions are deprecated. Please upgrade to 2.0.15 or later.",
|
|
4
5
|
"description": "Lightweight, type-safe cookie utility library for web developers",
|
|
5
6
|
"main": "./dist/index.js",
|
|
6
7
|
"module": "./dist/index.js",
|
|
@@ -26,5 +27,9 @@
|
|
|
26
27
|
"lite"
|
|
27
28
|
],
|
|
28
29
|
"author": "Gaurang Mody - G8X",
|
|
29
|
-
"license": "MIT"
|
|
30
|
+
"license": "MIT",
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "https://github.com/gbmrocks/ts-utils-lite.git"
|
|
34
|
+
}
|
|
30
35
|
}
|