|
| 1 | +import { Component, OnDestroy, OnInit } from '@angular/core'; |
| 2 | +import { ToastService } from '../../../shared/services/toast.service'; |
| 3 | +import { ToolStateService } from '../../../shared/services/tool-state.service'; |
| 4 | +import { ActiveToolService } from '../../../shared/services/active-tool.service'; |
| 5 | + |
| 6 | +const ULID_ALPHABET = '0123456789ABCDEFGHJKMNPQRSTVWXYZ'; |
| 7 | + |
| 8 | +type SortableIdType = 'uuidv7' | 'ulid'; |
| 9 | + |
| 10 | +@Component({ |
| 11 | + selector: 'app-sortable-ids', |
| 12 | + standalone: false, |
| 13 | + templateUrl: './sortable-ids.component.html', |
| 14 | + styleUrl: './sortable-ids.component.css' |
| 15 | +}) |
| 16 | +export class SortableIdsComponent implements OnInit, OnDestroy { |
| 17 | + kind: SortableIdType = 'uuidv7'; |
| 18 | + count = 5; |
| 19 | + output = ''; |
| 20 | + error = ''; |
| 21 | + private readonly runHandler = () => this.generate(); |
| 22 | + |
| 23 | + constructor( |
| 24 | + private toast: ToastService, |
| 25 | + private toolState: ToolStateService, |
| 26 | + private activeTool: ActiveToolService |
| 27 | + ) {} |
| 28 | + |
| 29 | + ngOnInit(): void { |
| 30 | + this.kind = this.toolState.get('sortable-ids.kind', 'uuidv7'); |
| 31 | + this.count = this.toolState.get('sortable-ids.count', 5); |
| 32 | + this.activeTool.register(this.runHandler); |
| 33 | + } |
| 34 | + |
| 35 | + ngOnDestroy(): void { |
| 36 | + this.activeTool.clear(this.runHandler); |
| 37 | + } |
| 38 | + |
| 39 | + generate(): void { |
| 40 | + this.error = ''; |
| 41 | + try { |
| 42 | + const total = Math.max(1, Math.min(50, Number(this.count) || 1)); |
| 43 | + const ids = Array.from({ length: total }, () => this.kind === 'ulid' ? createUlid() : createUuidV7()); |
| 44 | + this.output = ids.join('\n'); |
| 45 | + this.toolState.set('sortable-ids.kind', this.kind); |
| 46 | + this.toolState.set('sortable-ids.count', total); |
| 47 | + this.toast.success('Identifiers generated.'); |
| 48 | + } catch (error) { |
| 49 | + this.error = error instanceof Error ? error.message : 'Unable to generate identifiers.'; |
| 50 | + this.toast.error('Unable to generate identifiers.'); |
| 51 | + } |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +function createUuidV7(): string { |
| 56 | + const bytes = new Uint8Array(16); |
| 57 | + const now = Date.now(); |
| 58 | + bytes[0] = (now >>> 40) & 0xff; |
| 59 | + bytes[1] = (now >>> 32) & 0xff; |
| 60 | + bytes[2] = (now >>> 24) & 0xff; |
| 61 | + bytes[3] = (now >>> 16) & 0xff; |
| 62 | + bytes[4] = (now >>> 8) & 0xff; |
| 63 | + bytes[5] = now & 0xff; |
| 64 | + |
| 65 | + const random = crypto.getRandomValues(new Uint8Array(10)); |
| 66 | + bytes[6] = (random[0] & 0x0f) | 0x70; |
| 67 | + bytes[7] = random[1]; |
| 68 | + bytes[8] = (random[2] & 0x3f) | 0x80; |
| 69 | + bytes[9] = random[3]; |
| 70 | + bytes[10] = random[4]; |
| 71 | + bytes[11] = random[5]; |
| 72 | + bytes[12] = random[6]; |
| 73 | + bytes[13] = random[7]; |
| 74 | + bytes[14] = random[8]; |
| 75 | + bytes[15] = random[9]; |
| 76 | + |
| 77 | + const hex = Array.from(bytes, b => b.toString(16).padStart(2, '0')).join(''); |
| 78 | + return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`; |
| 79 | +} |
| 80 | + |
| 81 | +function createUlid(): string { |
| 82 | + const time = encodeUlidTime(Date.now()); |
| 83 | + const randomness = encodeUlidRandomness(); |
| 84 | + return `${time}${randomness}`; |
| 85 | +} |
| 86 | + |
| 87 | +function encodeUlidTime(ms: number): string { |
| 88 | + let value = BigInt(ms); |
| 89 | + let output = ''; |
| 90 | + for (let i = 0; i < 10; i++) { |
| 91 | + const mod = Number(value % 32n); |
| 92 | + output = ULID_ALPHABET[mod] + output; |
| 93 | + value = value / 32n; |
| 94 | + } |
| 95 | + return output; |
| 96 | +} |
| 97 | + |
| 98 | +function encodeUlidRandomness(): string { |
| 99 | + const randomBytes = crypto.getRandomValues(new Uint8Array(10)); |
| 100 | + let value = 0n; |
| 101 | + for (const byte of randomBytes) { |
| 102 | + value = (value << 8n) | BigInt(byte); |
| 103 | + } |
| 104 | + let output = ''; |
| 105 | + for (let i = 0; i < 16; i++) { |
| 106 | + const mod = Number(value % 32n); |
| 107 | + output = ULID_ALPHABET[mod] + output; |
| 108 | + value = value / 32n; |
| 109 | + } |
| 110 | + return output; |
| 111 | +} |
0 commit comments