@botique/libphonenumber-js
Version:
A simpler (and smaller) rewrite of Google Android's popular libphonenumber library
110 lines (85 loc) • 3.94 kB
JavaScript
;
var _typeof2 = require('babel-runtime/helpers/typeof');
var _typeof3 = _interopRequireDefault(_typeof2);
var _metadata2 = require('../metadata.full');
var _metadata3 = _interopRequireDefault(_metadata2);
var _metadata4 = require('./metadata');
var _metadata5 = _interopRequireDefault(_metadata4);
var _types = require('../source/types');
var _types2 = _interopRequireDefault(_types);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function get_number_type() {
for (var _len = arguments.length, parameters = Array(_len), _key = 0; _key < _len; _key++) {
parameters[_key] = arguments[_key];
}
parameters.push(_metadata3.default);
return _types2.default.apply(this, parameters);
}
describe('get_number_type', function () {
it('should infer phone number type MOBILE', function () {
get_number_type('9150000000', 'RU').should.equal('MOBILE');
get_number_type('7912345678', 'GB').should.equal('MOBILE');
get_number_type('91187654321', 'AR').should.equal('MOBILE');
get_number_type('15123456789', 'DE').should.equal('MOBILE');
get_number_type('51234567', 'EE').should.equal('MOBILE');
});
it('should infer phone number types', function () {
get_number_type('88005553535', 'RU').should.equal('TOLL_FREE');
get_number_type('8005553535', 'RU').should.equal('TOLL_FREE');
get_number_type('4957777777', 'RU').should.equal('FIXED_LINE');
get_number_type('8030000000', 'RU').should.equal('PREMIUM_RATE');
get_number_type('2133734253', 'US').should.equal('FIXED_LINE_OR_MOBILE');
get_number_type('5002345678', 'US').should.equal('PERSONAL_NUMBER');
});
it('should return FIXED_LINE_OR_MOBILE when there is ambiguity', function () {
// (no such country in the metadata, therefore no unit test for this `if`)
});
it('should check phone number length', function () {
// Too short.
check_number_length('800555353', 'FIXED_LINE', 'RU').should.equal('TOO_SHORT');
// Normal.
check_number_length('8005553535', 'FIXED_LINE', 'RU').should.equal('IS_POSSIBLE');
// Too long.
check_number_length('80055535355', 'FIXED_LINE', 'RU').should.equal('TOO_LONG');
// No such type.
check_number_length('169454850', 'VOIP', 'AC').should.equal('INVALID_LENGTH');
// No such possible length.
check_number_length('1694548', undefined, 'AD').should.equal('INVALID_LENGTH');
// FIXED_LINE_OR_MOBILE
check_number_length('1694548', 'FIXED_LINE_OR_MOBILE', 'AD').should.equal('INVALID_LENGTH');
// No mobile phones.
check_number_length('8123', 'FIXED_LINE_OR_MOBILE', 'TA').should.equal('IS_POSSIBLE');
// No "possible lengths" for "mobile".
check_number_length('81234567', 'FIXED_LINE_OR_MOBILE', 'SZ').should.equal('IS_POSSIBLE');
});
it('should work in edge cases', function () {
// No metadata
var thrower = function thrower() {
return (0, _types2.default)({ phone: '+78005553535' });
};
thrower.should.throw('Metadata is required');
// Parsed phone number
get_number_type({ phone: '8005553535', country: 'RU' }).should.equal('TOLL_FREE');
// Invalid phone number
type(get_number_type('123', 'RU')).should.equal('undefined');
// Invalid country
thrower = function thrower() {
return get_number_type({ phone: '8005553535', country: 'RUS' });
};
thrower.should.throw('Unknown country');
// Numerical `value`
thrower = function thrower() {
return get_number_type(89150000000, 'RU');
};
thrower.should.throw('A phone number must either be a string or an object of shape { phone, [country] }.');
});
});
function type(something) {
return typeof something === 'undefined' ? 'undefined' : (0, _typeof3.default)(something);
}
function check_number_length(number, type, country) {
var _metadata = new _metadata5.default(_metadata3.default);
_metadata.country(country);
return (0, _types.check_number_length_for_type)(number, type, _metadata);
}
//# sourceMappingURL=types.test.js.map