tripstorekrabi-shared 1.17.6 → 1.17.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +2 -1
  2. package/utils.js +22 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tripstorekrabi-shared",
3
- "version": "1.17.6",
3
+ "version": "1.17.7",
4
4
  "description": "Shared library for Trip Store Krabi",
5
5
  "author": "Stefan Sommer <tonprince@gmail.com>",
6
6
  "repository": {
@@ -16,6 +16,7 @@
16
16
  },
17
17
  "dependencies": {
18
18
  "dayjs": "^1.11.11",
19
+ "libphonenumber-js": "^1.11.7",
19
20
  "nanoid": "^5.0.7"
20
21
  },
21
22
  "scripts": {
package/utils.js CHANGED
@@ -6,6 +6,7 @@ import utc from 'dayjs/plugin/utc.js';
6
6
  import timezone from 'dayjs/plugin/timezone.js';
7
7
  import { findEndPoint, findStartingPoint } from './price.js';
8
8
  import { CUSTOMER_NOTE } from './booking.js';
9
+ import { parsePhoneNumber, AsYouType } from 'libphonenumber-js';
9
10
 
10
11
  dayjs.extend(customParseFormat);
11
12
  dayjs.extend(advancedFormat);
@@ -749,3 +750,24 @@ export function isJSON(text) {
749
750
  .replace(/(?:^|:|,)(?:\s*\[)+/g, '')
750
751
  );
751
752
  }
753
+
754
+ export function formatPhoneNumber(phoneNumber) {
755
+ if (phoneNumber) {
756
+ try {
757
+ let libPhoneNumber = parsePhoneNumber(phoneNumber, 'TH');
758
+ let asYouType = new AsYouType();
759
+ asYouType.input(phoneNumber);
760
+
761
+ if (asYouType.isInternational() && asYouType.getCountry() !== 'TH') {
762
+ return libPhoneNumber.formatInternational();
763
+ } else {
764
+ return libPhoneNumber.formatNational();
765
+ }
766
+ } catch (e) {
767
+ console.error(e);
768
+ return phoneNumber;
769
+ }
770
+ } else {
771
+ return phoneNumber;
772
+ }
773
+ }