15815213711
2024-08-26 67b8b6731811983447e053d4396b3708c14dfe3c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// This is a legacy function.
// Use `findNumbers()` instead.
 
import findNumbers, { searchPhoneNumbers } from './findPhoneNumbers.js'
import { PhoneNumberSearch } from './findPhoneNumbersInitialImplementation.js'
import metadata from '../../metadata.min.json' assert { type: 'json' }
 
describe('findPhoneNumbers', () => {
    it('should find numbers', () => {
        findNumbers('2133734253', 'US', metadata).should.deep.equal([{
            phone    : '2133734253',
            country  : 'US',
            startsAt : 0,
            endsAt   : 10
        }])
 
        findNumbers('(213) 373-4253', 'US', metadata).should.deep.equal([{
            phone    : '2133734253',
            country  : 'US',
            startsAt : 0,
            endsAt   : 14
        }])
 
        findNumbers('The number is +7 (800) 555-35-35 and not (213) 373-4253 as written in the document.', 'US', metadata).should.deep.equal([{
            phone    : '8005553535',
            country  : 'RU',
            startsAt : 14,
            endsAt   : 32
        }, {
            phone    : '2133734253',
            country  : 'US',
            startsAt : 41,
            endsAt   : 55
        }])
 
        // Opening parenthesis issue.
        // https://github.com/catamphetamine/libphonenumber-js/issues/252
        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([{
            phone    : '8005553535',
            country  : 'RU',
            startsAt : 14,
            endsAt   : 32
        }, {
            phone    : '2133734253',
            country  : 'US',
            startsAt : 41,
            endsAt   : 55
        }])
 
        // No default country.
        findNumbers('The number is +7 (800) 555-35-35 as written in the document.', metadata).should.deep.equal([{
            phone    : '8005553535',
            country  : 'RU',
            startsAt : 14,
            endsAt   : 32
        }])
 
        // Passing `options` and default country.
        findNumbers('The number is +7 (800) 555-35-35 as written in the document.', 'US', { leniency: 'VALID' }, metadata).should.deep.equal([{
            phone    : '8005553535',
            country  : 'RU',
            startsAt : 14,
            endsAt   : 32
        }])
 
        // Passing `options`.
        findNumbers('The number is +7 (800) 555-35-35 as written in the document.', { leniency: 'VALID' }, metadata).should.deep.equal([{
            phone    : '8005553535',
            country  : 'RU',
            startsAt : 14,
            endsAt   : 32
        }])
 
        // Not a phone number and a phone number.
        findNumbers('Digits 12 are not a number, but +7 (800) 555-35-35 is.', { leniency: 'VALID' }, metadata).should.deep.equal([{
            phone    : '8005553535',
            country  : 'RU',
            startsAt : 32,
            endsAt   : 50
        }])
 
        // Phone number extension.
        findNumbers('Date 02/17/2018 is not a number, but +7 (800) 555-35-35 ext. 123 is.', { leniency: 'VALID' }, metadata).should.deep.equal([{
            phone    : '8005553535',
            country  : 'RU',
            ext      : '123',
            startsAt : 37,
            endsAt   : 64
        }])
    })
 
    it('shouldn\'t find non-valid numbers', () => {
        // Not a valid phone number for US.
        findNumbers('1111111111', 'US', metadata).should.deep.equal([])
    })
 
    it('should find non-European digits', () => {
        // E.g. in Iraq they don't write `+442323234` but rather `+٤٤٢٣٢٣٢٣٤`.
        findNumbers('العَرَبِيَّة‎ +٤٤٣٣٣٣٣٣٣٣٣٣عَرَبِيّ‎', metadata).should.deep.equal([{
            country  : 'GB',
            phone    : '3333333333',
            startsAt : 14,
            endsAt   : 27
        }])
    })
 
    it('should iterate', () => {
        const expected_numbers = [{
            country : 'RU',
            phone   : '8005553535',
            // number   : '+7 (800) 555-35-35',
            startsAt : 14,
            endsAt   : 32
        }, {
            country : 'US',
            phone   : '2133734253',
            // number   : '(213) 373-4253',
            startsAt : 41,
            endsAt   : 55
        }]
 
        for (const number of searchPhoneNumbers('The number is +7 (800) 555-35-35 and not (213) 373-4253 as written in the document.', 'US', metadata)) {
            number.should.deep.equal(expected_numbers.shift())
        }
 
        expected_numbers.length.should.equal(0)
    })
 
    it('should work in edge cases', () => {
        let thrower
 
        // No input
        findNumbers('', metadata).should.deep.equal([])
 
        // No country metadata for this `require` country code
        thrower = () => findNumbers('123', 'ZZ', metadata)
        thrower.should.throw('Unknown country')
 
        // Numerical `value`
        thrower = () => findNumbers(2141111111, 'US')
        thrower.should.throw('A text for parsing must be a string.')
 
        // // No metadata
        // thrower = () => findNumbers('')
        // thrower.should.throw('`metadata` argument not passed')
    })
 
    it('shouldn\'t find phone numbers which are not phone numbers', () => {
        // A timestamp.
        findNumbers('2012-01-02 08:00', 'US', metadata).should.deep.equal([])
 
        // A valid number (not a complete timestamp).
        findNumbers('2012-01-02 08', 'US', metadata).should.deep.equal([{
            country  : 'US',
            phone    : '2012010208',
            startsAt : 0,
            endsAt   : 13
        }])
 
        // Invalid parens.
        findNumbers('213(3734253', 'US', metadata).should.deep.equal([])
 
        // Letters after phone number.
        findNumbers('2133734253a', 'US', metadata).should.deep.equal([])
 
        // Valid phone (same as the one found in the UUID below).
        findNumbers('The phone number is 231354125.', 'FR', metadata).should.deep.equal([{
            country  : 'FR',
            phone    : '231354125',
            startsAt : 20,
            endsAt   : 29
        }])
 
        // Not a phone number (part of a UUID).
        // Should parse in `{ extended: true }` mode.
        const possibleNumbers = findNumbers('The UUID is CA801c26f98cd16e231354125ad046e40b.', 'FR', { extended: true }, metadata)
        possibleNumbers.length.should.equal(3)
        possibleNumbers[1].country.should.equal('FR')
        possibleNumbers[1].phone.should.equal('231354125')
 
        // Not a phone number (part of a UUID).
        // Shouldn't parse by default.
        findNumbers('The UUID is CA801c26f98cd16e231354125ad046e40b.', 'FR', metadata).should.deep.equal([])
    })
})
 
