@jsarc/id-generator 0.0.0 → 0.0.1-beta.0.1
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/id-generator.ts +538 -0
- package/index.ts +20 -7
- package/js/index.js +7 -3
- package/package.json +1 -1
- package/ts/id-generator.ts +538 -0
- package/ts/index.ts +552 -0
- package/web/id-generator.js +14 -1
- package/web/id-generator.min.js +1 -1
package/ts/index.ts
ADDED
|
@@ -0,0 +1,552 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
|
|
3
|
+
interface Window {
|
|
4
|
+
default: typeof defaultElementGF;
|
|
5
|
+
IdentifierGenerator: any;
|
|
6
|
+
IdentifierGeneratorHandlers: any;
|
|
7
|
+
__bundledModules: any;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface RandomOptions {
|
|
11
|
+
size: number;
|
|
12
|
+
type: RandomType;
|
|
13
|
+
variant: boolean;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
export interface UUIDOptions {
|
|
18
|
+
version: UUIDVersion;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
export interface TokenDefinition {
|
|
23
|
+
type: 'string' | 'random' | 'uuid' | 'custom';
|
|
24
|
+
value: string | RandomOptions | UUIDOptions | CustomHandler;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
export type RandomType = (
|
|
29
|
+
'numeric'
|
|
30
|
+
| 'alphabetic'
|
|
31
|
+
| 'alphabetic-case'
|
|
32
|
+
| 'alphanumeric'
|
|
33
|
+
| 'alphanumeric-case'
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
export type UUIDVersion = 'v1' | 'v2' | 'v3' | 'v4' | 'v5';
|
|
37
|
+
|
|
38
|
+
export type CustomHandler = () => string;
|
|
39
|
+
|
|
40
|
+
const globalFunct = (function(global: any) {
|
|
41
|
+
'use strict';
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
class IdentifierGenerator {
|
|
56
|
+
private static instance: IdentifierGenerator;
|
|
57
|
+
private cache: Map<string, Map<string, string>> = new Map();
|
|
58
|
+
|
|
59
|
+
private constructor() { }
|
|
60
|
+
|
|
61
|
+
public static getInstance(): IdentifierGenerator {
|
|
62
|
+
if (!IdentifierGenerator.instance) {
|
|
63
|
+
IdentifierGenerator.instance = new IdentifierGenerator();
|
|
64
|
+
}
|
|
65
|
+
return IdentifierGenerator.instance;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
public generate(format: string, customHandlers: Record<string, CustomHandler> = {}): string {
|
|
69
|
+
const tokens = this.parseFormat(format);
|
|
70
|
+
return tokens.map(token => this.resolveToken(token, customHandlers)).join('');
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
private parseFormat(format: string): TokenDefinition[] {
|
|
74
|
+
const tokens: TokenDefinition[] = [];
|
|
75
|
+
let currentIndex = 0;
|
|
76
|
+
|
|
77
|
+
while (currentIndex < format.length) {
|
|
78
|
+
const nextHashIndex = format.indexOf('#', currentIndex);
|
|
79
|
+
|
|
80
|
+
if (nextHashIndex > currentIndex) {
|
|
81
|
+
tokens.push({
|
|
82
|
+
type: 'string',
|
|
83
|
+
value: format.substring(currentIndex, nextHashIndex)
|
|
84
|
+
});
|
|
85
|
+
currentIndex = nextHashIndex;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (format[currentIndex] === '#') {
|
|
89
|
+
const tokenEnd = this.findTokenEnd(format, currentIndex + 1);
|
|
90
|
+
if (tokenEnd === -1) {
|
|
91
|
+
throw new Error(`Format invalide à la position ${currentIndex}`);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const tokenContent = format.substring(currentIndex + 1, tokenEnd);
|
|
95
|
+
const token = this.parseToken(tokenContent);
|
|
96
|
+
tokens.push(token);
|
|
97
|
+
currentIndex = tokenEnd;
|
|
98
|
+
} else if (nextHashIndex === -1) {
|
|
99
|
+
|
|
100
|
+
tokens.push({
|
|
101
|
+
type: 'string',
|
|
102
|
+
value: format.substring(currentIndex)
|
|
103
|
+
});
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return tokens;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
private findTokenEnd(format: string, startIndex: number): number {
|
|
112
|
+
let braceCount = 0;
|
|
113
|
+
let inString = false;
|
|
114
|
+
let escapeNext = false;
|
|
115
|
+
|
|
116
|
+
for (let i = startIndex; i < format.length; i++) {
|
|
117
|
+
const char = format[i];
|
|
118
|
+
|
|
119
|
+
if (escapeNext) {
|
|
120
|
+
escapeNext = false;
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (char === '\\') {
|
|
125
|
+
escapeNext = true;
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (char === '"' || char === "'") {
|
|
130
|
+
inString = !inString;
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (!inString) {
|
|
135
|
+
if (char === '{') {
|
|
136
|
+
braceCount++;
|
|
137
|
+
} else if (char === '}') {
|
|
138
|
+
if (braceCount === 0) {
|
|
139
|
+
return i;
|
|
140
|
+
}
|
|
141
|
+
braceCount--;
|
|
142
|
+
} else if (char === '#' && braceCount === 0) {
|
|
143
|
+
return i;
|
|
144
|
+
} else if (/[a-zA-Z0-9]/.test(char) === false && char !== ',' && char !== ':' && char !== '-' && char !== '_' && braceCount === 0) {
|
|
145
|
+
return i;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return format.length;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Parse un token individuel
|
|
155
|
+
*/
|
|
156
|
+
private parseToken(tokenContent: string): TokenDefinition {
|
|
157
|
+
// Token personnalisé simple
|
|
158
|
+
if (tokenContent.includes('{') === false) {
|
|
159
|
+
return { type: 'string', value: tokenContent };
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const [type, optionsStr] = tokenContent.split('{', 2);
|
|
163
|
+
const optionsContent = optionsStr.slice(0, -1); // Retirer le '}'
|
|
164
|
+
|
|
165
|
+
if (type === 'rand') {
|
|
166
|
+
return {
|
|
167
|
+
type: 'random',
|
|
168
|
+
value: this.parseRandomOptions(optionsContent)
|
|
169
|
+
};
|
|
170
|
+
} else if (type === 'uuid') {
|
|
171
|
+
return {
|
|
172
|
+
type: 'uuid',
|
|
173
|
+
value: this.parseUUIDOptions(optionsContent)
|
|
174
|
+
};
|
|
175
|
+
} else if (type === 'custom') {
|
|
176
|
+
return {
|
|
177
|
+
type: 'custom',
|
|
178
|
+
value: optionsContent
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
throw new Error(`Type de token non supporté: ${type}`);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Parse les options pour un random string
|
|
187
|
+
*/
|
|
188
|
+
private parseRandomOptions(optionsStr: string): RandomOptions {
|
|
189
|
+
const defaults: RandomOptions = {
|
|
190
|
+
size: 8,
|
|
191
|
+
type: 'alphanumeric',
|
|
192
|
+
variant: false
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
const options = this.parseOptions(optionsStr);
|
|
196
|
+
|
|
197
|
+
return {
|
|
198
|
+
size: options.size ? parseInt(options.size) : defaults.size,
|
|
199
|
+
type: (options.type as RandomType) || defaults.type,
|
|
200
|
+
variant: options.variant === 'true' ? true : defaults.variant
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Parse les options pour un UUID
|
|
206
|
+
*/
|
|
207
|
+
private parseUUIDOptions(optionsStr: string): UUIDOptions {
|
|
208
|
+
const defaults: UUIDOptions = {
|
|
209
|
+
version: 'v4'
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
const options = this.parseOptions(optionsStr);
|
|
213
|
+
|
|
214
|
+
return {
|
|
215
|
+
version: (options.version as UUIDVersion) || defaults.version
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Parse une chaîne d'options key:value
|
|
221
|
+
*/
|
|
222
|
+
private parseOptions(optionsStr: string): Record<string, string> {
|
|
223
|
+
const options: Record<string, string> = {};
|
|
224
|
+
let currentKey = '';
|
|
225
|
+
let currentValue = '';
|
|
226
|
+
let inString = false;
|
|
227
|
+
let stringChar = '';
|
|
228
|
+
let braceCount = 0;
|
|
229
|
+
|
|
230
|
+
for (let i = 0; i < optionsStr.length; i++) {
|
|
231
|
+
const char = optionsStr[i];
|
|
232
|
+
|
|
233
|
+
if (char === '"' || char === "'") {
|
|
234
|
+
if (!inString) {
|
|
235
|
+
inString = true;
|
|
236
|
+
stringChar = char;
|
|
237
|
+
} else if (stringChar === char) {
|
|
238
|
+
inString = false;
|
|
239
|
+
} else {
|
|
240
|
+
currentValue += char;
|
|
241
|
+
}
|
|
242
|
+
continue;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
if (inString) {
|
|
246
|
+
currentValue += char;
|
|
247
|
+
continue;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
if (char === ':') {
|
|
251
|
+
if (braceCount === 0) {
|
|
252
|
+
currentKey = currentValue.trim();
|
|
253
|
+
currentValue = '';
|
|
254
|
+
continue;
|
|
255
|
+
}
|
|
256
|
+
} else if (char === '{') {
|
|
257
|
+
braceCount++;
|
|
258
|
+
} else if (char === '}') {
|
|
259
|
+
braceCount--;
|
|
260
|
+
} else if (char === ',' && braceCount === 0) {
|
|
261
|
+
options[currentKey] = currentValue.trim();
|
|
262
|
+
currentKey = '';
|
|
263
|
+
currentValue = '';
|
|
264
|
+
continue;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
currentValue += char;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
if (currentKey) {
|
|
271
|
+
options[currentKey] = currentValue.trim();
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
return options;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
private resolveToken(token: TokenDefinition, customHandlers: Record<string, CustomHandler>): string {
|
|
278
|
+
switch (token.type) {
|
|
279
|
+
case 'string':
|
|
280
|
+
return token.value as string;
|
|
281
|
+
|
|
282
|
+
case 'random':
|
|
283
|
+
return this.generateRandomString(token.value as RandomOptions);
|
|
284
|
+
|
|
285
|
+
case 'uuid':
|
|
286
|
+
return this.generateUUID(token.value as UUIDOptions);
|
|
287
|
+
|
|
288
|
+
case 'custom':
|
|
289
|
+
const handlerName = (token.value as string).split('}').join('').split('{').join('');
|
|
290
|
+
const checker = Object.keys(customHandlers).includes(handlerName);
|
|
291
|
+
if (checker) {
|
|
292
|
+
return customHandlers[handlerName]();
|
|
293
|
+
} else {
|
|
294
|
+
console.log(`[id-generator -> id-generator] IdentifierGenerator | resolveToken - custom - token:: `, token);
|
|
295
|
+
console.log(`[id-generator -> id-generator] IdentifierGenerator | resolveToken - custom - handlerName:: `, handlerName);
|
|
296
|
+
console.log(`[id-generator -> id-generator] IdentifierGenerator | resolveToken - custom - checker:: `, checker);
|
|
297
|
+
console.log(`[id-generator -> id-generator] IdentifierGenerator | resolveToken - custom - Object.keys(customHandlers):: `, Object.keys(customHandlers));
|
|
298
|
+
console.log(`[id-generator -> id-generator] IdentifierGenerator | resolveToken - custom - customHandlers:: `, customHandlers);
|
|
299
|
+
throw new Error(`Handler personnalisé non trouvé: ${handlerName}`);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
default:
|
|
303
|
+
throw new Error(`Type de token inconnu: ${token.type}`);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
private generateRandomString(options: RandomOptions): string {
|
|
308
|
+
const cacheKey = this.getRandomCacheKey(options);
|
|
309
|
+
|
|
310
|
+
if (!options.variant) {
|
|
311
|
+
if (!this.cache.has('random')) {
|
|
312
|
+
this.cache.set('random', new Map());
|
|
313
|
+
}
|
|
314
|
+
const randomCache = this.cache.get('random')!;
|
|
315
|
+
|
|
316
|
+
if (randomCache.has(cacheKey)) {
|
|
317
|
+
return randomCache.get(cacheKey)!;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
let charset = '';
|
|
322
|
+
let result = '';
|
|
323
|
+
|
|
324
|
+
switch (options.type) {
|
|
325
|
+
case 'numeric':
|
|
326
|
+
charset = '0123456789';
|
|
327
|
+
break;
|
|
328
|
+
case 'alphabetic':
|
|
329
|
+
charset = 'abcdefghijklmnopqrstuvwxyz';
|
|
330
|
+
break;
|
|
331
|
+
case 'alphabetic-case':
|
|
332
|
+
charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
333
|
+
break;
|
|
334
|
+
case 'alphanumeric':
|
|
335
|
+
charset = 'abcdefghijklmnopqrstuvwxyz0123456789';
|
|
336
|
+
break;
|
|
337
|
+
case 'alphanumeric-case':
|
|
338
|
+
charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
|
|
339
|
+
break;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
const randomValues = new Uint32Array(options.size);
|
|
343
|
+
crypto.getRandomValues(randomValues);
|
|
344
|
+
|
|
345
|
+
for (let i = 0; i < options.size; i++) {
|
|
346
|
+
result += charset[randomValues[i] % charset.length];
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
if (!options.variant) {
|
|
350
|
+
this.cache.get('random')!.set(cacheKey, result);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
return result;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
private generateUUID(options: UUIDOptions): string {
|
|
357
|
+
const cacheKey = `uuid-${options.version}`;
|
|
358
|
+
|
|
359
|
+
if (!this.cache.has('uuid')) {
|
|
360
|
+
this.cache.set('uuid', new Map());
|
|
361
|
+
}
|
|
362
|
+
const uuidCache = this.cache.get('uuid')!;
|
|
363
|
+
|
|
364
|
+
if (uuidCache.has(cacheKey)) {
|
|
365
|
+
|
|
366
|
+
if (options.version === 'v4') {
|
|
367
|
+
return this.generateUUIDv4();
|
|
368
|
+
}
|
|
369
|
+
return uuidCache.get(cacheKey)!;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
let uuid: string;
|
|
373
|
+
|
|
374
|
+
switch (options.version) {
|
|
375
|
+
case 'v1':
|
|
376
|
+
uuid = this.generateUUIDv1();
|
|
377
|
+
break;
|
|
378
|
+
case 'v2':
|
|
379
|
+
uuid = this.generateUUIDv2();
|
|
380
|
+
break;
|
|
381
|
+
case 'v3':
|
|
382
|
+
uuid = this.generateUUIDv3();
|
|
383
|
+
break;
|
|
384
|
+
case 'v4':
|
|
385
|
+
uuid = this.generateUUIDv4();
|
|
386
|
+
break;
|
|
387
|
+
case 'v5':
|
|
388
|
+
uuid = this.generateUUIDv5();
|
|
389
|
+
break;
|
|
390
|
+
default:
|
|
391
|
+
uuid = this.generateUUIDv4();
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
if (options.version === 'v3' || options.version === 'v5') {
|
|
395
|
+
uuidCache.set(cacheKey, uuid);
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
return uuid;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
private generateUUIDv1(): string {
|
|
402
|
+
|
|
403
|
+
const now = Date.now();
|
|
404
|
+
const hexTime = now.toString(16).padStart(12, '0');
|
|
405
|
+
return `${hexTime.slice(0, 8)}-${hexTime.slice(8, 12)}-1000-8000-${this.getRandomHex(12)}`;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
private generateUUIDv2(): string {
|
|
409
|
+
|
|
410
|
+
return this.generateUUIDv1();
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
private generateUUIDv3(): string {
|
|
414
|
+
|
|
415
|
+
const namespace = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';
|
|
416
|
+
const name = 'default';
|
|
417
|
+
return 'xxxxxxxx-xxxx-3xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
|
|
418
|
+
const r = Math.random() * 16 | 0;
|
|
419
|
+
const v = c === 'x' ? r : (r & 0x3 | 0x8);
|
|
420
|
+
return v.toString(16);
|
|
421
|
+
});
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
private generateUUIDv4(): string {
|
|
425
|
+
|
|
426
|
+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
|
|
427
|
+
const r = crypto.getRandomValues(new Uint8Array(1))[0] % 16 | 0;
|
|
428
|
+
const v = c === 'x' ? r : (r & 0x3 | 0x8);
|
|
429
|
+
return v.toString(16);
|
|
430
|
+
});
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
private generateUUIDv5(): string {
|
|
434
|
+
|
|
435
|
+
const namespace = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';
|
|
436
|
+
const name = 'default';
|
|
437
|
+
return 'xxxxxxxx-xxxx-5xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
|
|
438
|
+
const r = Math.random() * 16 | 0;
|
|
439
|
+
const v = c === 'x' ? r : (r & 0x3 | 0x8);
|
|
440
|
+
return v.toString(16);
|
|
441
|
+
});
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
private getRandomHex(size: number): string {
|
|
445
|
+
const bytes = new Uint8Array(Math.ceil(size / 2));
|
|
446
|
+
crypto.getRandomValues(bytes);
|
|
447
|
+
return Array.from(bytes)
|
|
448
|
+
.map(b => b.toString(16).padStart(2, '0'))
|
|
449
|
+
.join('')
|
|
450
|
+
.slice(0, size);
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
private getRandomCacheKey(options: RandomOptions): string {
|
|
454
|
+
return `${options.size}-${options.type}-${options.variant}`;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
public clearCache(): void {
|
|
458
|
+
this.cache.clear();
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
public static generateId(format: string, customHandlers: Record<string, CustomHandler> = {}): string {
|
|
462
|
+
return this.getInstance().generate(format, customHandlers);
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
static exposeToGlobal(): void {
|
|
466
|
+
if (typeof window !== 'undefined') {
|
|
467
|
+
(window as any).IdentifierGenerator = IdentifierGenerator;
|
|
468
|
+
(window as any).IdentifierGeneratorHandlers = IdentifierGeneratorHandlers;
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
const IdentifierGeneratorHandlers = {
|
|
474
|
+
timestamp: () => Date.now().toString(),
|
|
475
|
+
date: () => new Date().toISOString().slice(0, 10).replace(/-/g, ''),
|
|
476
|
+
time: () => new Date().toISOString().slice(11, 19).replace(/:/g, ''),
|
|
477
|
+
counter: (() => {
|
|
478
|
+
let count = 0;
|
|
479
|
+
return () => (++count).toString().padStart(6, '0');
|
|
480
|
+
})(),
|
|
481
|
+
env: (envVar: string) => process.env[envVar] || ''
|
|
482
|
+
};
|
|
483
|
+
|
|
484
|
+
if (typeof window !== 'undefined') {
|
|
485
|
+
(window as any).IdentifierGenerator = IdentifierGenerator;
|
|
486
|
+
(window as any).IdentifierGeneratorHandlers = IdentifierGeneratorHandlers;
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
const idGen = {
|
|
490
|
+
IdentifierGenerator,
|
|
491
|
+
IdentifierGeneratorHandlers,
|
|
492
|
+
};
|
|
493
|
+
|
|
494
|
+
|
|
495
|
+
const globalFunctModule = {
|
|
496
|
+
default: idGen,
|
|
497
|
+
IdentifierGenerator: IdentifierGenerator,
|
|
498
|
+
IdentifierGeneratorHandlers: IdentifierGeneratorHandlers,
|
|
499
|
+
};
|
|
500
|
+
|
|
501
|
+
|
|
502
|
+
const __bundledModules = globalFunctModule;
|
|
503
|
+
|
|
504
|
+
if (typeof global !== 'undefined') {
|
|
505
|
+
(global as any).default = idGen;
|
|
506
|
+
(global as any).IdentifierGenerator = IdentifierGenerator;
|
|
507
|
+
(global as any).IdentifierGeneratorHandlers = IdentifierGeneratorHandlers;
|
|
508
|
+
(global as any).__bundledModules = __bundledModules;
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
if (typeof window !== "undefined") {
|
|
512
|
+
(window as any).default = idGen;
|
|
513
|
+
(window as any).IdentifierGenerator = IdentifierGenerator;
|
|
514
|
+
(window as any).IdentifierGeneratorHandlers = IdentifierGeneratorHandlers;
|
|
515
|
+
(window as any).__bundledModules = __bundledModules;
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
if (typeof exports !== 'undefined') {
|
|
519
|
+
exports.default = idGen;
|
|
520
|
+
exports.IdentifierGenerator = IdentifierGenerator;
|
|
521
|
+
exports.IdentifierGeneratorHandlers = IdentifierGeneratorHandlers;
|
|
522
|
+
}
|
|
523
|
+
return globalFunctModule;
|
|
524
|
+
|
|
525
|
+
})(typeof global !== 'undefined' ? global :
|
|
526
|
+
typeof window !== 'undefined' ? window :
|
|
527
|
+
typeof self !== 'undefined' ? self :
|
|
528
|
+
typeof globalThis !== 'undefined' ? globalThis :
|
|
529
|
+
{});
|
|
530
|
+
|
|
531
|
+
type IdentifierGeneratorElementTypeGF = typeof globalFunct.IdentifierGenerator;
|
|
532
|
+
|
|
533
|
+
type IdentifierGeneratorHandlersElementTypeGF = typeof globalFunct.IdentifierGeneratorHandlers;
|
|
534
|
+
|
|
535
|
+
type DefaultElementTypeGF = typeof globalFunct.default;
|
|
536
|
+
|
|
537
|
+
const IdentifierGeneratorElementGF: IdentifierGeneratorElementTypeGF = globalFunct.IdentifierGenerator;
|
|
538
|
+
|
|
539
|
+
const IdentifierGeneratorHandlersElementGF: IdentifierGeneratorHandlersElementTypeGF = globalFunct.IdentifierGeneratorHandlers;
|
|
540
|
+
|
|
541
|
+
const defaultElementGF: DefaultElementTypeGF = globalFunct.default;
|
|
542
|
+
|
|
543
|
+
export {
|
|
544
|
+
defaultElementGF as default,
|
|
545
|
+
IdentifierGeneratorElementGF as IdentifierGenerator,
|
|
546
|
+
IdentifierGeneratorHandlersElementGF as IdentifierGeneratorHandlers,
|
|
547
|
+
};
|
|
548
|
+
export type {
|
|
549
|
+
DefaultElementTypeGF,
|
|
550
|
+
RandomType,UUIDVersion,CustomHandler,RandomOptions,UUIDOptions,TokenDefinition,
|
|
551
|
+
IdentifierGeneratorElementTypeGF,IdentifierGeneratorHandlersElementTypeGF,
|
|
552
|
+
};
|
package/web/id-generator.js
CHANGED
|
@@ -371,18 +371,30 @@ const globalFunct = (function (global) {
|
|
|
371
371
|
window.IdentifierGenerator = IdentifierGenerator;
|
|
372
372
|
window.IdentifierGeneratorHandlers = IdentifierGeneratorHandlers;
|
|
373
373
|
}
|
|
374
|
+
const idGen = {
|
|
375
|
+
IdentifierGenerator,
|
|
376
|
+
IdentifierGeneratorHandlers,
|
|
377
|
+
};
|
|
378
|
+
const globalFunctModule = {
|
|
379
|
+
default: idGen,
|
|
380
|
+
IdentifierGenerator: IdentifierGenerator,
|
|
381
|
+
IdentifierGeneratorHandlers: IdentifierGeneratorHandlers,
|
|
382
|
+
};
|
|
374
383
|
const __bundledModules = globalFunctModule;
|
|
375
384
|
if (typeof global !== 'undefined') {
|
|
385
|
+
global.default = idGen;
|
|
376
386
|
global.IdentifierGenerator = IdentifierGenerator;
|
|
377
387
|
global.IdentifierGeneratorHandlers = IdentifierGeneratorHandlers;
|
|
378
388
|
global.__bundledModules = __bundledModules;
|
|
379
389
|
}
|
|
380
390
|
if (typeof window !== "undefined") {
|
|
391
|
+
window.default = idGen;
|
|
381
392
|
window.IdentifierGenerator = IdentifierGenerator;
|
|
382
393
|
window.IdentifierGeneratorHandlers = IdentifierGeneratorHandlers;
|
|
383
394
|
window.__bundledModules = __bundledModules;
|
|
384
395
|
}
|
|
385
396
|
if (typeof exports !== 'undefined') {
|
|
397
|
+
exports.default = idGen;
|
|
386
398
|
exports.IdentifierGenerator = IdentifierGenerator;
|
|
387
399
|
exports.IdentifierGeneratorHandlers = IdentifierGeneratorHandlers;
|
|
388
400
|
}
|
|
@@ -393,4 +405,5 @@ const globalFunct = (function (global) {
|
|
|
393
405
|
typeof globalThis !== 'undefined' ? globalThis :
|
|
394
406
|
{});
|
|
395
407
|
const IdentifierGeneratorElementGF = globalFunct.IdentifierGenerator;
|
|
396
|
-
const IdentifierGeneratorHandlersElementGF = globalFunct.IdentifierGeneratorHandlers;
|
|
408
|
+
const IdentifierGeneratorHandlersElementGF = globalFunct.IdentifierGeneratorHandlers;
|
|
409
|
+
const defaultElementGF = globalFunct.default;
|
package/web/id-generator.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
let globalFunct=(e=>{class r{static instance;cache=new Map;constructor(){}static getInstance(){return r.instance||(r.instance=new r),r.instance}generate(e,r={}){return this.parseFormat(e).map(e=>this.resolveToken(e,r)).join("")}parseFormat(e){var r=[];let t=0;for(;t<e.length;){var n=e.indexOf("#",t);if(n>t&&(r.push({type:"string",value:e.substring(t,n)}),t=n),"#"===e[t]){var a=this.findTokenEnd(e,t+1);if(-1===a)throw new Error("Format invalide à la position "+t);var i=e.substring(t+1,a),i=this.parseToken(i);r.push(i),t=a}else if(-1===n){r.push({type:"string",value:e.substring(t)});break}}return r}findTokenEnd(r,t){let n=0,a=!1,i=!1;for(let e=t;e<r.length;e++){var o=r[e];if(i)i=!1;else if("\\"===o)i=!0;else if('"'===o||"'"===o)a=!a;else if(!a)if("{"===o)n++;else if("}"===o){if(0===n)return e;n--}else{if("#"===o&&0===n)return e;if(!1===/[a-zA-Z0-9]/.test(o)&&","!==o&&":"!==o&&"-"!==o&&"_"!==o&&0===n)return e}}return r.length}parseToken(e){if(!1===e.includes("{"))return{type:"string",value:e};var[e,r]=e.split("{",2),r=r.slice(0,-1);if("rand"===e)return{type:"random",value:this.parseRandomOptions(r)};if("uuid"===e)return{type:"uuid",value:this.parseUUIDOptions(r)};if("custom"===e)return{type:"custom",value:r};throw new Error("Type de token non supporté: "+e)}parseRandomOptions(e){const r=8,t="alphanumeric",n=!1;e=this.parseOptions(e);return{size:e.size?parseInt(e.size):r,type:e.type||t,variant:"true"===e.variant||n}}parseUUIDOptions(e){return{version:this.parseOptions(e).version||"v4"}}parseOptions(r){var t={};let n="",a="",i=!1,o="",s=0;for(let e=0;e<r.length;e++){var
|
|
1
|
+
let globalFunct=(e=>{class r{static instance;cache=new Map;constructor(){}static getInstance(){return r.instance||(r.instance=new r),r.instance}generate(e,r={}){return this.parseFormat(e).map(e=>this.resolveToken(e,r)).join("")}parseFormat(e){var r=[];let t=0;for(;t<e.length;){var n=e.indexOf("#",t);if(n>t&&(r.push({type:"string",value:e.substring(t,n)}),t=n),"#"===e[t]){var a=this.findTokenEnd(e,t+1);if(-1===a)throw new Error("Format invalide à la position "+t);var i=e.substring(t+1,a),i=this.parseToken(i);r.push(i),t=a}else if(-1===n){r.push({type:"string",value:e.substring(t)});break}}return r}findTokenEnd(r,t){let n=0,a=!1,i=!1;for(let e=t;e<r.length;e++){var o=r[e];if(i)i=!1;else if("\\"===o)i=!0;else if('"'===o||"'"===o)a=!a;else if(!a)if("{"===o)n++;else if("}"===o){if(0===n)return e;n--}else{if("#"===o&&0===n)return e;if(!1===/[a-zA-Z0-9]/.test(o)&&","!==o&&":"!==o&&"-"!==o&&"_"!==o&&0===n)return e}}return r.length}parseToken(e){if(!1===e.includes("{"))return{type:"string",value:e};var[e,r]=e.split("{",2),r=r.slice(0,-1);if("rand"===e)return{type:"random",value:this.parseRandomOptions(r)};if("uuid"===e)return{type:"uuid",value:this.parseUUIDOptions(r)};if("custom"===e)return{type:"custom",value:r};throw new Error("Type de token non supporté: "+e)}parseRandomOptions(e){const r=8,t="alphanumeric",n=!1;e=this.parseOptions(e);return{size:e.size?parseInt(e.size):r,type:e.type||t,variant:"true"===e.variant||n}}parseUUIDOptions(e){return{version:this.parseOptions(e).version||"v4"}}parseOptions(r){var t={};let n="",a="",i=!1,o="",s=0;for(let e=0;e<r.length;e++){var d=r[e];if('"'===d||"'"===d)i?o===d?i=!1:a+=d:(i=!0,o=d);else{if(!i)if(":"===d){if(0===s){n=a.trim(),a="";continue}}else if("{"===d)s++;else if("}"===d)s--;else if(","===d&&0===s){t[n]=a.trim(),n="",a="";continue}a+=d}}return n&&(t[n]=a.trim()),t}resolveToken(e,r){switch(e.type){case"string":return e.value;case"random":return this.generateRandomString(e.value);case"uuid":return this.generateUUID(e.value);case"custom":var t=e.value.split("}").join("").split("{").join(""),n=Object.keys(r).includes(t);if(n)return r[t]();throw console.log("[id-generator -> id-generator] IdentifierGenerator | resolveToken - custom - token:: ",e),console.log("[id-generator -> id-generator] IdentifierGenerator | resolveToken - custom - handlerName:: ",t),console.log("[id-generator -> id-generator] IdentifierGenerator | resolveToken - custom - checker:: ",n),console.log("[id-generator -> id-generator] IdentifierGenerator | resolveToken - custom - Object.keys(customHandlers):: ",Object.keys(r)),console.log("[id-generator -> id-generator] IdentifierGenerator | resolveToken - custom - customHandlers:: ",r),new Error("Handler personnalisé non trouvé: "+t);default:throw new Error("Type de token inconnu: "+e.type)}}generateRandomString(r){var e=this.getRandomCacheKey(r);if(!r.variant){this.cache.has("random")||this.cache.set("random",new Map);var t=this.cache.get("random");if(t.has(e))return t.get(e)}let n="",a="";switch(r.type){case"numeric":n="0123456789";break;case"alphabetic":n="abcdefghijklmnopqrstuvwxyz";break;case"alphabetic-case":n="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";break;case"alphanumeric":n="abcdefghijklmnopqrstuvwxyz0123456789";break;case"alphanumeric-case":n="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"}var i=new Uint32Array(r.size);crypto.getRandomValues(i);for(let e=0;e<r.size;e++)a+=n[i[e]%n.length];return r.variant||this.cache.get("random").set(e,a),a}generateUUID(e){var r="uuid-"+e.version,t=(this.cache.has("uuid")||this.cache.set("uuid",new Map),this.cache.get("uuid"));if(t.has(r))return"v4"===e.version?this.generateUUIDv4():t.get(r);let n;switch(e.version){case"v1":n=this.generateUUIDv1();break;case"v2":n=this.generateUUIDv2();break;case"v3":n=this.generateUUIDv3();break;case"v4":n=this.generateUUIDv4();break;case"v5":n=this.generateUUIDv5();break;default:n=this.generateUUIDv4()}return"v3"!==e.version&&"v5"!==e.version||t.set(r,n),n}generateUUIDv1(){var e=Date.now().toString(16).padStart(12,"0");return`${e.slice(0,8)}-${e.slice(8,12)}-1000-8000-`+this.getRandomHex(12)}generateUUIDv2(){return this.generateUUIDv1()}generateUUIDv3(){return"xxxxxxxx-xxxx-3xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,e=>{var r=16*Math.random()|0;return("x"===e?r:3&r|8).toString(16)})}generateUUIDv4(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,e=>{var r=crypto.getRandomValues(new Uint8Array(1))[0]%16|0;return("x"===e?r:3&r|8).toString(16)})}generateUUIDv5(){return"xxxxxxxx-xxxx-5xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,e=>{var r=16*Math.random()|0;return("x"===e?r:3&r|8).toString(16)})}getRandomHex(e){var r=new Uint8Array(Math.ceil(e/2));return crypto.getRandomValues(r),Array.from(r).map(e=>e.toString(16).padStart(2,"0")).join("").slice(0,e)}getRandomCacheKey(e){return e.size+`-${e.type}-`+e.variant}clearCache(){this.cache.clear()}static generateId(e,r={}){return this.getInstance().generate(e,r)}static exposeToGlobal(){"undefined"!=typeof window&&(window.IdentifierGenerator=r,window.IdentifierGeneratorHandlers=t)}}let t={timestamp:()=>Date.now().toString(),date:()=>(new Date).toISOString().slice(0,10).replace(/-/g,""),time:()=>(new Date).toISOString().slice(11,19).replace(/:/g,""),counter:(()=>{let e=0;return()=>(++e).toString().padStart(6,"0")})(),env:e=>process.env[e]||""};"undefined"!=typeof window&&(window.IdentifierGenerator=r,window.IdentifierGeneratorHandlers=t);var n={IdentifierGenerator:r,IdentifierGeneratorHandlers:t},a={default:n,IdentifierGenerator:r,IdentifierGeneratorHandlers:t},i=a;return void 0!==e&&(e.default=n,e.IdentifierGenerator=r,e.IdentifierGeneratorHandlers=t,e.__bundledModules=i),"undefined"!=typeof window&&(window.default=n,window.IdentifierGenerator=r,window.IdentifierGeneratorHandlers=t,window.__bundledModules=i),"undefined"!=typeof exports&&(exports.default=n,exports.IdentifierGenerator=r,exports.IdentifierGeneratorHandlers=t),a})("undefined"!=typeof global?global:"undefined"!=typeof window?window:"undefined"!=typeof self?self:"undefined"!=typeof globalThis?globalThis:{}),IdentifierGeneratorElementGF=globalFunct.IdentifierGenerator,IdentifierGeneratorHandlersElementGF=globalFunct.IdentifierGeneratorHandlers,defaultElementGF=globalFunct.default;
|