react-phone-number-input
Version:
Telephone input for React
245 lines (205 loc) • 5.87 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.formats = undefined;
var _getIterator2 = require('babel-runtime/core-js/get-iterator');
var _getIterator3 = _interopRequireDefault(_getIterator2);
exports.validate = validate;
exports.cleartext_international = cleartext_international;
exports.template = template;
exports.digits = digits;
exports.count_digits = count_digits;
exports.format = format;
exports.format_international = format_international;
exports.parse_digits = parse_digits;
exports.digits_in_number = digits_in_number;
exports.digit_index = digit_index;
exports.index_in_template = index_in_template;
exports.repeat = repeat;
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var formats = exports.formats = {
RU: {
country: '7',
city: 3,
number: [3, 2, 2]
},
UA: {
country: '380',
city: 2,
number: [3, 2, 2]
},
BY: {
country: '375',
city: 2,
number: [3, 2, 2]
},
US: {
country: '1',
city: 3,
number: [3, 4]
}
};
// Validates an international cleartext phone number
function validate(cleartext_international, format) {
if (!format) {
throw new Error('No "format" specified for phone number validation');
}
return cleartext_international.length === '+'.length + format.country.length + digits_in_number(format);
}
// Reduces a formatted phone number to a cleartext one (with country code).
// E.g. "(999) 123-45-67" -> "+79991234567"
function cleartext_international(formatted, format) {
return '+' + format.country + digits(formatted, format);
}
// Generates phone number template based on the phone format structure.
// e.g. { code: '7', city: 3, number: [3, 2, 2] } -> '(xxx) xxx-xx-xx'
function template(format) {
return '(' + repeat('x', format.city) + ') ' + format.number.map(function (n) {
return repeat('x', n);
}).join('-');
}
// Converts formatted phone number to just digits
// (e.g. "(999) 123-45-67" -> "9991234567")
function digits(value, format) {
return value.replace(/[^0-9]/g, '').substring(0, digits_in_number(format));
}
// Counts digits in a string
function count_digits(value) {
return value.replace(/[^0-9]/g, '').length;
}
// Formats a cleartext phone number
// as a local phone number.
//
// E.g.: "+79991234567" -> "(999) 123-45-67"
// "9991234567" -> "(999) 123-45-67"
//
function format(value, format) {
// Trims the value
value = value.trim();
// If the value starts with a plus sign,
// then trim it along with the country code.
// (because country code is not editable)
if (value[0] === '+') {
value = value.substring('+'.length + format.country.length);
}
if (!value) {
return '';
}
// If the value has something except digits, then abort
if (value.match(/[^0-9]/)) {
return value;
}
// Transform raw digits "9991234567"
// into a structure { city: '999', number: '1234567' }
var phone = parse_digits(value, format);
// Adds hyphens to phone number
// (e.g. '1234567' -> '123-45-67')
var number_parts = [];
var cursor = 0;
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = (0, _getIterator3.default)(format.number), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var digit_count = _step.value;
var number_part = phone.number.slice(cursor, cursor + digit_count);
if (number_part) {
number_parts.push(number_part);
}
cursor += digit_count;
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
var number = number_parts.join('-');
// Adds city code whitespace
// (e.g. '9' -> '9 ')
var city = phone.city;
while (city.length < format.city) {
city += ' ';
}
// The resulting formatted phone number
// (e.g. '(999) 123-45-67')
return '(' + city + ') ' + number;
}
// Formats a cleartext phone number
// as an international phone number.
//
// E.g.: "+79991234567" -> "+7 (999) 123-45-67"
// "9991234567" -> "+7 (999) 123-45-67"
//
function format_international(cleartext, phone_format) {
var prefix = '+' + phone_format.country;
var number = format(cleartext, phone_format);
if (!number) {
return '';
}
return prefix + ' ' + number;
}
// Transforms raw digits "9991234567"
// into a structure { city: '999', number: '1234567' }
function parse_digits(digits, format) {
var phone = {
// country : '7',
city: digits.slice(0, format.city),
number: digits.slice(format.city, format.city + format.number.reduce(function (a, b) {
return a + b;
}, 0))
};
return phone;
}
// Returns digit count in phone number format
function digits_in_number(format) {
return format.city + format.number.reduce(function (a, b) {
return a + b;
}, 0);
}
// Finds digit index in value at caret position
// (if there's no digit at caret position then
// returns the index of the closest next digit)
function digit_index(value, caret_position) {
return count_digits(value.substring(0, caret_position));
}
// Finds index of digit symbol in template
function index_in_template(digit_index, format) {
var phone_template = template(format);
var digit_index_so_far = -1;
var i = 0;
while (i <= phone_template.length) {
if (phone_template[i] === 'x') {
digit_index_so_far++;
}
if (digit_index_so_far === digit_index) {
return i;
}
i++;
}
}
// Repeats string N times
function repeat(pattern, count) {
if (count < 1) {
return '';
}
var result = '';
while (count > 1) {
if (count & 1) {
result += pattern;
}
count >>= 1;
pattern += pattern;
}
return result + pattern;
}
//# sourceMappingURL=phone.js.map