Skip to content

Commit ee312df

Browse files
committed
Fixed dialablePhoneNumber
1 parent 033106b commit ee312df

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

lib/services/dialable-phone-number.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11

22
const alterItems = require('./alter-items');
33

4-
module.exports = function dialablePhoneNumber (libphonenumberJs, defaultCountry = 'US', phoneField = 'phone', dailableField = 'dialablePhone') {
5-
return alterItems((item, context) => {
6-
if (typeof item[phoneField] === 'string') {
7-
const code = typeof defaultCountry === 'function' ? defaultCountry(item, context) : defaultCountry;
4+
module.exports = function dialablePhoneNumber (libphonenumberJs,
5+
defaultCountry = 'US', phoneField = 'phone', dialableField = 'dialablePhone', countryField = 'country'
6+
) {
87

9-
item[dailableField] = libphonenumberJs.parsePhoneNumber(item[phoneField], code);
8+
return context => alterItems(rec => {
9+
const phone = rec[phoneField];
10+
11+
if (typeof phone === 'string') {
12+
const parsedPhoneNumber = libphonenumberJs.parsePhoneNumber(phone, defaultCountry);
13+
14+
if (dialableField) {
15+
rec[dialableField] = parsedPhoneNumber.number;
16+
}
17+
18+
if (countryField) {
19+
rec[countryField] = parsedPhoneNumber.country;
20+
}
1021
}
11-
});
22+
})(context);
1223
};

0 commit comments

Comments
 (0)