@spudlabs/guardis 0.4.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/README.md +901 -0
- package/esm/mod.d.ts +55 -0
- package/esm/mod.d.ts.map +1 -0
- package/esm/mod.js +43 -0
- package/esm/package.json +3 -0
- package/esm/specs/standard-schema-spec.v1.d.ts +56 -0
- package/esm/specs/standard-schema-spec.v1.d.ts.map +1 -0
- package/esm/specs/standard-schema-spec.v1.js +1 -0
- package/esm/src/batch.d.ts +23 -0
- package/esm/src/batch.d.ts.map +1 -0
- package/esm/src/batch.js +23 -0
- package/esm/src/brand.d.ts +62 -0
- package/esm/src/brand.d.ts.map +1 -0
- package/esm/src/brand.js +9 -0
- package/esm/src/context.d.ts +19 -0
- package/esm/src/context.d.ts.map +1 -0
- package/esm/src/context.js +41 -0
- package/esm/src/extend.d.ts +23 -0
- package/esm/src/extend.d.ts.map +1 -0
- package/esm/src/extend.js +9 -0
- package/esm/src/guard.d.ts +288 -0
- package/esm/src/guard.d.ts.map +1 -0
- package/esm/src/guard.js +631 -0
- package/esm/src/helpers/http.helpers.d.ts +3 -0
- package/esm/src/helpers/http.helpers.d.ts.map +1 -0
- package/esm/src/helpers/http.helpers.js +2 -0
- package/esm/src/helpers/strings.helpers.d.ts +18 -0
- package/esm/src/helpers/strings.helpers.d.ts.map +1 -0
- package/esm/src/helpers/strings.helpers.js +47 -0
- package/esm/src/introspect.d.ts +36 -0
- package/esm/src/introspect.d.ts.map +1 -0
- package/esm/src/introspect.js +25 -0
- package/esm/src/modules/async.d.ts +27 -0
- package/esm/src/modules/async.d.ts.map +1 -0
- package/esm/src/modules/async.js +38 -0
- package/esm/src/modules/http.branded.d.ts +42 -0
- package/esm/src/modules/http.branded.d.ts.map +1 -0
- package/esm/src/modules/http.branded.js +24 -0
- package/esm/src/modules/http.d.ts +46 -0
- package/esm/src/modules/http.d.ts.map +1 -0
- package/esm/src/modules/http.js +59 -0
- package/esm/src/modules/strings.branded.d.ts +185 -0
- package/esm/src/modules/strings.branded.d.ts.map +1 -0
- package/esm/src/modules/strings.branded.js +123 -0
- package/esm/src/modules/strings.d.ts +109 -0
- package/esm/src/modules/strings.d.ts.map +1 -0
- package/esm/src/modules/strings.js +147 -0
- package/esm/src/types.d.ts +318 -0
- package/esm/src/types.d.ts.map +1 -0
- package/esm/src/types.js +1 -0
- package/esm/src/utilities.d.ts +95 -0
- package/esm/src/utilities.d.ts.map +1 -0
- package/esm/src/utilities.js +196 -0
- package/package.json +40 -0
- package/script/mod.d.ts +55 -0
- package/script/mod.d.ts.map +1 -0
- package/script/mod.js +83 -0
- package/script/package.json +3 -0
- package/script/specs/standard-schema-spec.v1.d.ts +56 -0
- package/script/specs/standard-schema-spec.v1.d.ts.map +1 -0
- package/script/specs/standard-schema-spec.v1.js +2 -0
- package/script/src/batch.d.ts +23 -0
- package/script/src/batch.d.ts.map +1 -0
- package/script/src/batch.js +26 -0
- package/script/src/brand.d.ts +62 -0
- package/script/src/brand.d.ts.map +1 -0
- package/script/src/brand.js +12 -0
- package/script/src/context.d.ts +19 -0
- package/script/src/context.d.ts.map +1 -0
- package/script/src/context.js +45 -0
- package/script/src/extend.d.ts +23 -0
- package/script/src/extend.d.ts.map +1 -0
- package/script/src/extend.js +12 -0
- package/script/src/guard.d.ts +288 -0
- package/script/src/guard.d.ts.map +1 -0
- package/script/src/guard.js +640 -0
- package/script/src/helpers/http.helpers.d.ts +3 -0
- package/script/src/helpers/http.helpers.d.ts.map +1 -0
- package/script/src/helpers/http.helpers.js +5 -0
- package/script/src/helpers/strings.helpers.d.ts +18 -0
- package/script/src/helpers/strings.helpers.d.ts.map +1 -0
- package/script/src/helpers/strings.helpers.js +52 -0
- package/script/src/introspect.d.ts +36 -0
- package/script/src/introspect.d.ts.map +1 -0
- package/script/src/introspect.js +31 -0
- package/script/src/modules/async.d.ts +27 -0
- package/script/src/modules/async.d.ts.map +1 -0
- package/script/src/modules/async.js +41 -0
- package/script/src/modules/http.branded.d.ts +42 -0
- package/script/src/modules/http.branded.d.ts.map +1 -0
- package/script/src/modules/http.branded.js +30 -0
- package/script/src/modules/http.d.ts +46 -0
- package/script/src/modules/http.d.ts.map +1 -0
- package/script/src/modules/http.js +62 -0
- package/script/src/modules/strings.branded.d.ts +185 -0
- package/script/src/modules/strings.branded.d.ts.map +1 -0
- package/script/src/modules/strings.branded.js +126 -0
- package/script/src/modules/strings.d.ts +109 -0
- package/script/src/modules/strings.d.ts.map +1 -0
- package/script/src/modules/strings.js +150 -0
- package/script/src/types.d.ts +318 -0
- package/script/src/types.d.ts.map +1 -0
- package/script/src/types.js +2 -0
- package/script/src/utilities.d.ts +95 -0
- package/script/src/utilities.d.ts.map +1 -0
- package/script/src/utilities.js +207 -0
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* This module provides type guards for common string patterns.
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.isCommaDelimitedNumbers = exports.isCommaDelimitedIntegers = exports.isPeriodDelimited = exports.isCommaDelimited = exports.isUUIDv7 = exports.isUUIDv4 = exports.isPhoneNumber = exports.isUSPhone = exports.isInternationalPhone = exports.isEmail = void 0;
|
|
7
|
+
const strings_js_1 = require("./strings.js");
|
|
8
|
+
/**
|
|
9
|
+
* Type guard for validating whether a given value is a valid email string. Uses a
|
|
10
|
+
* branded type to ensure type safety and distinguish it from other strings.
|
|
11
|
+
*
|
|
12
|
+
* Uses the `EMAIL_REGEX` to test if the input is a string that matches the email format.
|
|
13
|
+
*
|
|
14
|
+
* @param t - The value to test for email validity.
|
|
15
|
+
* @returns Boolean indicating whether the input is a valid email.
|
|
16
|
+
*/
|
|
17
|
+
exports.isEmail = strings_js_1.isEmail;
|
|
18
|
+
/**
|
|
19
|
+
* Type guard for validating whether a given value is a valid international phone number string.
|
|
20
|
+
* Uses a branded type to ensure type safety and distinguish it from other strings.
|
|
21
|
+
*
|
|
22
|
+
* Uses the `INT_PHONE_REGEX` to test if the input is a string that matches the international phone number format.
|
|
23
|
+
*
|
|
24
|
+
* @param t - The value to test for international phone number validity.
|
|
25
|
+
* @returns Boolean indicating whether the input is a valid international phone number.
|
|
26
|
+
*/
|
|
27
|
+
exports.isInternationalPhone = strings_js_1.isInternationalPhone;
|
|
28
|
+
/**
|
|
29
|
+
* Type guard for validating whether a given value is a valid US phone number string.
|
|
30
|
+
* Uses a branded type to ensure type safety and distinguish it from other strings.
|
|
31
|
+
*
|
|
32
|
+
* Uses the `US_PHONE_REGEX` to test if the input is a string that matches the US phone number format.
|
|
33
|
+
*
|
|
34
|
+
* @param t - The value to test for US phone number validity.
|
|
35
|
+
* @returns Boolean indicating whether the input is a valid US phone number.
|
|
36
|
+
*/
|
|
37
|
+
exports.isUSPhone = strings_js_1.isUSPhone;
|
|
38
|
+
/**
|
|
39
|
+
* Type guard for validating whether a given value is a valid phone number string,
|
|
40
|
+
* either in international or US format. Uses a branded type to ensure type safety
|
|
41
|
+
* and distinguish it from other strings.
|
|
42
|
+
*
|
|
43
|
+
* Uses both `INT_PHONE_REGEX` and `US_PHONE_REGEX` to test if the input is a string
|
|
44
|
+
* that matches either format.
|
|
45
|
+
*
|
|
46
|
+
* @param t - The value to test for phone number validity.
|
|
47
|
+
* @returns Boolean indicating whether the input is a valid phone number.
|
|
48
|
+
*/
|
|
49
|
+
exports.isPhoneNumber = strings_js_1.isPhoneNumber;
|
|
50
|
+
/**
|
|
51
|
+
* Type guard for validating whether a given value is a valid UUIDv4 string.
|
|
52
|
+
* Uses a branded type to ensure type safety and distinguish it from other strings.
|
|
53
|
+
*
|
|
54
|
+
* Uses a regex pattern to test if the input is a string that matches the UUIDv4 format.
|
|
55
|
+
*
|
|
56
|
+
* @param t - The value to test for UUIDv4 validity.
|
|
57
|
+
* @returns Boolean indicating whether the input is a valid UUIDv4.
|
|
58
|
+
*/
|
|
59
|
+
exports.isUUIDv4 = strings_js_1.isUUIDv4;
|
|
60
|
+
/**
|
|
61
|
+
* Type guard for validating whether a given value is a valid UUIDv7 string.
|
|
62
|
+
* Uses a branded type to ensure type safety and distinguish it from other strings.
|
|
63
|
+
*
|
|
64
|
+
* Uses a regex pattern to test if the input is a string that matches the UUIDv7 format.
|
|
65
|
+
*
|
|
66
|
+
* @param t - The value to test for UUIDv7 validity.
|
|
67
|
+
* @returns Boolean indicating whether the input is a valid UUIDv7.
|
|
68
|
+
*/
|
|
69
|
+
exports.isUUIDv7 = strings_js_1.isUUIDv7;
|
|
70
|
+
/**
|
|
71
|
+
* Type guard for validating whether a given value is a valid comma-delimited string.
|
|
72
|
+
* Uses a branded type to ensure type safety and distinguish it from other strings.
|
|
73
|
+
*
|
|
74
|
+
* Uses a regex pattern to test if the input is a string that matches the comma-delimited format.
|
|
75
|
+
*
|
|
76
|
+
* @param t - The value to test for comma-delimited string validity.
|
|
77
|
+
* @returns Boolean indicating whether the input is a valid comma-delimited string.
|
|
78
|
+
*/
|
|
79
|
+
exports.isCommaDelimited = strings_js_1.isCommaDelimited;
|
|
80
|
+
/**
|
|
81
|
+
* Type guard for validating whether a given value is a valid period-delimited string.
|
|
82
|
+
* Uses a branded type to ensure type safety and distinguish it from other strings.
|
|
83
|
+
*
|
|
84
|
+
* Uses a regex pattern to test if the input is a string that matches the period-delimited format.
|
|
85
|
+
*
|
|
86
|
+
* @param t - The value to test for period-delimited string validity.
|
|
87
|
+
* @returns Boolean indicating whether the input is a valid period-delimited string.
|
|
88
|
+
*/
|
|
89
|
+
exports.isPeriodDelimited = strings_js_1.isPeriodDelimited;
|
|
90
|
+
/**
|
|
91
|
+
* A type guard function that checks if a given string matches the pattern of a comma-delimited
|
|
92
|
+
* string of integers.
|
|
93
|
+
*
|
|
94
|
+
* - Matches one or more integers (positive or negative) separated by commas.
|
|
95
|
+
* - Does not allow empty values between commas.
|
|
96
|
+
* - Does not allow any whitespace (between digits or after commas).
|
|
97
|
+
* - Does not match empty strings.
|
|
98
|
+
*
|
|
99
|
+
* @param t - The value to test for comma-delimited integers validity.
|
|
100
|
+
* @returns Boolean indicating whether the input is a valid comma-delimited string of integers.
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* - Valid: "1,2,3", "123,456,789", "-1,2,-3"
|
|
104
|
+
* - Invalid: "1,,3", "1, 2, 3", "1 2,3", "", "1.5,2"
|
|
105
|
+
*/
|
|
106
|
+
exports.isCommaDelimitedIntegers = strings_js_1.isCommaDelimitedIntegers;
|
|
107
|
+
/**
|
|
108
|
+
* A type guard function that checks if a given string matches the pattern of a comma-delimited
|
|
109
|
+
* string of numbers.
|
|
110
|
+
*
|
|
111
|
+
* - Matches one or more numbers (integers, floats, decimals, or percentages) separated by commas.
|
|
112
|
+
* - Supports positive and negative numbers.
|
|
113
|
+
* - Supports decimal values (e.g., "1.5", "3.14159").
|
|
114
|
+
* - Supports percentage values (e.g., "50%", "12.5%").
|
|
115
|
+
* - Does not allow empty values between commas.
|
|
116
|
+
* - Does not allow any whitespace (between digits or after commas).
|
|
117
|
+
* - Does not match empty strings.
|
|
118
|
+
*
|
|
119
|
+
* @param t - The value to test for comma-delimited numbers validity.
|
|
120
|
+
* @returns Boolean indicating whether the input is a valid comma-delimited string of numbers.
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
* - Valid: "1,2,3", "1.5,2.5,3.5", "-1.5,2,3.14", "50%,75%,100%", "1.5%,2,3%"
|
|
124
|
+
* - Invalid: "1,,3", "1, 2, 3", "1 2,3", "", "1..5,2", "1.5.5,2"
|
|
125
|
+
*/
|
|
126
|
+
exports.isCommaDelimitedNumbers = strings_js_1.isCommaDelimitedNumbers;
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import type { TypeGuard } from "../types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Type guard that checks if the provided value is a valid email string.
|
|
4
|
+
*
|
|
5
|
+
* Uses the `EMAIL_REGEX` to test if the input is a string matching the email format.
|
|
6
|
+
*
|
|
7
|
+
* @param t - The value to check.
|
|
8
|
+
* @returns Boolean indicating whether the input is a valid email.
|
|
9
|
+
*/
|
|
10
|
+
export declare const isEmail: TypeGuard<string>;
|
|
11
|
+
/**
|
|
12
|
+
* Type guard that checks if the provided value is a valid international phone number.
|
|
13
|
+
*
|
|
14
|
+
* Uses the `INT_PHONE_REGEX` to test if the input is a string matching the international phone number format.
|
|
15
|
+
*
|
|
16
|
+
* @param t - The value to check.
|
|
17
|
+
* @returns Boolean indicating whether the input is a valid international phone number.
|
|
18
|
+
*/
|
|
19
|
+
export declare const isInternationalPhone: TypeGuard<string>;
|
|
20
|
+
/**
|
|
21
|
+
* Type guard that checks if the provided value is a valid US phone number.
|
|
22
|
+
*
|
|
23
|
+
* Uses the `US_PHONE_REGEX` to test if the input is a string matching the US phone number format.
|
|
24
|
+
*
|
|
25
|
+
* @param t - The value to check.
|
|
26
|
+
* @returns Boolean indicating whether the input is a valid US phone number.
|
|
27
|
+
*/
|
|
28
|
+
export declare const isUSPhone: TypeGuard<string>;
|
|
29
|
+
/**
|
|
30
|
+
* Type guard that checks if the provided value is a valid phone number, either international or US format.
|
|
31
|
+
*
|
|
32
|
+
* Uses both `INT_PHONE_REGEX` and `US_PHONE_REGEX` to test if the input is a string matching either format.
|
|
33
|
+
*
|
|
34
|
+
* @param t - The value to check.
|
|
35
|
+
* @returns Boolean indicating whether the input is a valid phone number.
|
|
36
|
+
*/
|
|
37
|
+
export declare const isPhoneNumber: TypeGuard<string>;
|
|
38
|
+
/**
|
|
39
|
+
* A type guard function that checks if a given value is a valid UUID version 4 string.
|
|
40
|
+
*
|
|
41
|
+
* @param t - The value to be checked.
|
|
42
|
+
* @returns Boolean indicating whether the input is a valid UUID v4 string.
|
|
43
|
+
*/
|
|
44
|
+
export declare const isUUIDv4: TypeGuard<string>;
|
|
45
|
+
/**
|
|
46
|
+
* A type guard function that checks if a given value is a valid UUID version 7 string.
|
|
47
|
+
*
|
|
48
|
+
* @param t - The value to be checked.
|
|
49
|
+
* @returns Boolean indicating whether the input is a valid UUID v7 string.
|
|
50
|
+
*/
|
|
51
|
+
export declare const isUUIDv7: TypeGuard<string>;
|
|
52
|
+
/**
|
|
53
|
+
* A type guard function that checks if a given string matches the pattern of a comma-delimited string.
|
|
54
|
+
*
|
|
55
|
+
* This function extends the `isString` type guard and applies an additional validation
|
|
56
|
+
* using the `COMMA_DELIMITED_REGEX` regular expression. If the string matches the pattern,
|
|
57
|
+
* it is returned; otherwise, `null` is returned.
|
|
58
|
+
*
|
|
59
|
+
* @param t - The input value to be checked.
|
|
60
|
+
* @returns Boolean indicating whether the input is a valid comma-delimited string.
|
|
61
|
+
*/
|
|
62
|
+
export declare const isCommaDelimited: TypeGuard<string>;
|
|
63
|
+
/**
|
|
64
|
+
* Checks if a given string matches the period-delimited format.
|
|
65
|
+
* This function extends the `isString` type guard to validate
|
|
66
|
+
* whether the input string conforms to the `PERIOD_DELIMITED_REGEX`.
|
|
67
|
+
*
|
|
68
|
+
* @param t - The string to be validated.
|
|
69
|
+
* @returns Boolean indicating whether the input is a valid period-delimited string.
|
|
70
|
+
*/
|
|
71
|
+
export declare const isPeriodDelimited: TypeGuard<string>;
|
|
72
|
+
/**
|
|
73
|
+
* A type guard function that checks if a given string matches the pattern of a comma-delimited
|
|
74
|
+
* string of integers.
|
|
75
|
+
*
|
|
76
|
+
* - Matches one or more integers (positive or negative) separated by commas.
|
|
77
|
+
* - Does not allow empty values between commas.
|
|
78
|
+
* - Does not allow any whitespace (between digits or after commas).
|
|
79
|
+
* - Does not match empty strings.
|
|
80
|
+
*
|
|
81
|
+
* @param t - The value to test for comma-delimited integers validity.
|
|
82
|
+
* @returns Boolean indicating whether the input is a valid comma-delimited string of integers.
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* - Valid: "1,2,3", "123,456,789", "-1,2,-3"
|
|
86
|
+
* - Invalid: "1,,3", "1, 2, 3", "1 2,3", "", "1.5,2"
|
|
87
|
+
*/
|
|
88
|
+
export declare const isCommaDelimitedIntegers: TypeGuard<string>;
|
|
89
|
+
/**
|
|
90
|
+
* A type guard function that checks if a given string matches the pattern of a comma-delimited
|
|
91
|
+
* string of numbers.
|
|
92
|
+
*
|
|
93
|
+
* - Matches one or more numbers (integers, floats, decimals, or percentages) separated by commas.
|
|
94
|
+
* - Supports positive and negative numbers.
|
|
95
|
+
* - Supports decimal values (e.g., "1.5", "3.14159").
|
|
96
|
+
* - Supports percentage values (e.g., "50%", "12.5%").
|
|
97
|
+
* - Does not allow empty values between commas.
|
|
98
|
+
* - Does not allow any whitespace (between digits or after commas).
|
|
99
|
+
* - Does not match empty strings.
|
|
100
|
+
*
|
|
101
|
+
* @param t - The value to test for comma-delimited numbers validity.
|
|
102
|
+
* @returns Boolean indicating whether the input is a valid comma-delimited string of numbers.
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* - Valid: "1,2,3", "1.5,2.5,3.5", "-1.5,2,3.14", "50%,75%,100%", "1.5%,2,3%"
|
|
106
|
+
* - Invalid: "1,,3", "1, 2, 3", "1 2,3", "", "1..5,2", "1.5.5,2"
|
|
107
|
+
*/
|
|
108
|
+
export declare const isCommaDelimitedNumbers: TypeGuard<string>;
|
|
109
|
+
//# sourceMappingURL=strings.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"strings.d.ts","sourceRoot":"","sources":["../../../src/src/modules/strings.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAM7C;;;;;;;GAOG;AACH,eAAO,MAAM,OAAO,EAAE,SAAS,CAAC,MAAM,CAGrC,CAAC;AAOF;;;;;;;GAOG;AACH,eAAO,MAAM,oBAAoB,EAAE,SAAS,CAAC,MAAM,CAGlD,CAAC;AAOF;;;;;;;GAOG;AACH,eAAO,MAAM,SAAS,EAAE,SAAS,CAAC,MAAM,CAGvC,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,aAAa,EAAE,SAAS,CAAC,MAAM,CAQ3C,CAAC;AAKF;;;;;GAKG;AACH,eAAO,MAAM,QAAQ,EAAE,SAAS,CAAC,MAAM,CAGtC,CAAC;AAKF;;;;;GAKG;AACH,eAAO,MAAM,QAAQ,EAAE,SAAS,CAAC,MAAM,CAGtC,CAAC;AAiBF;;;;;;;;;GASG;AACH,eAAO,MAAM,gBAAgB,EAAE,SAAS,CAAC,MAAM,CAG9C,CAAC;AAKF;;;;;;;GAOG;AACH,eAAO,MAAM,iBAAiB,EAAE,SAAS,CAAC,MAAM,CAG/C,CAAC;AAIF;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,wBAAwB,EAAE,SAAS,CAAC,MAAM,CAGtD,CAAC;AAIF;;;;;;;;;;;;;;;;;;GAkBG;AACH,eAAO,MAAM,uBAAuB,EAAE,SAAS,CAAC,MAAM,CAGrD,CAAC"}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isCommaDelimitedNumbers = exports.isCommaDelimitedIntegers = exports.isPeriodDelimited = exports.isCommaDelimited = exports.isUUIDv7 = exports.isUUIDv4 = exports.isPhoneNumber = exports.isUSPhone = exports.isInternationalPhone = exports.isEmail = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Type guards for common string formats such as email, phone numbers, and UUIDs.
|
|
6
|
+
* These guards extend the basic string type guard to include regex-based validation.
|
|
7
|
+
*/
|
|
8
|
+
const guard_js_1 = require("../guard.js");
|
|
9
|
+
/** A regex statement to detect _most_ email formats. */
|
|
10
|
+
const EMAIL_REGEX = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?)*\.[a-zA-Z]{2,}$/;
|
|
11
|
+
/**
|
|
12
|
+
* Type guard that checks if the provided value is a valid email string.
|
|
13
|
+
*
|
|
14
|
+
* Uses the `EMAIL_REGEX` to test if the input is a string matching the email format.
|
|
15
|
+
*
|
|
16
|
+
* @param t - The value to check.
|
|
17
|
+
* @returns Boolean indicating whether the input is a valid email.
|
|
18
|
+
*/
|
|
19
|
+
exports.isEmail = guard_js_1.isString.extend("email", (t) => EMAIL_REGEX.test(t) ? t : null);
|
|
20
|
+
/** Regex for validating international phone numbers. See
|
|
21
|
+
* https://blog.stevenlevithan.com/archives/validate-phone-number#r4-3 for more
|
|
22
|
+
* information. */
|
|
23
|
+
const INT_PHONE_REGEX = /^\+(?:[0-9] ?){6,14}[0-9]$/;
|
|
24
|
+
/**
|
|
25
|
+
* Type guard that checks if the provided value is a valid international phone number.
|
|
26
|
+
*
|
|
27
|
+
* Uses the `INT_PHONE_REGEX` to test if the input is a string matching the international phone number format.
|
|
28
|
+
*
|
|
29
|
+
* @param t - The value to check.
|
|
30
|
+
* @returns Boolean indicating whether the input is a valid international phone number.
|
|
31
|
+
*/
|
|
32
|
+
exports.isInternationalPhone = guard_js_1.isString.extend("international phone number", (t) => INT_PHONE_REGEX.test(t) ? t : null);
|
|
33
|
+
/** Regex for validating US phone numbers. See
|
|
34
|
+
* https://blog.stevenlevithan.com/archives/validate-phone-number#r4-3 for more
|
|
35
|
+
* information. */
|
|
36
|
+
const US_PHONE_REGEX = /^(?:\+?1[-. ]?)?\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
|
|
37
|
+
/**
|
|
38
|
+
* Type guard that checks if the provided value is a valid US phone number.
|
|
39
|
+
*
|
|
40
|
+
* Uses the `US_PHONE_REGEX` to test if the input is a string matching the US phone number format.
|
|
41
|
+
*
|
|
42
|
+
* @param t - The value to check.
|
|
43
|
+
* @returns Boolean indicating whether the input is a valid US phone number.
|
|
44
|
+
*/
|
|
45
|
+
exports.isUSPhone = guard_js_1.isString.extend("US phone number", (t) => US_PHONE_REGEX.test(t) ? t : null);
|
|
46
|
+
/**
|
|
47
|
+
* Type guard that checks if the provided value is a valid phone number, either international or US format.
|
|
48
|
+
*
|
|
49
|
+
* Uses both `INT_PHONE_REGEX` and `US_PHONE_REGEX` to test if the input is a string matching either format.
|
|
50
|
+
*
|
|
51
|
+
* @param t - The value to check.
|
|
52
|
+
* @returns Boolean indicating whether the input is a valid phone number.
|
|
53
|
+
*/
|
|
54
|
+
exports.isPhoneNumber = guard_js_1.isString.extend("phone number", (t) => {
|
|
55
|
+
if (INT_PHONE_REGEX.test(t) || US_PHONE_REGEX.test(t)) {
|
|
56
|
+
return t;
|
|
57
|
+
}
|
|
58
|
+
return null;
|
|
59
|
+
});
|
|
60
|
+
/** Regex for validating UUID v4 strings. */
|
|
61
|
+
const UUID_4_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
|
62
|
+
/**
|
|
63
|
+
* A type guard function that checks if a given value is a valid UUID version 4 string.
|
|
64
|
+
*
|
|
65
|
+
* @param t - The value to be checked.
|
|
66
|
+
* @returns Boolean indicating whether the input is a valid UUID v4 string.
|
|
67
|
+
*/
|
|
68
|
+
exports.isUUIDv4 = guard_js_1.isString.extend("UUIDv4", (t) => UUID_4_REGEX.test(t) ? t : null);
|
|
69
|
+
/** Regex for validating UUID v7 strings. */
|
|
70
|
+
const UUID_7_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
|
71
|
+
/**
|
|
72
|
+
* A type guard function that checks if a given value is a valid UUID version 7 string.
|
|
73
|
+
*
|
|
74
|
+
* @param t - The value to be checked.
|
|
75
|
+
* @returns Boolean indicating whether the input is a valid UUID v7 string.
|
|
76
|
+
*/
|
|
77
|
+
exports.isUUIDv7 = guard_js_1.isString.extend("UUIDv7", (t) => UUID_7_REGEX.test(t) ? t : null);
|
|
78
|
+
/**
|
|
79
|
+
* A regular expression used to validate comma-delimited strings.
|
|
80
|
+
*
|
|
81
|
+
* - Matches strings enclosed in double quotes, allowing for escaped characters within the quotes.
|
|
82
|
+
* - Matches unquoted segments that do not contain commas, double quotes, or backslashes.
|
|
83
|
+
* - Useful for parsing CSV-like strings where fields may be quoted or unquoted.
|
|
84
|
+
* - Validates the entire string structure, allowing empty strings, single values, or comma-separated values.
|
|
85
|
+
*
|
|
86
|
+
* Example:
|
|
87
|
+
* Input: `"value1","value2,with,commas",unquotedValue`
|
|
88
|
+
* Matches: entire string as valid comma-delimited format
|
|
89
|
+
*/
|
|
90
|
+
const COMMA_DELIMITED_REGEX = /^(?:(?:"(?:[^"\\]|\\.)*"|[^,"\\]+)(?:,(?:"(?:[^"\\]|\\.)*"|[^,"\\]+))*)?$/;
|
|
91
|
+
/**
|
|
92
|
+
* A type guard function that checks if a given string matches the pattern of a comma-delimited string.
|
|
93
|
+
*
|
|
94
|
+
* This function extends the `isString` type guard and applies an additional validation
|
|
95
|
+
* using the `COMMA_DELIMITED_REGEX` regular expression. If the string matches the pattern,
|
|
96
|
+
* it is returned; otherwise, `null` is returned.
|
|
97
|
+
*
|
|
98
|
+
* @param t - The input value to be checked.
|
|
99
|
+
* @returns Boolean indicating whether the input is a valid comma-delimited string.
|
|
100
|
+
*/
|
|
101
|
+
exports.isCommaDelimited = guard_js_1.isString.extend("comma-delimited string", (t) => COMMA_DELIMITED_REGEX.test(t) ? t : null);
|
|
102
|
+
const PERIOD_DELIMITED_REGEX = /^(?:(?:"(?:[^"\\]|\\.)*"|[^."\\""]+)(?:.(?:"(?:[^"\\]|\\.)*"|[^."\\""]+))*)?$/;
|
|
103
|
+
/**
|
|
104
|
+
* Checks if a given string matches the period-delimited format.
|
|
105
|
+
* This function extends the `isString` type guard to validate
|
|
106
|
+
* whether the input string conforms to the `PERIOD_DELIMITED_REGEX`.
|
|
107
|
+
*
|
|
108
|
+
* @param t - The string to be validated.
|
|
109
|
+
* @returns Boolean indicating whether the input is a valid period-delimited string.
|
|
110
|
+
*/
|
|
111
|
+
exports.isPeriodDelimited = guard_js_1.isString.extend("period-delimited string", (t) => PERIOD_DELIMITED_REGEX.test(t) ? t : null);
|
|
112
|
+
const COMMA_DELIMITED_INTEGERS_REGEX = /^-?\d+(?:,-?\d+)*$/;
|
|
113
|
+
/**
|
|
114
|
+
* A type guard function that checks if a given string matches the pattern of a comma-delimited
|
|
115
|
+
* string of integers.
|
|
116
|
+
*
|
|
117
|
+
* - Matches one or more integers (positive or negative) separated by commas.
|
|
118
|
+
* - Does not allow empty values between commas.
|
|
119
|
+
* - Does not allow any whitespace (between digits or after commas).
|
|
120
|
+
* - Does not match empty strings.
|
|
121
|
+
*
|
|
122
|
+
* @param t - The value to test for comma-delimited integers validity.
|
|
123
|
+
* @returns Boolean indicating whether the input is a valid comma-delimited string of integers.
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
* - Valid: "1,2,3", "123,456,789", "-1,2,-3"
|
|
127
|
+
* - Invalid: "1,,3", "1, 2, 3", "1 2,3", "", "1.5,2"
|
|
128
|
+
*/
|
|
129
|
+
exports.isCommaDelimitedIntegers = guard_js_1.isString.extend("comma-delimited integers", (t) => COMMA_DELIMITED_INTEGERS_REGEX.test(t) ? t : null);
|
|
130
|
+
const COMMA_DELIMITED_NUMBERS_REGEX = /^-?\d+(?:\.\d+)?%?(?:,-?\d+(?:\.\d+)?%?)*$/;
|
|
131
|
+
/**
|
|
132
|
+
* A type guard function that checks if a given string matches the pattern of a comma-delimited
|
|
133
|
+
* string of numbers.
|
|
134
|
+
*
|
|
135
|
+
* - Matches one or more numbers (integers, floats, decimals, or percentages) separated by commas.
|
|
136
|
+
* - Supports positive and negative numbers.
|
|
137
|
+
* - Supports decimal values (e.g., "1.5", "3.14159").
|
|
138
|
+
* - Supports percentage values (e.g., "50%", "12.5%").
|
|
139
|
+
* - Does not allow empty values between commas.
|
|
140
|
+
* - Does not allow any whitespace (between digits or after commas).
|
|
141
|
+
* - Does not match empty strings.
|
|
142
|
+
*
|
|
143
|
+
* @param t - The value to test for comma-delimited numbers validity.
|
|
144
|
+
* @returns Boolean indicating whether the input is a valid comma-delimited string of numbers.
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
* - Valid: "1,2,3", "1.5,2.5,3.5", "-1.5,2,3.14", "50%,75%,100%", "1.5%,2,3%"
|
|
148
|
+
* - Invalid: "1,,3", "1, 2, 3", "1 2,3", "", "1..5,2", "1.5.5,2"
|
|
149
|
+
*/
|
|
150
|
+
exports.isCommaDelimitedNumbers = guard_js_1.isString.extend("comma-delimited numbers", (t) => COMMA_DELIMITED_NUMBERS_REGEX.test(t) ? t : null);
|