@verdocs/js-sdk 3.8.4 → 3.8.5
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.
- package/Utils/Strings.d.ts +5 -0
- package/Utils/Strings.js +24 -0
- package/package.json +1 -1
package/Utils/Strings.d.ts
CHANGED
|
@@ -2,3 +2,8 @@
|
|
|
2
2
|
* Capitalize the first letter of a string.
|
|
3
3
|
*/
|
|
4
4
|
export declare const capitalize: (str: string) => string;
|
|
5
|
+
/**
|
|
6
|
+
* Convert a phone-number-like string to E164 format.
|
|
7
|
+
* @see https://46elks.com/kb/e164
|
|
8
|
+
*/
|
|
9
|
+
export declare const convertToE164: (input: string) => string;
|
package/Utils/Strings.js
CHANGED
|
@@ -2,3 +2,27 @@
|
|
|
2
2
|
* Capitalize the first letter of a string.
|
|
3
3
|
*/
|
|
4
4
|
export var capitalize = function (str) { return str.charAt(0).toUpperCase() + str.slice(1); };
|
|
5
|
+
/**
|
|
6
|
+
* Convert a phone-number-like string to E164 format.
|
|
7
|
+
* @see https://46elks.com/kb/e164
|
|
8
|
+
*/
|
|
9
|
+
export var convertToE164 = function (input) {
|
|
10
|
+
// "(212) 555-1212" => +12125551212
|
|
11
|
+
// "+46766861004" => "+46766861004"
|
|
12
|
+
// "212-555-1212" => +12125551212
|
|
13
|
+
// "212.555.1212" => +12125551212
|
|
14
|
+
// "212 555 1212" => +12125551212
|
|
15
|
+
var temp = (input || '').trim();
|
|
16
|
+
// If we are already prefixed, assume the user did it deliberately and attempt to use what they entered. We also short-circuit blanks.
|
|
17
|
+
if (!temp || temp.startsWith('+')) {
|
|
18
|
+
return temp;
|
|
19
|
+
}
|
|
20
|
+
// Remove any spaces, parenthesis or other punctuation.
|
|
21
|
+
temp = temp.replace(/[^0-9]/g, '');
|
|
22
|
+
// If the number begins with a zero, remove the leading zero. Do not combine this with the previous step because it needs to be removed
|
|
23
|
+
// whether it's the actual first character e.g. `0(5)` or just the first digit e.g. `(05`.
|
|
24
|
+
temp = temp.replace(/^0/g, '');
|
|
25
|
+
// Prepend the country code and +. We're assuming US in this case given the target demographic. Users in other countries would/should be
|
|
26
|
+
// already entering a prefix so they'd shortcut out of this routine via the + prefix check.
|
|
27
|
+
return "+1".concat(temp);
|
|
28
|
+
};
|