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.
47 lines (46 loc) • 1.41 kB
JavaScript
export class DigiPinError extends Error {
constructor(message) {
super(message);
this.name = 'DigiPinError';
}
}
export 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';
}
}
export class PinFormatError extends DigiPinError {
constructor(pin) {
super('Invalid DIGIPIN format');
this.pin = pin;
this.name = 'PinFormatError';
}
}
export 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';
}
}
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;
}