ts-cookie-lite 1.0.0
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/dist/cookie.d.ts +8 -0
- package/dist/cookie.js +44 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +17 -0
- package/package.json +19 -0
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,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CookieUtils = void 0;
|
|
4
|
+
class CookieUtils {
|
|
5
|
+
set(key, value, days = 7, path = '/') {
|
|
6
|
+
const expires = new Date(Date.now() + days * 864e5).toUTCString();
|
|
7
|
+
document.cookie = `${encodeURIComponent(key)}=${encodeURIComponent(value)};expires=${expires};path=${path}`;
|
|
8
|
+
}
|
|
9
|
+
get(key) {
|
|
10
|
+
const cookies = document.cookie.split(';');
|
|
11
|
+
for (const cookie of cookies) {
|
|
12
|
+
const [cookieKey, ...valParts] = cookie.trim().split('=');
|
|
13
|
+
if (decodeURIComponent(cookieKey) === key) {
|
|
14
|
+
return decodeURIComponent(valParts.join('='));
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
has(key) {
|
|
20
|
+
return this.get(key) !== null;
|
|
21
|
+
}
|
|
22
|
+
delete(key, path = '/') {
|
|
23
|
+
document.cookie = `${encodeURIComponent(key)}=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=${path}`;
|
|
24
|
+
}
|
|
25
|
+
clear() {
|
|
26
|
+
const cookies = document.cookie.split(';');
|
|
27
|
+
for (const cookie of cookies) {
|
|
28
|
+
const key = cookie.trim().split('=')[0];
|
|
29
|
+
this.delete(decodeURIComponent(key));
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
all() {
|
|
33
|
+
const result = {};
|
|
34
|
+
const cookies = document.cookie.split(';');
|
|
35
|
+
for (const cookie of cookies) {
|
|
36
|
+
const [cookieKey, ...valParts] = cookie.trim().split('=');
|
|
37
|
+
const key = decodeURIComponent(cookieKey);
|
|
38
|
+
const value = decodeURIComponent(valParts.join('='));
|
|
39
|
+
result[key] = value;
|
|
40
|
+
}
|
|
41
|
+
return result;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
exports.CookieUtils = CookieUtils;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './cookie';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./cookie"), exports);
|
package/package.json
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ts-cookie-lite",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Lightweight, type-safe cookie utility library for web developers",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"require": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": ["dist"],
|
|
16
|
+
"keywords": ["utils", "typescript", "cookie", "helpers", "lite"],
|
|
17
|
+
"author": "Gaurang Mody - G8X",
|
|
18
|
+
"license": "MIT"
|
|
19
|
+
}
|