digipinjs
Version:
A comprehensive TypeScript library for encoding and decoding Indian geographic coordinates into DIGIPIN format (Indian Postal Digital PIN system). Features CLI tools, caching, batch processing, and Express middleware for seamless integration.
50 lines (49 loc) • 1.62 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCachedEncode = getCachedEncode;
exports.setCachedEncode = setCachedEncode;
exports.getCachedDecode = getCachedDecode;
exports.setCachedDecode = setCachedDecode;
exports.getCached = getCached;
exports.setCached = setCached;
exports.clearEncodeCache = clearEncodeCache;
exports.clearDecodeCache = clearDecodeCache;
exports.clearCache = clearCache;
const lru_cache_1 = __importDefault(require("lru-cache"));
const encodeCache = new lru_cache_1.default({ max: 10000 });
const decodeCache = new lru_cache_1.default({ max: 10000 });
function encodeKey(lat, lng, format) {
return `${lat},${lng}:${format}`;
}
function getCachedEncode(lat, lng, format) {
return encodeCache.get(encodeKey(lat, lng, format));
}
function setCachedEncode(lat, lng, pin, format) {
encodeCache.set(encodeKey(lat, lng, format), pin);
}
function getCachedDecode(pin) {
return decodeCache.get(pin);
}
function setCachedDecode(pin, coordinates) {
decodeCache.set(pin, coordinates);
}
// Backwards compatible helpers (default hyphenated format)
function getCached(lat, lng) {
return getCachedEncode(lat, lng, 'hyphenated');
}
function setCached(lat, lng, pin) {
setCachedEncode(lat, lng, pin, 'hyphenated');
}
function clearEncodeCache() {
encodeCache.clear();
}
function clearDecodeCache() {
decodeCache.clear();
}
function clearCache() {
clearEncodeCache();
clearDecodeCache();
}