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.
54 lines (53 loc) • 1.73 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.InvalidCharacterError = exports.PinFormatError = exports.BoundsError = exports.DigiPinError = void 0;
class DigiPinError extends Error {
constructor(message) {
super(message);
this.name = 'DigiPinError';
}
}
exports.DigiPinError = DigiPinError;
class BoundsError extends DigiPinError {
constructor(latitude, longitude, bounds) {
super('Coordinates out of supported bounds');
this.latitude = latitude;
this.longitude = longitude;
this.bounds = bounds;
this.name = 'BoundsError';
}
}
exports.BoundsError = BoundsError;
class PinFormatError extends DigiPinError {
constructor(pin) {
super('Invalid DIGIPIN format');
this.pin = pin;
this.name = 'PinFormatError';
}
}
exports.PinFormatError = PinFormatError;
class InvalidCharacterError extends DigiPinError {
constructor(char) {
const suggestion = getSuggestion(char);
const message = suggestion
? `Invalid character '${char}' in DIGIPIN. Did you mean '${suggestion}'?`
: `Invalid character '${char}' in DIGIPIN`;
super(message);
this.name = 'InvalidCharacterError';
}
}
exports.InvalidCharacterError = InvalidCharacterError;
function getSuggestion(char) {
const map = {
'0': 'C', // 0 looks like C or O (not used)
'1': 'J', // 1 looks like J or I (not used)
'O': '0', // O is not used, maybe 0? (also not used, but C is close)
'I': 'J', // I is not used
'Q': '9',
'Z': '2',
'S': '5',
'B': '8',
'G': '6',
};
return map[char.toUpperCase()] || null;
}