describe('PhoneNumberSearch', () => {
    it('should search for phone numbers', () => {
        const finder = new PhoneNumberSearch('The number is +7 (800) 555-35-35 and not (213) 373-4253 as written in the document.', { defaultCountry: 'US' }, metadata)
 
        finder.hasNext().should.equal(true)
        finder.next().should.deep.equal({
            country : 'RU',
            phone   : '8005553535',
            // number   : '+7 (800) 555-35-35',
            startsAt : 14,
            endsAt   : 32
        })
 
        finder.hasNext().should.equal(true)
        finder.next().should.deep.equal({
            country : 'US',
            phone   : '2133734253',
            // number   : '(213) 373-4253',
            startsAt : 41,
            endsAt   : 55
        })
 
        finder.hasNext().should.equal(false)
    })
 
    it('should search for phone numbers (no options)', () => {
        const finder = new PhoneNumberSearch('The number is +7 (800) 555-35-35', undefined, metadata)
        finder.hasNext().should.equal(true)
        finder.next().should.deep.equal({
            country : 'RU',
            phone   : '8005553535',
            // number   : '+7 (800) 555-35-35',
            startsAt : 14,
            endsAt   : 32
        })
        finder.hasNext().should.equal(false)
    })
 
    it('should work in edge cases', () => {
        // No options
        const search = new PhoneNumberSearch('', undefined, metadata)
 
        // No next element
        let thrower = () => search.next()
        thrower.should.throw('No next element')
    })
})