@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/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/js/index.js ADDED
@@ -0,0 +1,189 @@
1
+ import { Timez } from '@jsarc/timez';
2
+ const globalFunct = (function (global) {
3
+ 'use strict';
4
+ const NODEENV = 'development';
5
+ const langs = ['en', 'fr'];
6
+ const langCodes = {
7
+ 'fr': 'fr_FR',
8
+ 'en': 'en_US',
9
+ };
10
+ const dateTimeFormatInitial = '%Y-%m-%dT%H:%M:%S.%fZ';
11
+ const dateFormatInitial = '%Y-%m-%d';
12
+ const timeFormatInitial = '%H:%M:%S.%fZ';
13
+ const dateFormatForFile = '%Y%m%d%H%M%S';
14
+ const dateFormat1 = '%Y/%m/%d %H:%M:%S.%fZ';
15
+ const dateFormat2 = '%Y/%m/%d %H:%M:%S';
16
+ const dateFormat3 = '%Y/%m/%d %H:%M';
17
+ const dateFormat4 = '%d/%m/%Y %H:%M:%S GMT%z';
18
+ const dateFormat5 = '%Y/%m/%d';
19
+ const timeFormat1 = '%H:%M:%S.%fZ';
20
+ const timeFormat2 = '%H:%M:%S';
21
+ const timeFormat3 = '%H:%M:%S.%f';
22
+ const pagesPossibles = [5, 10, 15, 25, 50, 100, -1];
23
+ const regExpForAlphanumeric = /^[\w\s]{1,}/;
24
+ const tabNumerique = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
25
+ 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'];
26
+ const tabAlphabetiqueInsensitive = [...tabAlphabetique, ...tabAlphabetique.map(x => x.toUpperCase())];
27
+ const tabAlphanumerique = [...tabNumerique, ...tabAlphabetique];
28
+ const tabAlphanumeriqueInsensitive = [...tabNumerique, ...tabAlphabetiqueInsensitive];
29
+ class Cooks {
30
+ static DEFAULT_OPTIONS = {
31
+ path: '/',
32
+ secure: true,
33
+ sameSite: 'lax'
34
+ };
35
+ static set(key, value, options = {}) {
36
+ if (!this.isBrowser())
37
+ return;
38
+ try {
39
+ const serializedValue = this.serialize(value);
40
+ const encodedValue = encodeURIComponent(serializedValue);
41
+ const cookieString = this.buildCookieString(key, encodedValue, options);
42
+ document.cookie = cookieString;
43
+ }
44
+ catch (error) {
45
+ console.error(`Cooks: Error setting cookie "${key}":`, error);
46
+ }
47
+ }
48
+ static get(key) {
49
+ if (!this.isBrowser())
50
+ return null;
51
+ try {
52
+ const cookies = this.getAllCookies();
53
+ const encodedValue = cookies[key];
54
+ if (!encodedValue)
55
+ return null;
56
+ const decodedValue = decodeURIComponent(encodedValue);
57
+ return this.deserialize(decodedValue);
58
+ }
59
+ catch (error) {
60
+ console.error(`Cooks: Error getting cookie "${key}":`, error);
61
+ return null;
62
+ }
63
+ }
64
+ static remove(key, path = '/', domain) {
65
+ if (!this.isBrowser())
66
+ return;
67
+ const options = {
68
+ expires: new Date(0),
69
+ path,
70
+ domain
71
+ };
72
+ this.set(key, '', options);
73
+ }
74
+ static has(key) {
75
+ return this.get(key) !== null;
76
+ }
77
+ static keys() {
78
+ if (!this.isBrowser())
79
+ return [];
80
+ const cookies = this.getAllCookies();
81
+ return Object.keys(cookies);
82
+ }
83
+ static clear() {
84
+ if (!this.isBrowser())
85
+ return;
86
+ const cookies = this.getAllCookies();
87
+ Object.keys(cookies).forEach(key => {
88
+ this.remove(key);
89
+ });
90
+ }
91
+ static serialize(value) {
92
+ if (value instanceof Date) {
93
+ return JSON.stringify({
94
+ __type: 'Date',
95
+ __value: value.toISOString()
96
+ });
97
+ }
98
+ return JSON.stringify(value);
99
+ }
100
+ static deserialize(value) {
101
+ const parsed = JSON.parse(value);
102
+ if (parsed && typeof parsed === 'object' && parsed.__type === 'Date') {
103
+ return new Date(parsed.__value);
104
+ }
105
+ return parsed;
106
+ }
107
+ static buildCookieString(key, value, options) {
108
+ const mergedOptions = { ...this.DEFAULT_OPTIONS, ...options };
109
+ let cookieString = `${key}=${value}`;
110
+ if (mergedOptions.expires !== undefined) {
111
+ let expirationDate;
112
+ if (typeof mergedOptions.expires === 'number') {
113
+ const expirationDateInitial = new Timez().add(mergedOptions.expires, 'seconds').toDate();
114
+ if (!!expirationDateInitial) {
115
+ expirationDate = expirationDateInitial;
116
+ }
117
+ else {
118
+ expirationDate = new Date();
119
+ }
120
+ }
121
+ else {
122
+ expirationDate = mergedOptions.expires;
123
+ }
124
+ cookieString += `; expires=${expirationDate.toUTCString()}`;
125
+ }
126
+ if (mergedOptions.path)
127
+ cookieString += `; path=${mergedOptions.path}`;
128
+ if (mergedOptions.domain)
129
+ cookieString += `; domain=${mergedOptions.domain}`;
130
+ if (mergedOptions.secure)
131
+ cookieString += `; secure`;
132
+ if (mergedOptions.sameSite)
133
+ cookieString += `; samesite=${mergedOptions.sameSite}`;
134
+ if (NODEENV === 'development') {
135
+ console.log(`Cooks - buildCookieString | cookieString:: `, cookieString);
136
+ }
137
+ return cookieString;
138
+ }
139
+ static getAllCookies() {
140
+ return document.cookie
141
+ .split(';')
142
+ .reduce((cookies, cookie) => {
143
+ const [key, value] = cookie.split('=').map(part => part.trim());
144
+ if (key) {
145
+ cookies[key] = value || '';
146
+ }
147
+ return cookies;
148
+ }, {});
149
+ }
150
+ static isBrowser() {
151
+ return typeof window !== 'undefined' && typeof document !== 'undefined';
152
+ }
153
+ static exposeToGlobal() {
154
+ if (typeof window !== "undefined") {
155
+ window.Cooks = Cooks;
156
+ }
157
+ }
158
+ }
159
+ if (typeof window !== "undefined") {
160
+ window.Cooks = Cooks;
161
+ }
162
+ const globalFunctModule = {
163
+ default: Cooks,
164
+ Cooks: Cooks,
165
+ };
166
+ const __bundledModules = globalFunctModule;
167
+ if (typeof global !== 'undefined') {
168
+ global.default = Cooks;
169
+ global.Cooks = Cooks;
170
+ global.__bundledModules = __bundledModules;
171
+ }
172
+ if (typeof window !== "undefined") {
173
+ window.default = Cooks;
174
+ window.Cooks = Cooks;
175
+ window.__bundledModules = __bundledModules;
176
+ }
177
+ if (typeof exports !== 'undefined') {
178
+ exports.default = Cooks;
179
+ exports.Cooks = Cooks;
180
+ }
181
+ return globalFunctModule;
182
+ })(typeof global !== 'undefined' ? global :
183
+ typeof window !== 'undefined' ? window :
184
+ typeof self !== 'undefined' ? self :
185
+ typeof globalThis !== 'undefined' ? globalThis :
186
+ {});
187
+ const CooksElementGF = globalFunct.Cooks;
188
+ const defaultElementGF = globalFunct.default;
189
+ export { defaultElementGF as default, CooksElementGF as Cooks, };
package/package.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "name": "@jsarc/cooks",
3
+ "publishConfig": {
4
+ "access": "public"
5
+ },
6
+ "version": "0.0.0",
7
+ "description": "Cooks est une bibliothèque TypeScript/JavaScript légère et type-safe pour la gestion des cookies dans les navigateurs web. Elle offre une API simple et intuitive pour stocker, récupérer et manipuler des cookies avec support des types complexes et des options avancées.",
8
+ "main": "index.ts",
9
+ "keywords": [],
10
+ "author": "INICODE <contact.inicode@gmail.com>",
11
+ "license": "MIT",
12
+ "scripts": {
13
+ "init": "npm init --scope=@jsarc/cooks",
14
+ "login": "npm login"
15
+ },
16
+ "devDependencies": {},
17
+ "dependencies": {
18
+ "@jsarc/timez": "^0.0.1-beta.0.1"
19
+ }
20
+ }