1 |
|
2 |
|
3 | import findNumbers, { searchPhoneNumbers } from './findPhoneNumbers';
|
4 | import { PhoneNumberSearch } from './findPhoneNumbers_';
|
5 | import metadata from '../metadata.min.json';
|
6 | describe('findPhoneNumbers', function () {
|
7 | it('should find numbers', function () {
|
8 | findNumbers('2133734253', 'US', metadata).should.deep.equal([{
|
9 | phone: '2133734253',
|
10 | country: 'US',
|
11 | startsAt: 0,
|
12 | endsAt: 10
|
13 | }]);
|
14 | findNumbers('(213) 373-4253', 'US', metadata).should.deep.equal([{
|
15 | phone: '2133734253',
|
16 | country: 'US',
|
17 | startsAt: 0,
|
18 | endsAt: 14
|
19 | }]);
|
20 | findNumbers('The number is +7 (800) 555-35-35 and not (213) 373-4253 as written in the document.', 'US', metadata).should.deep.equal([{
|
21 | phone: '8005553535',
|
22 | country: 'RU',
|
23 | startsAt: 14,
|
24 | endsAt: 32
|
25 | }, {
|
26 | phone: '2133734253',
|
27 | country: 'US',
|
28 | startsAt: 41,
|
29 | endsAt: 55
|
30 | }]);
|
31 |
|
32 |
|
33 | findNumbers('The number is +7 (800) 555-35-35 and not (213) 373-4253 (that\'s not even in the same country!) as written in the document.', 'US', metadata).should.deep.equal([{
|
34 | phone: '8005553535',
|
35 | country: 'RU',
|
36 | startsAt: 14,
|
37 | endsAt: 32
|
38 | }, {
|
39 | phone: '2133734253',
|
40 | country: 'US',
|
41 | startsAt: 41,
|
42 | endsAt: 55
|
43 | }]);
|
44 |
|
45 | findNumbers('The number is +7 (800) 555-35-35 as written in the document.', metadata).should.deep.equal([{
|
46 | phone: '8005553535',
|
47 | country: 'RU',
|
48 | startsAt: 14,
|
49 | endsAt: 32
|
50 | }]);
|
51 |
|
52 | findNumbers('The number is +7 (800) 555-35-35 as written in the document.', 'US', {
|
53 | leniency: 'VALID'
|
54 | }, metadata).should.deep.equal([{
|
55 | phone: '8005553535',
|
56 | country: 'RU',
|
57 | startsAt: 14,
|
58 | endsAt: 32
|
59 | }]);
|
60 |
|
61 | findNumbers('The number is +7 (800) 555-35-35 as written in the document.', {
|
62 | leniency: 'VALID'
|
63 | }, metadata).should.deep.equal([{
|
64 | phone: '8005553535',
|
65 | country: 'RU',
|
66 | startsAt: 14,
|
67 | endsAt: 32
|
68 | }]);
|
69 |
|
70 | findNumbers('Digits 12 are not a number, but +7 (800) 555-35-35 is.', {
|
71 | leniency: 'VALID'
|
72 | }, metadata).should.deep.equal([{
|
73 | phone: '8005553535',
|
74 | country: 'RU',
|
75 | startsAt: 32,
|
76 | endsAt: 50
|
77 | }]);
|
78 |
|
79 | findNumbers('Date 02/17/2018 is not a number, but +7 (800) 555-35-35 ext. 123 is.', {
|
80 | leniency: 'VALID'
|
81 | }, metadata).should.deep.equal([{
|
82 | phone: '8005553535',
|
83 | country: 'RU',
|
84 | ext: '123',
|
85 | startsAt: 37,
|
86 | endsAt: 64
|
87 | }]);
|
88 | });
|
89 | it('shouldn\'t find non-valid numbers', function () {
|
90 |
|
91 | findNumbers('1111111111', 'US', metadata).should.deep.equal([]);
|
92 | });
|
93 | it('should find non-European digits', function () {
|
94 |
|
95 | findNumbers('العَرَبِيَّة +٤٤٣٣٣٣٣٣٣٣٣٣عَرَبِيّ', metadata).should.deep.equal([{
|
96 | country: 'GB',
|
97 | phone: '3333333333',
|
98 | startsAt: 14,
|
99 | endsAt: 27
|
100 | }]);
|
101 | });
|
102 | it('should iterate', function () {
|
103 | var expected_numbers = [{
|
104 | country: 'RU',
|
105 | phone: '8005553535',
|
106 |
|
107 | startsAt: 14,
|
108 | endsAt: 32
|
109 | }, {
|
110 | country: 'US',
|
111 | phone: '2133734253',
|
112 |
|
113 | startsAt: 41,
|
114 | endsAt: 55
|
115 | }];
|
116 |
|
117 | for (var _iterator = searchPhoneNumbers('The number is +7 (800) 555-35-35 and not (213) 373-4253 as written in the document.', 'US', metadata), _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
|
118 | var _ref;
|
119 |
|
120 | if (_isArray) {
|
121 | if (_i >= _iterator.length) break;
|
122 | _ref = _iterator[_i++];
|
123 | } else {
|
124 | _i = _iterator.next();
|
125 | if (_i.done) break;
|
126 | _ref = _i.value;
|
127 | }
|
128 |
|
129 | var number = _ref;
|
130 | number.should.deep.equal(expected_numbers.shift());
|
131 | }
|
132 |
|
133 | expected_numbers.length.should.equal(0);
|
134 | });
|
135 | it('should work in edge cases', function () {
|
136 | var thrower;
|
137 |
|
138 | findNumbers('', metadata).should.deep.equal([]);
|
139 |
|
140 | thrower = function thrower() {
|
141 | return findNumbers('123', 'ZZ', metadata);
|
142 | };
|
143 |
|
144 | thrower.should["throw"]('Unknown country');
|
145 |
|
146 | thrower = function thrower() {
|
147 | return findNumbers(2141111111, 'US');
|
148 | };
|
149 |
|
150 | thrower.should["throw"]('A text for parsing must be a string.');
|
151 |
|
152 |
|
153 | });
|
154 | it('shouldn\'t find phone numbers which are not phone numbers', function () {
|
155 |
|
156 | findNumbers('2012-01-02 08:00', 'US', metadata).should.deep.equal([]);
|
157 |
|
158 | findNumbers('2012-01-02 08', 'US', metadata).should.deep.equal([{
|
159 | country: 'US',
|
160 | phone: '2012010208',
|
161 | startsAt: 0,
|
162 | endsAt: 13
|
163 | }]);
|
164 |
|
165 | findNumbers('213(3734253', 'US', metadata).should.deep.equal([]);
|
166 |
|
167 | findNumbers('2133734253a', 'US', metadata).should.deep.equal([]);
|
168 |
|
169 | findNumbers('The phone number is 231354125.', 'FR', metadata).should.deep.equal([{
|
170 | country: 'FR',
|
171 | phone: '231354125',
|
172 | startsAt: 20,
|
173 | endsAt: 29
|
174 | }]);
|
175 |
|
176 |
|
177 | var possibleNumbers = findNumbers('The UUID is CA801c26f98cd16e231354125ad046e40b.', 'FR', {
|
178 | extended: true
|
179 | }, metadata);
|
180 | possibleNumbers.length.should.equal(3);
|
181 | possibleNumbers[1].country.should.equal('FR');
|
182 | possibleNumbers[1].phone.should.equal('231354125');
|
183 |
|
184 |
|
185 | findNumbers('The UUID is CA801c26f98cd16e231354125ad046e40b.', 'FR', metadata).should.deep.equal([]);
|
186 | });
|
187 | });
|
188 | describe('PhoneNumberSearch', function () {
|
189 | it('should search for phone numbers', function () {
|
190 | var finder = new PhoneNumberSearch('The number is +7 (800) 555-35-35 and not (213) 373-4253 as written in the document.', {
|
191 | defaultCountry: 'US'
|
192 | }, metadata);
|
193 | finder.hasNext().should.equal(true);
|
194 | finder.next().should.deep.equal({
|
195 | country: 'RU',
|
196 | phone: '8005553535',
|
197 |
|
198 | startsAt: 14,
|
199 | endsAt: 32
|
200 | });
|
201 | finder.hasNext().should.equal(true);
|
202 | finder.next().should.deep.equal({
|
203 | country: 'US',
|
204 | phone: '2133734253',
|
205 |
|
206 | startsAt: 41,
|
207 | endsAt: 55
|
208 | });
|
209 | finder.hasNext().should.equal(false);
|
210 | });
|
211 | it('should search for phone numbers (no options)', function () {
|
212 | var finder = new PhoneNumberSearch('The number is +7 (800) 555-35-35', undefined, metadata);
|
213 | finder.hasNext().should.equal(true);
|
214 | finder.next().should.deep.equal({
|
215 | country: 'RU',
|
216 | phone: '8005553535',
|
217 |
|
218 | startsAt: 14,
|
219 | endsAt: 32
|
220 | });
|
221 | finder.hasNext().should.equal(false);
|
222 | });
|
223 | it('should work in edge cases', function () {
|
224 |
|
225 | var search = new PhoneNumberSearch('', undefined, metadata);
|
226 |
|
227 | var thrower = function thrower() {
|
228 | return search.next();
|
229 | };
|
230 |
|
231 | thrower.should["throw"]('No next element');
|
232 | });
|
233 | });
|
234 |
|
\ | No newline at end of file |