UNPKG

srid

Version:

A library for generating secure shared, traceable, and country-aware IDs for users, wallets, and transactions.

55 lines (52 loc) 1.81 kB
"use strict"; // SHARED ROOT IDENTIFIER (SRID) Library (TypeScript) Object.defineProperty(exports, "__esModule", { value: true }); // Utility: Generate a random 6-char alphanumeric string function generateHashChunk(length = 6) { const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; let result = ''; for (let i = 0; i < length; i++) { result += chars.charAt(Math.floor(Math.random() * chars.length)); } return result; } // Utility: Format date as YYMMDD function getCurrentDateYYMMDD() { const now = new Date(); const yy = String(now.getFullYear()).slice(-2); const mm = String(now.getMonth() + 1).padStart(2, '0'); const dd = String(now.getDate()).padStart(2, '0'); return `${yy}${mm}${dd}`; } class SRID { constructor(countryCode) { this.countryCode = countryCode.toUpperCase(); this.sharedId = `${this.countryCode}${getCurrentDateYYMMDD()}-${generateHashChunk()}`; this.transactionCount = 0; } getSharedId() { return this.sharedId; } generateUserId() { return `USR-${this.sharedId}`; } generateWalletId() { return `WLT-${this.sharedId}`; } generateTransactionId() { this.transactionCount++; const txnSuffix = String(this.transactionCount).padStart(3, '0'); return `TXN-${this.sharedId}-${txnSuffix}`; } } exports.default = SRID; /* Usage Example: import SRID from './srid'; const srid = new SRID('KE'); console.log(srid.getSharedId()); // e.g., KE250326-A7K2Z9 console.log(srid.generateUserId()); // e.g., USR-KE250326-A7K2Z9 console.log(srid.generateWalletId()); // e.g., WLT-KE250326-A7K2Z9 console.log(srid.generateTransactionId()); // e.g., TXN-KE250326-A7K2Z9-001 console.log(srid.generateTransactionId()); // e.g., TXN-KE250326-A7K2Z9-002 */