Skip to content

Commit

Permalink
fix(message-parser): Phone pattern (#900)
Browse files Browse the repository at this point in the history
* fix phone pattern check

* adding more test and fixing parser

* fixing left char tight with phone number
  • Loading branch information
hugocostadev authored Nov 14, 2022
1 parent 29dd497 commit 6062700
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
8 changes: 6 additions & 2 deletions packages/message-parser/src/grammar.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
task,
tasks,
unorderedList,
phoneChecker,
} = require('./utils');
}

Expand Down Expand Up @@ -151,7 +152,7 @@ Whitespace = w:$" "+ { return plain(w); }

Escaped = "\\" t:$("*" / "_" / "~" / "`" / "#" / ".") { return plain(t); }

Any = !EndOfLine t:$. u:$URL? { return plain(t + u); }
Any = !EndOfLine t:$. p:$AutolinkedPhone? u:$URL? { return plain(t + p + u); }

// = Line

Expand Down Expand Up @@ -297,7 +298,7 @@ unicode
return String.fromCharCode(parseInt(digits, 16));
}

AutolinkedPhone = p:Phone { return link('tel:' + p.number, plain(p.text)); }
AutolinkedPhone = p:Phone { return phoneChecker(p.text, p.number); }

AutolinkedURL = u:URL { return link(u); }

Expand Down Expand Up @@ -369,6 +370,9 @@ phoneNumber
= p:phonePrefix "-" d:digits {
return { text: p.text + '-' + d, number: p.number + d };
}
/ p:phonePrefix d1:digits "-" d2:digits {
return { text: p.text + d1 + '-' + d2, number: p.number + d1 + d2 };
}
/ p:phonePrefix d:digits {
return { text: p.text + d, number: p.number + d };
}
Expand Down
8 changes: 8 additions & 0 deletions packages/message-parser/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,11 @@ export const inlineKatex = (content: string): InlineKaTeX => ({
type: 'INLINE_KATEX',
value: content,
});

export const phoneChecker = (text: string, number: string) => {
if (number.length < 5) {
return plain(text);
}

return link(`tel:${number}`, plain(text));
};
23 changes: 23 additions & 0 deletions packages/message-parser/tests/phoneNumber.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,29 @@ test.each([
'[**here**](+(075)63546725)',
[paragraph([link('tel:07563546725', bold([plain('here')]))])],
],
[
'[**here**](+(075)63546725)',
[paragraph([link('tel:07563546725', bold([plain('here')]))])],
],
[
'+(11)99999-9999',
[paragraph([link('tel:11999999999', plain('+(11)99999-9999'))])],
],
['5 +51231', [paragraph([plain('5 '), link('tel:51231', plain('+51231'))])]],
[
'5 +51231 5',
[paragraph([plain('5 '), link('tel:51231', plain('+51231')), plain(' 5')])],
],
['+(12)3-45', [paragraph([link('tel:12345', plain('+(12)3-45'))])]],
['+1.599123', [paragraph([plain('+1.599123')])]],
['1+1=2', [paragraph([plain('1+1=2')])]],
['1+1=2 text', [paragraph([plain('1+1=2 text')])]],
['+1000,00', [paragraph([plain('+1000,00')])]],
['+ 1199999999', [paragraph([plain('+ 1199999999')])]],
['+1234', [paragraph([plain('+1234')])]],
['+(12)3-4', [paragraph([plain('+(12)3-4')])]],
['+123-4', [paragraph([plain('+123-4')])]],
['5+51231', [paragraph([plain('5+51231')])]],
])('parses %p', (input, output) => {
expect(parse(input)).toMatchObject(output);
});

0 comments on commit 6062700

Please sign in to comment.