digit-to-words-nepali
Version:
A comprehensive TypeScript library for converting numbers to words in English and Nepali languages. Supports numbers up to 10^39 (Adanta Singhar), currency formatting, decimal handling with individual digit pronunciation, and BigInt. Zero dependencies, fu
68 lines (67 loc) • 1.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.conversionCache = exports.ConversionCache = void 0;
// Create a stable string key for caching
const createCacheKey = (key) => [
key.value,
key.lang,
key.isCurrency ? '1' : '0',
key.includeDecimal ? '1' : '0',
key.individualDecimalDigits ? '1' : '0',
key.currency || '',
key.decimalSuffix || '',
key.currencyDecimalSuffix || ''
].join('|');
/**
* LRU Cache for conversion results
*/
class ConversionCache {
constructor(maxSize = 1000) {
this.cache = new Map();
this.maxSize = maxSize;
}
/**
* Get cached result, implementing LRU behavior.
*/
get(key) {
const cacheKey = createCacheKey(key);
const result = this.cache.get(cacheKey);
// Move to end of Map to implement LRU behavior
if (result) {
this.cache.delete(cacheKey);
this.cache.set(cacheKey, result);
}
return result;
}
/**
* Set cached result, evicting oldest if necessary.
*/
set(key, result) {
const cacheKey = createCacheKey(key);
// Ensure cache doesn't exceed max size by removing oldest entry
if (this.cache.size >= this.maxSize) {
const oldestKey = this.cache.keys().next().value;
if (oldestKey !== undefined) {
this.cache.delete(oldestKey);
}
}
this.cache.set(cacheKey, result);
}
/**
* Clear all cached entries.
*/
clear() {
this.cache.clear();
}
/**
* Get current cache size.
*/
get size() {
return this.cache.size;
}
}
exports.ConversionCache = ConversionCache;
/**
* Global singleton cache instance
*/
exports.conversionCache = new ConversionCache();