nigerian-mobile-validator
Version:
The most rigorous, up-to-date library for validating Nigerian mobile numbers. Fully NCC-compliant, and security-focused, with enterprise-grade features to prevent the business risks of validation failures in regulated industries.
49 lines (48 loc) • 1.7 kB
JavaScript
// src/numbering-plan/mobile-number-range.ts
/**
* Represents a range of NCC-assigned numbers.
*
* This class does not know whether it is storing the subscriber number part
* or the local number part. In fact, all it knows is that it stores an ordered
* set of 2 integers.
*
* NOTE: By definition the "local number" begins with the network access code (e.g. 08031234567)
* while the "subscriber number" part does not include the access code (e.g. 1234567).
*/
export class MobileNumberRange {
constructor(lowerBound, upperBound) {
this.lowerBound = lowerBound;
this.upperBound = upperBound;
if (lowerBound >= upperBound) {
throw new Error('Lower bound must be less than upper bound');
}
}
/**
* Checks whether the given MobileNumberRange is a subset of this object.
*/
isSubset(otherMobileNumberRange) {
return otherMobileNumberRange.lowerBound >= this.lowerBound &&
otherMobileNumberRange.upperBound <= this.upperBound;
}
/**
* Checks whether the given number lies within the lower and upperbounds of this object.
*/
isWithinRange(usersNumber) {
return usersNumber >= this.lowerBound && usersNumber <= this.upperBound;
}
/**
* Compares this range with another range.
* Returns -1 if this range comes before, 1 if it comes after, 0 if they overlap.
*/
compareTo(otherMobileNumberRange) {
if (this.upperBound < otherMobileNumberRange.lowerBound) {
return -1;
}
else if (otherMobileNumberRange.upperBound < this.lowerBound) {
return 1;
}
else {
return 0;
}
}
}