A TypeScript library for generating financial-safe reference codes with built-in persistence support to avoid duplicates.
- Financial-Safe Character Set: Uses
23456789ABCDEFGHJKLMNPQRSTUVWXYZby default, avoiding similar-looking characters (1, I, i, l, 0, O, o) - Cryptographically Secure: Uses Node.js crypto module for random generation
- Configurable Length: Default 6 characters, customizable up to 12
- Duplicate Prevention: Built-in persistence layer to avoid generating duplicate references
- TTL Support: Automatic expiration of references (default 24 hours)
- Multiple Adapters: In-memory (default) and Redis persistence options
- TypeScript First: Full type safety and IntelliSense support
pnpm add fin-riff
# or
npm install fin-riff
# or
yarn add fin-riffimport { FinRiff } from 'fin-riff';
const finriff = new FinRiff();
// Generate a 6-character reference
const reference = await finriff.generate();
console.log(reference); // e.g., "A3K9P2"
// Generate with custom length
const longRef = await finriff.generate(10);
console.log(longRef); // e.g., "B4N7X2M9Q5"
// Generate multiple references
const batch = await finriff.generateBatch(5);
console.log(batch); // Array of 5 unique referencesimport { FinRiff, MemoryAdapter, RedisAdapter } from 'fin-riff';
// Default configuration
const finriff = new FinRiff({
length: 6, // Default reference length
ttl: 24 * 60 * 60 * 1000, // 24 hours in milliseconds
charset: '23456789ABCDEFGHJKLMNPQRSTUVWXYZ', // Safe charset
adapter: new MemoryAdapter() // In-memory persistence
});import { FinRiff, MemoryAdapter } from 'fin-riff';
const adapter = new MemoryAdapter(60000); // Cleanup interval in ms
const finriff = new FinRiff({ adapter });import { FinRiff, RedisAdapter } from 'fin-riff';
import Redis from 'ioredis';
// Option 1: Pass existing Redis client
const redis = new Redis();
const adapter = new RedisAdapter({
client: redis,
prefix: 'finriff:'
});
// Option 2: Let adapter create client
const adapter = new RedisAdapter({
host: 'localhost',
port: 6379,
password: 'your-password',
db: 0,
clientType: 'ioredis', // or 'redis'
prefix: 'finriff:'
});
const finriff = new FinRiff({ adapter });Creates a new FinRiff instance.
Generates a unique reference code.
Generates multiple unique reference codes.
Checks if a reference is unique (not in persistence).
Reserves a custom reference if available.
Clears all stored references.
Returns the count of stored references.
Calculates total possible combinations for given length.
Estimates the probability of collision based on existing references.
const finriff = new FinRiff({
charset: 'ABCDEF123456', // Hexadecimal-like
length: 8
});const finriff = new FinRiff();
// Check how many references exist
const count = await finriff.size();
// Calculate collision probability
const probability = finriff.estimateCollisionProbability(count, 6);
console.log(`Collision probability: ${(probability * 100).toFixed(4)}%`);
// Total possible combinations
const combinations = finriff.calculatePossibleCombinations(6);
console.log(`Total combinations: ${combinations.toLocaleString()}`);const finriff = new FinRiff();
// Try to reserve a specific reference
const reserved = await finriff.reserve('CUSTOM1');
if (reserved) {
console.log('Reference reserved successfully');
} else {
console.log('Reference already exists');
}The default character set 2356789BCDEFGHJKLMNPQRSTUVWXYZ excludes:
0andO(zero and letter O)1,I, andl(one, capital i, lowercase L)Aand4
This ensures references are unambiguous when read by customers or entered manually.
- 6 characters: 729,000,000 possible combinations
- 8 characters: 656,100,000,000 combinations (~656 billion)
- 10 characters: 590,490,000,000,000 combinations (~590 trillion)
- 12 characters: 531,441,000,000,000,000 combinations (~531 quadrillion)
pnpm test # Run tests
pnpm test:ui # Run tests with UI
pnpm test:coverage # Generate coverage reportpnpm build # Compile TypeScript
pnpm dev # Watch modeMIT