@nr1e/commons 0.4.5 → 0.4.6
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/dist/business/index.d.mts +1 -0
- package/dist/business/index.d.mts.map +1 -1
- package/dist/business/index.mjs +1 -0
- package/dist/business/index.mjs.map +1 -1
- package/dist/business/phone-number.d.mts +44 -0
- package/dist/business/phone-number.d.mts.map +1 -0
- package/dist/business/phone-number.mjs +85 -0
- package/dist/business/phone-number.mjs.map +1 -0
- package/package.json +3 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../src/business/index.mts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,wBAAwB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../src/business/index.mts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,oBAAoB,CAAC;AACnC,cAAc,wBAAwB,CAAC"}
|
package/dist/business/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sourceRoot":"","sources":["../../src/business/index.mts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,wBAAwB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.mjs","sourceRoot":"","sources":["../../src/business/index.mts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,oBAAoB,CAAC;AACnC,cAAc,wBAAwB,CAAC"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import * as v from 'valibot';
|
|
2
|
+
import { parsePhoneNumberFromString, CountryCode } from 'libphonenumber-js';
|
|
3
|
+
export { parsePhoneNumberFromString, CountryCode };
|
|
4
|
+
/**
|
|
5
|
+
* Checks if a phone number is valid according to E.164 format.
|
|
6
|
+
*
|
|
7
|
+
* @param phoneNumber - The phone number to check.
|
|
8
|
+
*/
|
|
9
|
+
export declare function isValidE164(phoneNumber: string): boolean;
|
|
10
|
+
/**
|
|
11
|
+
* Checks if a phone number is valid according to the specified country code.
|
|
12
|
+
*
|
|
13
|
+
* @param phoneNumber - The phone number to check.
|
|
14
|
+
* @param countryCode - The 2-letter country code (ISO 3166-1 alpha-2).
|
|
15
|
+
*/
|
|
16
|
+
export declare function isValidPhoneNumber(phoneNumber: string, countryCode: CountryCode): boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Converts a phone number to E.164 format.
|
|
19
|
+
*
|
|
20
|
+
* @param phoneNumber
|
|
21
|
+
* @param defaultCountry
|
|
22
|
+
*/
|
|
23
|
+
export declare function toE164(phoneNumber: string, defaultCountry?: CountryCode): string | null;
|
|
24
|
+
/**
|
|
25
|
+
* Generates a valibot schema to validate a phone number given a country code.
|
|
26
|
+
*
|
|
27
|
+
* @param countryCode - The 2-letter country code (ISO 3166-1 alpha-2).
|
|
28
|
+
* @param msg - The error message to use if the phone number is invalid.
|
|
29
|
+
*/
|
|
30
|
+
export declare function phoneNumberSchema(countryCode: CountryCode, msg?: string): v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.CheckAction<string, string>]>;
|
|
31
|
+
/**
|
|
32
|
+
* Generates a valibot schema to validate a phone number in E.164 format.
|
|
33
|
+
*
|
|
34
|
+
* @param msg - The error message to use if the phone number is invalid.
|
|
35
|
+
*/
|
|
36
|
+
export declare function phoneNumberE164Schema(msg?: string): v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.CheckAction<string, string>]>;
|
|
37
|
+
/**
|
|
38
|
+
* Generates a valibot schema to validate a phone number and transform it into E.164 format
|
|
39
|
+
*
|
|
40
|
+
* @param countryCode - The 2-letter country code (ISO 3166-1 alpha-2).
|
|
41
|
+
* @param msg - The error message to use if the phone number is invalid.
|
|
42
|
+
*/
|
|
43
|
+
export declare function phoneNumberToE164Schema(countryCode?: CountryCode, msg?: string): v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.TrimAction, v.TransformAction<string, string | null>, v.CheckAction<string | null, string>]>;
|
|
44
|
+
//# sourceMappingURL=phone-number.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"phone-number.d.mts","sourceRoot":"","sources":["../../src/business/phone-number.mts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,SAAS,CAAC;AAC7B,OAAO,EAAC,0BAA0B,EAAE,WAAW,EAAC,MAAM,mBAAmB,CAAC;AAE1E,OAAO,EAAC,0BAA0B,EAAE,WAAW,EAAC,CAAC;AAEjD;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,WAAW,EAAE,MAAM,GAAG,OAAO,CAOxD;AAED;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAChC,WAAW,EAAE,MAAM,EACnB,WAAW,EAAE,WAAW,WAMzB;AAED;;;;;GAKG;AACH,wBAAgB,MAAM,CACpB,WAAW,EAAE,MAAM,EACnB,cAAc,CAAC,EAAE,WAAW,GAC3B,MAAM,GAAG,IAAI,CAQf;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,WAAW,EAAE,WAAW,EAAE,GAAG,CAAC,EAAE,MAAM,yFAUvE;AAED;;;;GAIG;AACH,wBAAgB,qBAAqB,CAAC,GAAG,CAAC,EAAE,MAAM,yFASjD;AAED;;;;;GAKG;AACH,wBAAgB,uBAAuB,CACrC,WAAW,CAAC,EAAE,WAAW,EACzB,GAAG,CAAC,EAAE,MAAM,wJAab"}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import * as v from 'valibot';
|
|
2
|
+
import { parsePhoneNumberFromString } from 'libphonenumber-js';
|
|
3
|
+
export { parsePhoneNumberFromString };
|
|
4
|
+
/**
|
|
5
|
+
* Checks if a phone number is valid according to E.164 format.
|
|
6
|
+
*
|
|
7
|
+
* @param phoneNumber - The phone number to check.
|
|
8
|
+
*/
|
|
9
|
+
export function isValidE164(phoneNumber) {
|
|
10
|
+
// Basic E.164 format check
|
|
11
|
+
if (!/^\+[1-9]\d{1,14}$/.test(phoneNumber)) {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
const parsed = parsePhoneNumberFromString(phoneNumber);
|
|
15
|
+
return parsed?.isValid() ?? false;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Checks if a phone number is valid according to the specified country code.
|
|
19
|
+
*
|
|
20
|
+
* @param phoneNumber - The phone number to check.
|
|
21
|
+
* @param countryCode - The 2-letter country code (ISO 3166-1 alpha-2).
|
|
22
|
+
*/
|
|
23
|
+
export function isValidPhoneNumber(phoneNumber, countryCode) {
|
|
24
|
+
const parsed = parsePhoneNumberFromString(phoneNumber, {
|
|
25
|
+
defaultCountry: countryCode,
|
|
26
|
+
});
|
|
27
|
+
return parsed?.isValid() ?? false;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Converts a phone number to E.164 format.
|
|
31
|
+
*
|
|
32
|
+
* @param phoneNumber
|
|
33
|
+
* @param defaultCountry
|
|
34
|
+
*/
|
|
35
|
+
export function toE164(phoneNumber, defaultCountry) {
|
|
36
|
+
const phone = parsePhoneNumberFromString(phoneNumber, defaultCountry);
|
|
37
|
+
if (!phone || !phone.isValid()) {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
return phone.number; // already E.164
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Generates a valibot schema to validate a phone number given a country code.
|
|
44
|
+
*
|
|
45
|
+
* @param countryCode - The 2-letter country code (ISO 3166-1 alpha-2).
|
|
46
|
+
* @param msg - The error message to use if the phone number is invalid.
|
|
47
|
+
*/
|
|
48
|
+
export function phoneNumberSchema(countryCode, msg) {
|
|
49
|
+
return v.pipe(v.string(), v.check((phoneNumber) => {
|
|
50
|
+
const parsed = parsePhoneNumberFromString(phoneNumber, {
|
|
51
|
+
defaultCountry: countryCode,
|
|
52
|
+
});
|
|
53
|
+
return parsed?.isValid() ?? false;
|
|
54
|
+
}, msg ?? 'Invalid phone number'));
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Generates a valibot schema to validate a phone number in E.164 format.
|
|
58
|
+
*
|
|
59
|
+
* @param msg - The error message to use if the phone number is invalid.
|
|
60
|
+
*/
|
|
61
|
+
export function phoneNumberE164Schema(msg) {
|
|
62
|
+
return v.pipe(v.string(), v.check((phoneNumber) => {
|
|
63
|
+
if (!isValidE164(phoneNumber))
|
|
64
|
+
return false;
|
|
65
|
+
const parsed = parsePhoneNumberFromString(phoneNumber);
|
|
66
|
+
return parsed?.isValid() ?? false;
|
|
67
|
+
}, msg ?? 'Invalid phone number'));
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Generates a valibot schema to validate a phone number and transform it into E.164 format
|
|
71
|
+
*
|
|
72
|
+
* @param countryCode - The 2-letter country code (ISO 3166-1 alpha-2).
|
|
73
|
+
* @param msg - The error message to use if the phone number is invalid.
|
|
74
|
+
*/
|
|
75
|
+
export function phoneNumberToE164Schema(countryCode, msg) {
|
|
76
|
+
return v.pipe(v.string(), v.trim(), v.transform((input) => toE164(input, countryCode)), v.check((phoneNumber) => {
|
|
77
|
+
if (!phoneNumber)
|
|
78
|
+
return false;
|
|
79
|
+
if (!isValidE164(phoneNumber))
|
|
80
|
+
return false;
|
|
81
|
+
const parsed = parsePhoneNumberFromString(phoneNumber);
|
|
82
|
+
return parsed?.isValid() ?? false;
|
|
83
|
+
}, msg ?? 'Invalid phone number'));
|
|
84
|
+
}
|
|
85
|
+
//# sourceMappingURL=phone-number.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"phone-number.mjs","sourceRoot":"","sources":["../../src/business/phone-number.mts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,SAAS,CAAC;AAC7B,OAAO,EAAC,0BAA0B,EAAc,MAAM,mBAAmB,CAAC;AAE1E,OAAO,EAAC,0BAA0B,EAAc,CAAC;AAEjD;;;;GAIG;AACH,MAAM,UAAU,WAAW,CAA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ChD,OAAO,CAAC,CAAC,IAAI,CACX,CAAC,CAAC,MAAM,EAAE,EACV,CAAC,CAAC,KAAK,CAAC,CAAC,WAAW,EAAE,EAAE;QACtB,IAAI,CAAC,WAAW,CAAC,WAAW,CAAC;YAAE,OAAO,KAAK,CAAC;QAC5C,MAAM,MAAM,GAAG,0BAA0B,CAAC,WAAW,CAAC,CAAC;QACvD,OAAO,MAAM,EAAE,OAAO,EAAE,IAAI,KAAK,CAAC;IACpC,CAAC,EAAE,GAAG,IAAI,sBAAsB,CAAC,CAClC,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,uBAAuB,CACrC,WAAyB,EACzB,GAAY;IAEZ,OAAO,CAAC,CAAC,IAAI,CACX,CAAC,CAAC,MAAM,EAAE,EACV,CAAC,CAAC,IAAI,EAAE,EACR,CAAC,CAAC,SAAS,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,MAAM,CAAC,KAAK,EAAE,WAAW,CAAC,CAAC,EAClD,CAAC,CAAC,KAAK,CAAC,CAAC,WAAW,EAAE,EAAE;QACtB,IAAI,CAAC,WAAW;YAAE,OAAO,KAAK,CAAC;QAC/B,IAAI,CAAC,WAAW,CAAC,WAAW,CAAC;YAAE,OAAO,KAAK,CAAC;QAC5C,MAAM,MAAM,GAAG,0BAA0B,CAAC,WAAW,CAAC,CAAC;QACvD,OAAO,MAAM,EAAE,OAAO,EAAE,IAAI,KAAK,CAAC;IACpC,CAAC,EAAE,GAAG,IAAI,sBAAsB,CAAC,CAClC,CAAC;AACJ,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nr1e/commons",
|
|
3
3
|
"description": "Common utilities for TypeScript projects",
|
|
4
|
-
"version": "0.4.
|
|
4
|
+
"version": "0.4.6",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"author": "NR1E, Inc.",
|
|
7
7
|
"publishConfig": {
|
|
@@ -19,9 +19,11 @@
|
|
|
19
19
|
"@eslint/js": "^9.39.3",
|
|
20
20
|
"@types/node": "^24.10.13",
|
|
21
21
|
"eslint": "9.39.3",
|
|
22
|
+
"libphonenumber-js": "^1.12.26",
|
|
22
23
|
"prettier": "3.8.1",
|
|
23
24
|
"typescript": "5.9.3",
|
|
24
25
|
"typescript-eslint": "8.56.1",
|
|
26
|
+
"valibot": "^1.2.0",
|
|
25
27
|
"vitest": "4.0.18"
|
|
26
28
|
},
|
|
27
29
|
"dependencies": {
|