Skip to content

Commit

Permalink
Доработки по форматированию телефонных номеров
Browse files Browse the repository at this point in the history
  • Loading branch information
hcodes committed Nov 4, 2016
1 parent 8dc5282 commit 989d509
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
32 changes: 22 additions & 10 deletions src/rules/ru/other/phone-number.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,21 +115,33 @@ function phoneBlocks(num){
return add + num.split(/(?=(?:\d\d)+$)/).join('-');
}

function clearPhone(text) {
return text.replace(/[^\d\+]/g, '');
}

Typograf.rule({
name: 'ru/other/phone-number',
live: false,
handler: function(text) {
return text.replace(
/(т.|тел.|ф.|моб.|факс|сотовый|мобильный|телефон)(\:?\s*?)([\+\d\(][\d \u00A0\-\(\)]{3,}\d)/gi,
function($0, $1, $2, $3) {
var buf = $3.replace(/[^\d\+]/g, '');
if (buf.length >= 5) {
return $1 + $2 + phone(buf);
var tag = Typograf._privateLabel,
re = new RegExp('(^|,| |' + tag + ')(\\+7[\\d\\(\\) \u00A0-]{10,18})(?=,|;|' + tag + '|$)', 'gm');

return text
.replace(re, function($0, $1, $2) {
var buf = clearPhone($2);
return buf.length === 12 ? $1 + phone(buf) : $0;
})
.replace(
/(^|[^а-яё])(т\.|тел\.|ф\.|моб\.|факс|сотовый|мобильный|телефон)(\:?\s*?)([\+\d\(][\d \u00A0\-\(\)]{3,}\d)/gi,
function($0, $1, $2, $3, $4) {
var buf = clearPhone($4);
if (buf.length >= 5) {
return $1 + $2 + $3 + phone(buf);
}

return $0;
}

return $0;
}
);
);
}
});

Expand Down
6 changes: 6 additions & 0 deletions src/rules/ru/other/phone-number.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
tests.push(['ru/other/phone-number', [
[' тел.: +74957397000', ' тел.: +7\u00A0495\u00A0739-70-00'],
[' ГОСТ 22222 ', ' ГОСТ 22222 '],
['тел.: 812 5551234', 'тел.: (812)\u00A0555-12-34'],
['тел.: +7915178-22-12', 'тел.: +7\u00A0915\u00A0178-22-12'],
['телефон 495 555-1234', 'телефон 495\u00A0555-12-34'],
['моб. +79225551234', 'моб. +7\u00A0922\u00A0555-12-34'],
['+79225551234', '+7\u00A0922\u00A0555-12-34'],
['\n+79225551234\n+79225551235', '\n+7\u00A0922\u00A0555-12-34\n+7\u00A0922\u00A0555-12-35'],
['+7922555123', '+7922555123'],
[',+79225551234\n', ',+7\u00A0922\u00A0555-12-34\n'],
['\n+79225551234\n', '\n+7\u00A0922\u00A0555-12-34\n'],
['моб. +19225551234', 'моб. +19225551234'],
['Телефон: (4942)514172', 'Телефон: (49-42)\u00A051-41-72'],
['Телефон: 51-41-72', 'Телефон: 51-41-72'],
Expand Down

0 comments on commit 989d509

Please sign in to comment.