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.
52 lines (51 loc) • 2.86 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const chai_1 = require("chai");
const core_1 = require("../core");
describe('DigiPin Core Functions', () => {
describe('getDigiPin', () => {
it('should encode valid coordinates to digipin', () => {
// Test a known coordinate pair
const lat = 28.6139; // Delhi
const lng = 77.2090;
const pin = (0, core_1.getDigiPin)(lat, lng);
(0, chai_1.expect)(pin).to.match(/^[FC98J327K456LMPT]{3}-[FC98J327K456LMPT]{3}-[FC98J327K456LMPT]{4}$/);
});
it('should throw error for out of bounds latitude', () => {
(0, chai_1.expect)(() => (0, core_1.getDigiPin)(1, 77.2090)).to.throw('Latitude out of range');
(0, chai_1.expect)(() => (0, core_1.getDigiPin)(40, 77.2090)).to.throw('Latitude out of range');
});
it('should throw error for out of bounds longitude', () => {
(0, chai_1.expect)(() => (0, core_1.getDigiPin)(28.6139, 60)).to.throw('Longitude out of range');
(0, chai_1.expect)(() => (0, core_1.getDigiPin)(28.6139, 100)).to.throw('Longitude out of range');
});
});
describe('getLatLngFromDigiPin', () => {
it('should decode valid digipin to coordinates', () => {
const pin = 'K4P-9C6-LMPT';
const result = (0, core_1.getLatLngFromDigiPin)(pin);
(0, chai_1.expect)(result).to.have.property('latitude');
(0, chai_1.expect)(result).to.have.property('longitude');
(0, chai_1.expect)(result.latitude).to.be.at.least(2.5);
(0, chai_1.expect)(result.latitude).to.be.at.most(38.5);
(0, chai_1.expect)(result.longitude).to.be.at.least(63.5);
(0, chai_1.expect)(result.longitude).to.be.at.most(99.5);
});
it('should throw error for invalid digipin length', () => {
(0, chai_1.expect)(() => (0, core_1.getLatLngFromDigiPin)('K4P-9C6')).to.throw('Invalid DIGIPIN');
(0, chai_1.expect)(() => (0, core_1.getLatLngFromDigiPin)('K4P-9C6-LMPT-EXTRA')).to.throw('Invalid DIGIPIN');
});
it('should throw error for invalid characters', () => {
(0, chai_1.expect)(() => (0, core_1.getLatLngFromDigiPin)('K4P-9C6-LMP1')).to.throw('Invalid character');
});
it('should roundtrip encode and decode', () => {
const lat = 28.6139;
const lng = 77.2090;
const pin = (0, core_1.getDigiPin)(lat, lng);
const result = (0, core_1.getLatLngFromDigiPin)(pin);
// Allow for small floating point differences
(0, chai_1.expect)(Math.abs(result.latitude - lat)).to.be.lessThan(0.0001);
(0, chai_1.expect)(Math.abs(result.longitude - lng)).to.be.lessThan(0.0001);
});
});
});