@clipboard-health/phone-number 0.2.1 → 0.3.0
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/package.json +4 -4
- package/src/index.d.ts +1 -0
- package/src/index.js +1 -0
- package/src/index.js.map +1 -1
- package/src/lib/formatPhoneNumber.d.ts +44 -3
- package/src/lib/formatPhoneNumber.js +49 -2
- package/src/lib/formatPhoneNumber.js.map +1 -1
- package/src/lib/isValidPhoneNumber.d.ts +3 -0
- package/src/lib/isValidPhoneNumber.js +11 -0
- package/src/lib/isValidPhoneNumber.js.map +1 -0
- package/src/lib/types.d.ts +3 -0
- package/src/lib/types.js +3 -0
- package/src/lib/types.js.map +1 -0
package/package.json
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clipboard-health/phone-number",
|
|
3
3
|
"description": "Phone number utility functions.",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.3.0",
|
|
5
5
|
"bugs": "https://github.com/ClipboardHealth/core-utils/issues",
|
|
6
6
|
"dependencies": {
|
|
7
|
-
"@clipboard-health/util-ts": "3.12.
|
|
7
|
+
"@clipboard-health/util-ts": "3.12.2",
|
|
8
8
|
"tslib": "2.8.1"
|
|
9
9
|
},
|
|
10
10
|
"devDependencies": {
|
|
11
|
-
"@clipboard-health/testing-core": "0.21.
|
|
12
|
-
"libphonenumber-js": "1.12.
|
|
11
|
+
"@clipboard-health/testing-core": "0.21.2",
|
|
12
|
+
"libphonenumber-js": "1.12.23"
|
|
13
13
|
},
|
|
14
14
|
"keywords": [],
|
|
15
15
|
"license": "MIT",
|
package/src/index.d.ts
CHANGED
package/src/index.js
CHANGED
|
@@ -2,4 +2,5 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const tslib_1 = require("tslib");
|
|
4
4
|
tslib_1.__exportStar(require("./lib/formatPhoneNumber"), exports);
|
|
5
|
+
tslib_1.__exportStar(require("./lib/isValidPhoneNumber"), exports);
|
|
5
6
|
//# sourceMappingURL=index.js.map
|
package/src/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../packages/phone-number/src/index.ts"],"names":[],"mappings":";;;AAAA,kEAAwC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../packages/phone-number/src/index.ts"],"names":[],"mappings":";;;AAAA,kEAAwC;AACxC,mEAAyC"}
|
|
@@ -1,5 +1,46 @@
|
|
|
1
1
|
import { type ServiceResult } from "@clipboard-health/util-ts";
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
import { type WithPhoneNumber } from "./types";
|
|
3
|
+
export interface FormatPhoneNumberParams extends WithPhoneNumber {
|
|
4
4
|
format: "E.164" | "humanReadable";
|
|
5
|
-
}
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Formats a phone number to the specified format.
|
|
8
|
+
*
|
|
9
|
+
* @param params - The formatting parameters
|
|
10
|
+
* @param params.phoneNumber - The phone number to format
|
|
11
|
+
* @param params.format - The desired output format ("E.164" for international format or "humanReadable" for national format)
|
|
12
|
+
* @returns A ServiceResult containing the formatted phone number or an error
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* const result = formatPhoneNumber({ phoneNumber: "(555) 123-4567", format: "E.164" });
|
|
17
|
+
* if (isSuccess(result)) {
|
|
18
|
+
* console.log(result.value); // "+15551234567"
|
|
19
|
+
* }
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export declare function formatPhoneNumber(params: FormatPhoneNumberParams): ServiceResult<string>;
|
|
23
|
+
/**
|
|
24
|
+
* Formats a phone number to the specified format, throwing an error if formatting fails.
|
|
25
|
+
*
|
|
26
|
+
* This is a convenience function that wraps `formatPhoneNumber` and throws an error
|
|
27
|
+
* instead of returning a ServiceResult. Use this when you want to handle errors via
|
|
28
|
+
* exception handling rather than explicit error checking.
|
|
29
|
+
*
|
|
30
|
+
* @param params - The formatting parameters
|
|
31
|
+
* @param params.phoneNumber - The phone number to format
|
|
32
|
+
* @param params.format - The desired output format ("E.164" for international format or "humanReadable" for national format)
|
|
33
|
+
* @returns The formatted phone number
|
|
34
|
+
* @throws Error when the phone number cannot be formatted (invalid format, missing country code, etc.)
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```ts
|
|
38
|
+
* try {
|
|
39
|
+
* const formatted = formatPhoneNumberOrThrow({ phoneNumber: "(555) 123-4567", format: "E.164" });
|
|
40
|
+
* console.log(formatted); // "+15551234567"
|
|
41
|
+
* } catch (error) {
|
|
42
|
+
* console.error("Invalid phone number:", error.message);
|
|
43
|
+
* }
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
export declare function formatPhoneNumberOrThrow(params: FormatPhoneNumberParams): string;
|
|
@@ -1,8 +1,25 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.formatPhoneNumber = formatPhoneNumber;
|
|
4
|
+
exports.formatPhoneNumberOrThrow = formatPhoneNumberOrThrow;
|
|
4
5
|
const util_ts_1 = require("@clipboard-health/util-ts");
|
|
5
6
|
const libphonenumber_js_1 = require("libphonenumber-js");
|
|
7
|
+
/**
|
|
8
|
+
* Formats a phone number to the specified format.
|
|
9
|
+
*
|
|
10
|
+
* @param params - The formatting parameters
|
|
11
|
+
* @param params.phoneNumber - The phone number to format
|
|
12
|
+
* @param params.format - The desired output format ("E.164" for international format or "humanReadable" for national format)
|
|
13
|
+
* @returns A ServiceResult containing the formatted phone number or an error
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* const result = formatPhoneNumber({ phoneNumber: "(555) 123-4567", format: "E.164" });
|
|
18
|
+
* if (isSuccess(result)) {
|
|
19
|
+
* console.log(result.value); // "+15551234567"
|
|
20
|
+
* }
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
6
23
|
function formatPhoneNumber(params) {
|
|
7
24
|
const { phoneNumber, format } = params;
|
|
8
25
|
try {
|
|
@@ -11,8 +28,38 @@ function formatPhoneNumber(params) {
|
|
|
11
28
|
});
|
|
12
29
|
return (0, util_ts_1.success)(parsedPhoneNumber.format(format === "E.164" ? "E.164" : "NATIONAL"));
|
|
13
30
|
}
|
|
14
|
-
catch {
|
|
15
|
-
return (0, util_ts_1.failure)({ issues: [{ message:
|
|
31
|
+
catch (error) {
|
|
32
|
+
return (0, util_ts_1.failure)({ issues: [{ message: (0, util_ts_1.toError)(error).message, code: "INVALID_PHONE_NUMBER" }] });
|
|
16
33
|
}
|
|
17
34
|
}
|
|
35
|
+
/**
|
|
36
|
+
* Formats a phone number to the specified format, throwing an error if formatting fails.
|
|
37
|
+
*
|
|
38
|
+
* This is a convenience function that wraps `formatPhoneNumber` and throws an error
|
|
39
|
+
* instead of returning a ServiceResult. Use this when you want to handle errors via
|
|
40
|
+
* exception handling rather than explicit error checking.
|
|
41
|
+
*
|
|
42
|
+
* @param params - The formatting parameters
|
|
43
|
+
* @param params.phoneNumber - The phone number to format
|
|
44
|
+
* @param params.format - The desired output format ("E.164" for international format or "humanReadable" for national format)
|
|
45
|
+
* @returns The formatted phone number
|
|
46
|
+
* @throws Error when the phone number cannot be formatted (invalid format, missing country code, etc.)
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```ts
|
|
50
|
+
* try {
|
|
51
|
+
* const formatted = formatPhoneNumberOrThrow({ phoneNumber: "(555) 123-4567", format: "E.164" });
|
|
52
|
+
* console.log(formatted); // "+15551234567"
|
|
53
|
+
* } catch (error) {
|
|
54
|
+
* console.error("Invalid phone number:", error.message);
|
|
55
|
+
* }
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
function formatPhoneNumberOrThrow(params) {
|
|
59
|
+
const result = formatPhoneNumber(params);
|
|
60
|
+
if ((0, util_ts_1.isFailure)(result)) {
|
|
61
|
+
throw result.error;
|
|
62
|
+
}
|
|
63
|
+
return result.value;
|
|
64
|
+
}
|
|
18
65
|
//# sourceMappingURL=formatPhoneNumber.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"formatPhoneNumber.js","sourceRoot":"","sources":["../../../../../packages/phone-number/src/lib/formatPhoneNumber.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"formatPhoneNumber.js","sourceRoot":"","sources":["../../../../../packages/phone-number/src/lib/formatPhoneNumber.ts"],"names":[],"mappings":";;AA+BA,8CAWC;AAyBD,4DAOC;AA1ED,uDAMmC;AACnC,yDAA8D;AAQ9D;;;;;;;;;;;;;;;GAeG;AACH,SAAgB,iBAAiB,CAAC,MAA+B;IAC/D,MAAM,EAAE,WAAW,EAAE,MAAM,EAAE,GAAG,MAAM,CAAC;IAEvC,IAAI,CAAC;QACH,MAAM,iBAAiB,GAAG,IAAA,6CAAyB,EAAC,WAAW,CAAC,IAAI,EAAE,EAAE;YACtE,cAAc,EAAE,IAAI;SACrB,CAAC,CAAC;QACH,OAAO,IAAA,iBAAO,EAAC,iBAAiB,CAAC,MAAM,CAAC,MAAM,KAAK,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC;IACtF,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,IAAA,iBAAO,EAAC,EAAE,MAAM,EAAE,CAAC,EAAE,OAAO,EAAE,IAAA,iBAAO,EAAC,KAAK,CAAC,CAAC,OAAO,EAAE,IAAI,EAAE,sBAAsB,EAAE,CAAC,EAAE,CAAC,CAAC;IAClG,CAAC;AACH,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,SAAgB,wBAAwB,CAAC,MAA+B;IACtE,MAAM,MAAM,GAAG,iBAAiB,CAAC,MAAM,CAAC,CAAC;IACzC,IAAI,IAAA,mBAAS,EAAC,MAAM,CAAC,EAAE,CAAC;QACtB,MAAM,MAAM,CAAC,KAAK,CAAC;IACrB,CAAC;IAED,OAAO,MAAM,CAAC,KAAK,CAAC;AACtB,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isValidPhoneNumber = isValidPhoneNumber;
|
|
4
|
+
const libphonenumber_js_1 = require("libphonenumber-js");
|
|
5
|
+
function isValidPhoneNumber(params) {
|
|
6
|
+
const { phoneNumber } = params;
|
|
7
|
+
return (0, libphonenumber_js_1.isValidPhoneNumber)(phoneNumber.trim(), {
|
|
8
|
+
defaultCountry: "US",
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=isValidPhoneNumber.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"isValidPhoneNumber.js","sourceRoot":"","sources":["../../../../../packages/phone-number/src/lib/isValidPhoneNumber.ts"],"names":[],"mappings":";;AAMA,gDAMC;AAZD,yDAA8E;AAM9E,SAAgB,kBAAkB,CAAC,MAAgC;IACjE,MAAM,EAAE,WAAW,EAAE,GAAG,MAAM,CAAC;IAE/B,OAAO,IAAA,sCAAmB,EAAC,WAAW,CAAC,IAAI,EAAE,EAAE;QAC7C,cAAc,EAAE,IAAI;KACrB,CAAC,CAAC;AACL,CAAC"}
|
package/src/lib/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../../../packages/phone-number/src/lib/types.ts"],"names":[],"mappings":""}
|