@jsarc/cooks 0.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/README.md +372 -0
- package/cooks.ts +1159 -0
- package/index.ts +274 -0
- package/js/index.js +189 -0
- package/package.json +20 -0
- package/ts/cooks.ts +1159 -0
- package/ts/index.ts +274 -0
- package/tsconfig.json +27 -0
- package/web/cooks.js +920 -0
- package/web/cooks.min.js +1 -0
package/ts/index.ts
ADDED
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
import { Timez } from '@jsarc/timez';
|
|
2
|
+
|
|
3
|
+
// @ts-nocheck
|
|
4
|
+
|
|
5
|
+
interface Window {
|
|
6
|
+
default: typeof defaultElementGF;
|
|
7
|
+
Cooks: any;
|
|
8
|
+
__bundledModules: any;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
interface CooksOptions {
|
|
12
|
+
expires?: Date | number;
|
|
13
|
+
path?: string;
|
|
14
|
+
domain?: string;
|
|
15
|
+
secure?: boolean;
|
|
16
|
+
sameSite?: 'strict' | 'lax' | 'none';
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
type Serializable =
|
|
21
|
+
| string
|
|
22
|
+
| number
|
|
23
|
+
| boolean
|
|
24
|
+
| Date
|
|
25
|
+
| object
|
|
26
|
+
| Array<any>
|
|
27
|
+
| null
|
|
28
|
+
| undefined;
|
|
29
|
+
|
|
30
|
+
const globalFunct = (function(global: any) {
|
|
31
|
+
'use strict';
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
const NODEENV: 'development' | 'production' | 'debug' = 'development'; // development | debug | production
|
|
35
|
+
|
|
36
|
+
const langs = ['en', 'fr'];
|
|
37
|
+
const langCodes = {
|
|
38
|
+
'fr': 'fr_FR',
|
|
39
|
+
'en': 'en_US',
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const dateTimeFormatInitial = '%Y-%m-%dT%H:%M:%S.%fZ';
|
|
43
|
+
const dateFormatInitial = '%Y-%m-%d';
|
|
44
|
+
const timeFormatInitial = '%H:%M:%S.%fZ';
|
|
45
|
+
|
|
46
|
+
const dateFormatForFile = '%Y%m%d%H%M%S';
|
|
47
|
+
const dateFormat1 = '%Y/%m/%d %H:%M:%S.%fZ';
|
|
48
|
+
const dateFormat2 = '%Y/%m/%d %H:%M:%S';
|
|
49
|
+
const dateFormat3 = '%Y/%m/%d %H:%M';
|
|
50
|
+
const dateFormat4 = '%d/%m/%Y %H:%M:%S GMT%z';
|
|
51
|
+
const dateFormat5 = '%Y/%m/%d';
|
|
52
|
+
const timeFormat1 = '%H:%M:%S.%fZ';
|
|
53
|
+
const timeFormat2 = '%H:%M:%S';
|
|
54
|
+
const timeFormat3 = '%H:%M:%S.%f';
|
|
55
|
+
const pagesPossibles = [5, 10, 15, 25, 50, 100, -1];
|
|
56
|
+
|
|
57
|
+
const regExpForAlphanumeric = /^[\w\s]{1,}/;
|
|
58
|
+
|
|
59
|
+
const tabNumerique = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
|
|
60
|
+
const tabAlphabetique = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
|
|
61
|
+
const tabAlphabetiqueInsensitive = [...tabAlphabetique, ...tabAlphabetique.map(x => x.toUpperCase())];
|
|
62
|
+
const tabAlphanumerique = [...tabNumerique, ...tabAlphabetique];
|
|
63
|
+
const tabAlphanumeriqueInsensitive = [...tabNumerique, ...tabAlphabetiqueInsensitive];
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
class Cooks {
|
|
72
|
+
private static DEFAULT_OPTIONS: CooksOptions = {
|
|
73
|
+
path: '/',
|
|
74
|
+
secure: true,
|
|
75
|
+
sameSite: 'lax' as const
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
public static set<T extends Serializable>(key: string, value: T, options: CooksOptions = {}): void {
|
|
79
|
+
if (!this.isBrowser()) return;
|
|
80
|
+
|
|
81
|
+
try {
|
|
82
|
+
const serializedValue = this.serialize(value);
|
|
83
|
+
const encodedValue = encodeURIComponent(serializedValue);
|
|
84
|
+
const cookieString = this.buildCookieString(key, encodedValue, options);
|
|
85
|
+
|
|
86
|
+
document.cookie = cookieString;
|
|
87
|
+
} catch (error) {
|
|
88
|
+
console.error(`Cooks: Error setting cookie "${key}":`, error);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
public static get<T extends Serializable>(key: string): T | null {
|
|
93
|
+
if (!this.isBrowser()) return null;
|
|
94
|
+
|
|
95
|
+
try {
|
|
96
|
+
const cookies = this.getAllCookies();
|
|
97
|
+
const encodedValue = cookies[key];
|
|
98
|
+
|
|
99
|
+
if (!encodedValue) return null;
|
|
100
|
+
|
|
101
|
+
const decodedValue = decodeURIComponent(encodedValue);
|
|
102
|
+
return this.deserialize<T>(decodedValue);
|
|
103
|
+
} catch (error) {
|
|
104
|
+
console.error(`Cooks: Error getting cookie "${key}":`, error);
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
public static remove(key: string, path: string = '/', domain?: string): void {
|
|
110
|
+
if (!this.isBrowser()) return;
|
|
111
|
+
|
|
112
|
+
const options: CooksOptions = {
|
|
113
|
+
expires: new Date(0),
|
|
114
|
+
path,
|
|
115
|
+
domain
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
this.set(key, '', options);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
public static has(key: string): boolean {
|
|
122
|
+
return this.get(key) !== null;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
public static keys(): string[] {
|
|
126
|
+
if (!this.isBrowser()) return [];
|
|
127
|
+
|
|
128
|
+
const cookies = this.getAllCookies();
|
|
129
|
+
return Object.keys(cookies);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
public static clear(): void {
|
|
133
|
+
if (!this.isBrowser()) return;
|
|
134
|
+
|
|
135
|
+
const cookies = this.getAllCookies();
|
|
136
|
+
Object.keys(cookies).forEach(key => {
|
|
137
|
+
this.remove(key);
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
private static serialize(value: Serializable): string {
|
|
142
|
+
if (value instanceof Date) {
|
|
143
|
+
return JSON.stringify({
|
|
144
|
+
__type: 'Date',
|
|
145
|
+
__value: value.toISOString()
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return JSON.stringify(value);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
private static deserialize<T>(value: string): T {
|
|
153
|
+
const parsed = JSON.parse(value);
|
|
154
|
+
|
|
155
|
+
if (parsed && typeof parsed === 'object' && parsed.__type === 'Date') {
|
|
156
|
+
return new Date(parsed.__value) as T;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return parsed as T;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
private static buildCookieString(key: string, value: string, options: CooksOptions): string {
|
|
163
|
+
const mergedOptions = { ...this.DEFAULT_OPTIONS, ...options };
|
|
164
|
+
let cookieString = `${key}=${value}`;
|
|
165
|
+
|
|
166
|
+
if (mergedOptions.expires !== undefined) {
|
|
167
|
+
let expirationDate: Date;
|
|
168
|
+
|
|
169
|
+
if (typeof mergedOptions.expires === 'number') {
|
|
170
|
+
const expirationDateInitial = new Timez().add(mergedOptions.expires, 'seconds').toDate();
|
|
171
|
+
|
|
172
|
+
if(!!expirationDateInitial) {
|
|
173
|
+
expirationDate = expirationDateInitial;
|
|
174
|
+
} else {
|
|
175
|
+
expirationDate = new Date();
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
} else {
|
|
179
|
+
|
|
180
|
+
expirationDate = mergedOptions.expires;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
cookieString += `; expires=${expirationDate.toUTCString()}`;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (mergedOptions.path) cookieString += `; path=${mergedOptions.path}`;
|
|
187
|
+
if (mergedOptions.domain) cookieString += `; domain=${mergedOptions.domain}`;
|
|
188
|
+
if (mergedOptions.secure) cookieString += `; secure`;
|
|
189
|
+
if (mergedOptions.sameSite) cookieString += `; samesite=${mergedOptions.sameSite}`;
|
|
190
|
+
|
|
191
|
+
if(NODEENV === 'development') {
|
|
192
|
+
console.log(`Cooks - buildCookieString | cookieString:: `, cookieString);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
return cookieString;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
private static getAllCookies(): Record<string, string> {
|
|
199
|
+
return document.cookie
|
|
200
|
+
.split(';')
|
|
201
|
+
.reduce((cookies, cookie) => {
|
|
202
|
+
const [key, value] = cookie.split('=').map(part => part.trim());
|
|
203
|
+
if (key) {
|
|
204
|
+
cookies[key] = value || '';
|
|
205
|
+
}
|
|
206
|
+
return cookies;
|
|
207
|
+
}, {} as Record<string, string>);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
private static isBrowser(): boolean {
|
|
211
|
+
return typeof window !== 'undefined' && typeof document !== 'undefined';
|
|
212
|
+
}
|
|
213
|
+
static exposeToGlobal(): void {
|
|
214
|
+
if (typeof window !== "undefined") {
|
|
215
|
+
(window as any).Cooks = Cooks;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
if (typeof window !== "undefined") {
|
|
221
|
+
(window as any).Cooks = Cooks;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
const globalFunctModule = {
|
|
228
|
+
default: Cooks,
|
|
229
|
+
Cooks: Cooks,
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
const __bundledModules = globalFunctModule;
|
|
234
|
+
|
|
235
|
+
if (typeof global !== 'undefined') {
|
|
236
|
+
(global as any).default = Cooks;
|
|
237
|
+
(global as any).Cooks = Cooks;
|
|
238
|
+
(global as any).__bundledModules = __bundledModules;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
if (typeof window !== "undefined") {
|
|
242
|
+
(window as any).default = Cooks;
|
|
243
|
+
(window as any).Cooks = Cooks;
|
|
244
|
+
(window as any).__bundledModules = __bundledModules;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
if (typeof exports !== 'undefined') {
|
|
248
|
+
exports.default = Cooks;
|
|
249
|
+
exports.Cooks = Cooks;
|
|
250
|
+
}
|
|
251
|
+
return globalFunctModule;
|
|
252
|
+
|
|
253
|
+
})(typeof global !== 'undefined' ? global :
|
|
254
|
+
typeof window !== 'undefined' ? window :
|
|
255
|
+
typeof self !== 'undefined' ? self :
|
|
256
|
+
typeof globalThis !== 'undefined' ? globalThis :
|
|
257
|
+
{});
|
|
258
|
+
|
|
259
|
+
type CooksElementTypeGF = typeof globalFunct.Cooks;
|
|
260
|
+
|
|
261
|
+
type DefaultElementTypeGF = typeof globalFunct.default;
|
|
262
|
+
|
|
263
|
+
const CooksElementGF: CooksElementTypeGF = globalFunct.Cooks;
|
|
264
|
+
|
|
265
|
+
const defaultElementGF: DefaultElementTypeGF = globalFunct.default;
|
|
266
|
+
|
|
267
|
+
export {
|
|
268
|
+
defaultElementGF as default,
|
|
269
|
+
CooksElementGF as Cooks,
|
|
270
|
+
};
|
|
271
|
+
export type {
|
|
272
|
+
DefaultElementTypeGF,
|
|
273
|
+
CooksElementTypeGF,
|
|
274
|
+
};
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES6",
|
|
4
|
+
"module": "commonjs",
|
|
5
|
+
"lib": ["ES2020", "DOM", "DOM.Iterable", "ESNext"],
|
|
6
|
+
"esModuleInterop": true,
|
|
7
|
+
"skipLibCheck": true,
|
|
8
|
+
"declaration": false,
|
|
9
|
+
"sourceMap": false,
|
|
10
|
+
"allowSyntheticDefaultImports": true,
|
|
11
|
+
"noEmit": false,
|
|
12
|
+
|
|
13
|
+
"strict": false,
|
|
14
|
+
"noImplicitAny": false,
|
|
15
|
+
"strictNullChecks": false,
|
|
16
|
+
"strictFunctionTypes": false,
|
|
17
|
+
"strictBindCallApply": false,
|
|
18
|
+
"strictPropertyInitialization": false,
|
|
19
|
+
"noImplicitThis": false,
|
|
20
|
+
"alwaysStrict": false
|
|
21
|
+
},
|
|
22
|
+
"include": [],
|
|
23
|
+
"exclude": [
|
|
24
|
+
"node_modules",
|
|
25
|
+
"**/*.d.ts"
|
|
26
|
+
]
|
|
27
|
+
}
|