@samline/formatter 1.0.1 → 1.1.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 +6 -4
- package/dist/browser/global.d.ts +209 -10
- package/dist/browser/global.global.js +239 -44
- package/dist/index.cjs +209 -14
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +316 -6
- package/dist/index.d.ts +316 -6
- package/dist/index.js +209 -14
- package/dist/index.js.map +1 -1
- package/dist/vanilla/index.cjs +209 -14
- package/dist/vanilla/index.cjs.map +1 -1
- package/dist/vanilla/index.d.cts +316 -6
- package/dist/vanilla/index.d.ts +316 -6
- package/dist/vanilla/index.js +209 -14
- package/dist/vanilla/index.js.map +1 -1
- package/package.json +2 -2
|
@@ -2082,8 +2082,8 @@ var Formatter = (() => {
|
|
|
2082
2082
|
}
|
|
2083
2083
|
return cleared_string;
|
|
2084
2084
|
}
|
|
2085
|
-
function populateTemplateWithDigits(template, position,
|
|
2086
|
-
for (var _iterator2 = _createForOfIteratorHelperLoose5(
|
|
2085
|
+
function populateTemplateWithDigits(template, position, digits2) {
|
|
2086
|
+
for (var _iterator2 = _createForOfIteratorHelperLoose5(digits2.split("")), _step2; !(_step2 = _iterator2()).done; ) {
|
|
2087
2087
|
var digit = _step2.value;
|
|
2088
2088
|
if (template.slice(position + 1).search(DIGIT_PLACEHOLDER_MATCHER) < 0) {
|
|
2089
2089
|
return;
|
|
@@ -2894,14 +2894,14 @@ var Formatter = (() => {
|
|
|
2894
2894
|
if (SUPPORT_LEGACY_FORMATTING_PATTERNS) {
|
|
2895
2895
|
pattern = pattern.replace(CREATE_CHARACTER_CLASS_PATTERN(), "\\d").replace(CREATE_STANDALONE_DIGIT_PATTERN(), "\\d");
|
|
2896
2896
|
}
|
|
2897
|
-
var
|
|
2898
|
-
if (nationalSignificantNumber.length >
|
|
2897
|
+
var digits2 = LONGEST_DUMMY_PHONE_NUMBER.match(pattern)[0];
|
|
2898
|
+
if (nationalSignificantNumber.length > digits2.length) {
|
|
2899
2899
|
return;
|
|
2900
2900
|
}
|
|
2901
2901
|
var strictPattern = new RegExp("^" + pattern + "$");
|
|
2902
2902
|
var nationalNumberDummyDigits = nationalSignificantNumber.replace(/\d/g, DUMMY_DIGIT);
|
|
2903
2903
|
if (strictPattern.test(nationalNumberDummyDigits)) {
|
|
2904
|
-
|
|
2904
|
+
digits2 = nationalNumberDummyDigits;
|
|
2905
2905
|
}
|
|
2906
2906
|
var numberFormat = this.getFormatFormat(format2, international);
|
|
2907
2907
|
var nationalPrefixIncludedInTemplate;
|
|
@@ -2922,7 +2922,7 @@ var Formatter = (() => {
|
|
|
2922
2922
|
}
|
|
2923
2923
|
}
|
|
2924
2924
|
}
|
|
2925
|
-
var template =
|
|
2925
|
+
var template = digits2.replace(new RegExp(pattern), numberFormat).replace(new RegExp(DUMMY_DIGIT, "g"), DIGIT_PLACEHOLDER);
|
|
2926
2926
|
if (!nationalPrefixIncludedInTemplate) {
|
|
2927
2927
|
if (prefixBeforeNationalSignificantNumberThatIsNotNationalPrefix) {
|
|
2928
2928
|
template = repeat(DIGIT_PLACEHOLDER, prefixBeforeNationalSignificantNumberThatIsNotNationalPrefix.length) + " " + template;
|
|
@@ -2937,8 +2937,8 @@ var Formatter = (() => {
|
|
|
2937
2937
|
}
|
|
2938
2938
|
}, {
|
|
2939
2939
|
key: "formatNextNationalNumberDigits",
|
|
2940
|
-
value: function formatNextNationalNumberDigits(
|
|
2941
|
-
var result = populateTemplateWithDigits(this.populatedNationalNumberTemplate, this.populatedNationalNumberTemplatePosition,
|
|
2940
|
+
value: function formatNextNationalNumberDigits(digits2) {
|
|
2941
|
+
var result = populateTemplateWithDigits(this.populatedNationalNumberTemplate, this.populatedNationalNumberTemplatePosition, digits2);
|
|
2942
2942
|
if (!result) {
|
|
2943
2943
|
this.resetFormat();
|
|
2944
2944
|
return;
|
|
@@ -3056,21 +3056,21 @@ var Formatter = (() => {
|
|
|
3056
3056
|
key: "input",
|
|
3057
3057
|
value: function input(text, state) {
|
|
3058
3058
|
var _extractFormattedDigi = extractFormattedDigitsAndPlus(text), _extractFormattedDigi2 = _slicedToArray(_extractFormattedDigi, 2), formattedDigits = _extractFormattedDigi2[0], hasPlus = _extractFormattedDigi2[1];
|
|
3059
|
-
var
|
|
3059
|
+
var digits2 = parseDigits(formattedDigits);
|
|
3060
3060
|
var justLeadingPlus;
|
|
3061
3061
|
if (hasPlus) {
|
|
3062
3062
|
if (!state.digits) {
|
|
3063
3063
|
state.startInternationalNumber(void 0, void 0);
|
|
3064
|
-
if (!
|
|
3064
|
+
if (!digits2) {
|
|
3065
3065
|
justLeadingPlus = true;
|
|
3066
3066
|
}
|
|
3067
3067
|
}
|
|
3068
3068
|
}
|
|
3069
|
-
if (
|
|
3070
|
-
this.inputDigits(
|
|
3069
|
+
if (digits2) {
|
|
3070
|
+
this.inputDigits(digits2, state);
|
|
3071
3071
|
}
|
|
3072
3072
|
return {
|
|
3073
|
-
digits,
|
|
3073
|
+
digits: digits2,
|
|
3074
3074
|
justLeadingPlus
|
|
3075
3075
|
};
|
|
3076
3076
|
}
|
|
@@ -3082,8 +3082,8 @@ var Formatter = (() => {
|
|
|
3082
3082
|
}, {
|
|
3083
3083
|
key: "inputDigits",
|
|
3084
3084
|
value: function inputDigits(nextDigits, state) {
|
|
3085
|
-
var
|
|
3086
|
-
var hasReceivedThreeLeadingDigits =
|
|
3085
|
+
var digits2 = state.digits;
|
|
3086
|
+
var hasReceivedThreeLeadingDigits = digits2.length < 3 && digits2.length + nextDigits.length >= 3;
|
|
3087
3087
|
state.appendDigits(nextDigits);
|
|
3088
3088
|
if (hasReceivedThreeLeadingDigits) {
|
|
3089
3089
|
this.extractIddPrefix(state);
|
|
@@ -3222,14 +3222,14 @@ var Formatter = (() => {
|
|
|
3222
3222
|
}, {
|
|
3223
3223
|
key: "extractIddPrefix",
|
|
3224
3224
|
value: function extractIddPrefix(state) {
|
|
3225
|
-
var international = state.international, IDDPrefix = state.IDDPrefix,
|
|
3225
|
+
var international = state.international, IDDPrefix = state.IDDPrefix, digits2 = state.digits, nationalSignificantNumber = state.nationalSignificantNumber;
|
|
3226
3226
|
if (international || IDDPrefix) {
|
|
3227
3227
|
return;
|
|
3228
3228
|
}
|
|
3229
|
-
var numberWithoutIDD = stripIddPrefix(
|
|
3230
|
-
if (numberWithoutIDD !== void 0 && numberWithoutIDD !==
|
|
3229
|
+
var numberWithoutIDD = stripIddPrefix(digits2, this.defaultCountry, this.defaultCallingCode, this.metadata.metadata);
|
|
3230
|
+
if (numberWithoutIDD !== void 0 && numberWithoutIDD !== digits2) {
|
|
3231
3231
|
state.update({
|
|
3232
|
-
IDDPrefix:
|
|
3232
|
+
IDDPrefix: digits2.slice(0, digits2.length - numberWithoutIDD.length)
|
|
3233
3233
|
});
|
|
3234
3234
|
this.startInternationalNumber(state, {
|
|
3235
3235
|
country: void 0,
|
|
@@ -3429,17 +3429,17 @@ var Formatter = (() => {
|
|
|
3429
3429
|
}, {
|
|
3430
3430
|
key: "input",
|
|
3431
3431
|
value: function input(text) {
|
|
3432
|
-
var _this$parser$input = this.parser.input(text, this.state),
|
|
3432
|
+
var _this$parser$input = this.parser.input(text, this.state), digits2 = _this$parser$input.digits, justLeadingPlus = _this$parser$input.justLeadingPlus;
|
|
3433
3433
|
if (justLeadingPlus) {
|
|
3434
3434
|
this.formattedOutput = "+";
|
|
3435
|
-
} else if (
|
|
3435
|
+
} else if (digits2) {
|
|
3436
3436
|
this.determineTheCountryIfNeeded();
|
|
3437
3437
|
if (this.state.nationalSignificantNumber) {
|
|
3438
3438
|
this.formatter.narrowDownMatchingFormats(this.state);
|
|
3439
3439
|
}
|
|
3440
3440
|
var formattedNationalNumber;
|
|
3441
3441
|
if (this.metadata.hasSelectedNumberingPlan()) {
|
|
3442
|
-
formattedNationalNumber = this.formatter.format(
|
|
3442
|
+
formattedNationalNumber = this.formatter.format(digits2, this.state);
|
|
3443
3443
|
}
|
|
3444
3444
|
if (formattedNationalNumber === void 0) {
|
|
3445
3445
|
if (this.parser.reExtractNationalSignificantNumber(this.state)) {
|
|
@@ -3527,8 +3527,8 @@ var Formatter = (() => {
|
|
|
3527
3527
|
}, {
|
|
3528
3528
|
key: "getCountry",
|
|
3529
3529
|
value: function getCountry() {
|
|
3530
|
-
var
|
|
3531
|
-
if (
|
|
3530
|
+
var digits2 = this.state.digits;
|
|
3531
|
+
if (digits2) {
|
|
3532
3532
|
return this._getCountry();
|
|
3533
3533
|
}
|
|
3534
3534
|
}
|
|
@@ -3647,15 +3647,15 @@ var Formatter = (() => {
|
|
|
3647
3647
|
}, {
|
|
3648
3648
|
key: "getNumberValue",
|
|
3649
3649
|
value: function getNumberValue() {
|
|
3650
|
-
var _this$state2 = this.state,
|
|
3651
|
-
if (!
|
|
3650
|
+
var _this$state2 = this.state, digits2 = _this$state2.digits, callingCode = _this$state2.callingCode, country = _this$state2.country, nationalSignificantNumber = _this$state2.nationalSignificantNumber;
|
|
3651
|
+
if (!digits2) {
|
|
3652
3652
|
return;
|
|
3653
3653
|
}
|
|
3654
3654
|
if (this.isInternational()) {
|
|
3655
3655
|
if (callingCode) {
|
|
3656
3656
|
return "+" + callingCode + nationalSignificantNumber;
|
|
3657
3657
|
} else {
|
|
3658
|
-
return "+" +
|
|
3658
|
+
return "+" + digits2;
|
|
3659
3659
|
}
|
|
3660
3660
|
} else {
|
|
3661
3661
|
if (country || callingCode) {
|
|
@@ -3733,8 +3733,8 @@ var Formatter = (() => {
|
|
|
3733
3733
|
}, {
|
|
3734
3734
|
key: "validateLength",
|
|
3735
3735
|
value: function validateLength() {
|
|
3736
|
-
var _this$state4 = this.state,
|
|
3737
|
-
if (!
|
|
3736
|
+
var _this$state4 = this.state, digits2 = _this$state4.digits, nationalSignificantNumber = _this$state4.nationalSignificantNumber;
|
|
3737
|
+
if (!digits2) {
|
|
3738
3738
|
return "NOT_A_NUMBER";
|
|
3739
3739
|
}
|
|
3740
3740
|
if (!this.metadata.numberingPlan) {
|
|
@@ -3795,9 +3795,9 @@ var Formatter = (() => {
|
|
|
3795
3795
|
// src/core/phone.ts
|
|
3796
3796
|
var formatPhone = (value, country = "MX", delimiter = " ") => {
|
|
3797
3797
|
if (!value) return "";
|
|
3798
|
-
const
|
|
3798
|
+
const digits2 = value.replace(/[^\d+]/g, "");
|
|
3799
3799
|
const formatter = new AsYouType2(country);
|
|
3800
|
-
const formatted = formatter.input(
|
|
3800
|
+
const formatted = formatter.input(digits2);
|
|
3801
3801
|
if (delimiter !== " ") {
|
|
3802
3802
|
return formatted.replace(/ /g, delimiter);
|
|
3803
3803
|
}
|
|
@@ -3834,12 +3834,12 @@ var Formatter = (() => {
|
|
|
3834
3834
|
}
|
|
3835
3835
|
};
|
|
3836
3836
|
var getDateSegments = (value, pattern) => {
|
|
3837
|
-
const
|
|
3837
|
+
const digits2 = value.replace(/[^\d]/g, "");
|
|
3838
3838
|
const segments = {};
|
|
3839
3839
|
let start = 0;
|
|
3840
3840
|
for (const unit of pattern) {
|
|
3841
3841
|
const end = start + getDateUnitLength(unit);
|
|
3842
|
-
const segment =
|
|
3842
|
+
const segment = digits2.slice(start, end);
|
|
3843
3843
|
if (segment) {
|
|
3844
3844
|
segments[getDateSegmentKey(unit)] = segment;
|
|
3845
3845
|
}
|
|
@@ -3861,12 +3861,12 @@ var Formatter = (() => {
|
|
|
3861
3861
|
}
|
|
3862
3862
|
};
|
|
3863
3863
|
var getTimeSegments = (value, pattern) => {
|
|
3864
|
-
const
|
|
3864
|
+
const digits2 = value.replace(/[^\d]/g, "");
|
|
3865
3865
|
const segments = {};
|
|
3866
3866
|
let start = 0;
|
|
3867
3867
|
for (const unit of pattern) {
|
|
3868
3868
|
const end = start + 2;
|
|
3869
|
-
const segment =
|
|
3869
|
+
const segment = digits2.slice(start, end);
|
|
3870
3870
|
if (segment) {
|
|
3871
3871
|
segments[getTimeSegmentKey(unit)] = segment;
|
|
3872
3872
|
}
|
|
@@ -4032,21 +4032,81 @@ var Formatter = (() => {
|
|
|
4032
4032
|
};
|
|
4033
4033
|
|
|
4034
4034
|
// src/core/regex.ts
|
|
4035
|
-
|
|
4035
|
+
function createDigits(min, max, errorMsg) {
|
|
4036
|
+
return {
|
|
4037
|
+
pattern: new RegExp(`^\\d{${min},${max}}$`),
|
|
4038
|
+
errorMessage: errorMsg
|
|
4039
|
+
};
|
|
4040
|
+
}
|
|
4041
|
+
function createPhone(length) {
|
|
4042
|
+
return {
|
|
4043
|
+
pattern: new RegExp(`^(?:\\D*\\d){${length}}\\D*$`),
|
|
4044
|
+
errorMessage: `Please enter a valid ${length}-digit phone number.`
|
|
4045
|
+
};
|
|
4046
|
+
}
|
|
4047
|
+
function createCreditCard(min, max) {
|
|
4048
|
+
return {
|
|
4049
|
+
pattern: new RegExp(`^(?:\\D*\\d){${min},${max}}\\D*$`),
|
|
4050
|
+
errorMessage: `Please enter a valid card number (${min}-${max} digits).`
|
|
4051
|
+
};
|
|
4052
|
+
}
|
|
4053
|
+
function createUrl(requireProtocol) {
|
|
4054
|
+
const protocol = requireProtocol === "all" ? "https?" : requireProtocol;
|
|
4055
|
+
return {
|
|
4056
|
+
pattern: new RegExp(`^${protocol}://[^\\s]+$`),
|
|
4057
|
+
errorMessage: `Please enter a valid URL${requireProtocol !== "all" ? ` (${requireProtocol}://...)` : ""}.`
|
|
4058
|
+
};
|
|
4059
|
+
}
|
|
4060
|
+
function createPassword(params) {
|
|
4061
|
+
const {
|
|
4062
|
+
min = 8,
|
|
4063
|
+
max = 128,
|
|
4064
|
+
uppercase = true,
|
|
4065
|
+
lowercase = true,
|
|
4066
|
+
numbers = true,
|
|
4067
|
+
special = false
|
|
4068
|
+
} = params;
|
|
4069
|
+
let chars = "";
|
|
4070
|
+
if (uppercase) chars += "A-Z";
|
|
4071
|
+
if (lowercase) chars += "a-z";
|
|
4072
|
+
if (numbers) chars += "0-9";
|
|
4073
|
+
if (special) chars += `!@#$%^&*()_+\\-=\\[\\]{}|;:'",.<>?/`;
|
|
4074
|
+
const ruleParts = [];
|
|
4075
|
+
if (uppercase) ruleParts.push("uppercase letter");
|
|
4076
|
+
if (lowercase) ruleParts.push("lowercase letter");
|
|
4077
|
+
if (numbers) ruleParts.push("number");
|
|
4078
|
+
if (special) ruleParts.push("special character");
|
|
4079
|
+
const rulesText = ruleParts.length > 2 ? ruleParts.slice(0, -1).join(", ") + " and " + ruleParts.slice(-1) : ruleParts.join(" and ");
|
|
4080
|
+
return {
|
|
4081
|
+
pattern: new RegExp(`^[${chars}]{${min},${max}}$`),
|
|
4082
|
+
errorMessage: `Password must be ${min}-${max} characters with at least one ${rulesText}.`
|
|
4083
|
+
};
|
|
4084
|
+
}
|
|
4085
|
+
var _regex = {
|
|
4036
4086
|
phone: {
|
|
4037
|
-
// 10 digit phone numbers, allowing for optional country code and delimiters
|
|
4038
4087
|
pattern: /^(?:\D*\d){10}\D*$/,
|
|
4039
4088
|
errorMessage: "Please enter a valid 10-digit phone number."
|
|
4040
4089
|
},
|
|
4041
4090
|
email: {
|
|
4091
|
+
// Simple but effective email validation
|
|
4042
4092
|
pattern: /^[^\s@]+@[^\s@]+\.[a-zA-Z]{2,}$/,
|
|
4043
4093
|
errorMessage: "Please enter a valid email address."
|
|
4044
4094
|
},
|
|
4045
4095
|
rfc: {
|
|
4046
|
-
// Mexican RFC
|
|
4096
|
+
// Mexican RFC ( Personas Morales y Físicas )
|
|
4047
4097
|
pattern: /^([A-ZÑ&]{3,4})\d{6}([A-Z0-9]{0,3})$/i,
|
|
4048
4098
|
errorMessage: "Please enter a valid RFC."
|
|
4049
4099
|
},
|
|
4100
|
+
curp: {
|
|
4101
|
+
// Mexican CURP (18 characters)
|
|
4102
|
+
pattern: /^[A-Z]{4}\d{6}[A-Z]{6}[0-9A-Z]\d$/i,
|
|
4103
|
+
errorMessage: "Please enter a valid CURP."
|
|
4104
|
+
},
|
|
4105
|
+
cp: {
|
|
4106
|
+
// Mexican postal code (5 digits)
|
|
4107
|
+
pattern: /^\d{5}$/,
|
|
4108
|
+
errorMessage: "Please enter a valid 5-digit postal code."
|
|
4109
|
+
},
|
|
4050
4110
|
numeral: {
|
|
4051
4111
|
// Numbers with optional thousand separators and decimal points
|
|
4052
4112
|
pattern: /\d{1,3}(,\d{3})*(\.\d+)?/,
|
|
@@ -4059,17 +4119,17 @@ var Formatter = (() => {
|
|
|
4059
4119
|
creditCard: {
|
|
4060
4120
|
// 15 or 16 digits, allowing spaces or delimiters
|
|
4061
4121
|
pattern: /^(?:\D*\d){15,16}\D*$/,
|
|
4062
|
-
errorMessage: "Please enter a valid card number."
|
|
4122
|
+
errorMessage: "Please enter a valid card number (15-16 digits)."
|
|
4063
4123
|
},
|
|
4064
4124
|
expirationDate: {
|
|
4065
|
-
// MM/YY format
|
|
4066
|
-
pattern: /^(0[1-9]|1[0-2])
|
|
4067
|
-
errorMessage: "Please enter a valid expiration date."
|
|
4125
|
+
// MM/YY or MM/YYYY format
|
|
4126
|
+
pattern: /^(0[1-9]|1[0-2])\/(\d{2}|\d{4})$/,
|
|
4127
|
+
errorMessage: "Please enter a valid expiration date (MM/YY or MM/YYYY)."
|
|
4068
4128
|
},
|
|
4069
4129
|
cardCvc: {
|
|
4070
4130
|
// 3 or 4 digit card security codes
|
|
4071
4131
|
pattern: /^\d{3,4}$/,
|
|
4072
|
-
errorMessage: "Please enter a valid CVC."
|
|
4132
|
+
errorMessage: "Please enter a valid CVC (3-4 digits)."
|
|
4073
4133
|
},
|
|
4074
4134
|
onlyLetters: {
|
|
4075
4135
|
// Letters only (including accented characters and spaces)
|
|
@@ -4080,15 +4140,150 @@ var Formatter = (() => {
|
|
|
4080
4140
|
// Letters and numbers (including accented characters and spaces)
|
|
4081
4141
|
pattern: /^[A-Za-zÁÉÍÓÚáéíóúÑñ0-9\s]+$/,
|
|
4082
4142
|
errorMessage: "Please enter only letters and numbers."
|
|
4143
|
+
},
|
|
4144
|
+
url: {
|
|
4145
|
+
// Accepts http, https, ftp
|
|
4146
|
+
pattern: /^(?:https?|ftp):\/\/[^\s]+$/,
|
|
4147
|
+
errorMessage: "Please enter a valid URL."
|
|
4148
|
+
},
|
|
4149
|
+
ipv4: {
|
|
4150
|
+
// IPv4 addresses (0-255 per octet)
|
|
4151
|
+
pattern: /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/,
|
|
4152
|
+
errorMessage: "Please enter a valid IPv4 address."
|
|
4153
|
+
},
|
|
4154
|
+
ipv6: {
|
|
4155
|
+
// IPv6 addresses (simplified)
|
|
4156
|
+
pattern: /^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$/,
|
|
4157
|
+
errorMessage: "Please enter a valid IPv6 address."
|
|
4158
|
+
},
|
|
4159
|
+
uuid: {
|
|
4160
|
+
// UUID v4
|
|
4161
|
+
pattern: /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,
|
|
4162
|
+
errorMessage: "Please enter a valid UUID."
|
|
4163
|
+
},
|
|
4164
|
+
hexColor: {
|
|
4165
|
+
// Hex color codes (#RGB or #RRGGBB)
|
|
4166
|
+
pattern: /^#(?:[0-9a-fA-F]{3}){1,2}$/,
|
|
4167
|
+
errorMessage: "Please enter a valid hex color code (e.g., #FFF or #FFFFFF)."
|
|
4168
|
+
},
|
|
4169
|
+
hashtag: {
|
|
4170
|
+
// Social media hashtags
|
|
4171
|
+
pattern: /^#[a-zA-Z_][a-zA-Z0-9_]*$/,
|
|
4172
|
+
errorMessage: "Please enter a valid hashtag (e.g., #example)."
|
|
4173
|
+
},
|
|
4174
|
+
mention: {
|
|
4175
|
+
// Social media mentions
|
|
4176
|
+
pattern: /^@[a-zA-Z_][a-zA-Z0-9_]*$/,
|
|
4177
|
+
errorMessage: "Please enter a valid mention (e.g., @username)."
|
|
4178
|
+
},
|
|
4179
|
+
postalCode: {
|
|
4180
|
+
// Generic 5-digit postal code (US-style ZIP / MX CP)
|
|
4181
|
+
pattern: /^\d{5}(?:-\d{4})?$/,
|
|
4182
|
+
errorMessage: "Please enter a valid postal code (e.g., 90210 or 90210-1234)."
|
|
4183
|
+
},
|
|
4184
|
+
time24: {
|
|
4185
|
+
// 24-hour time HH:MM
|
|
4186
|
+
pattern: /^([01]\d|2[0-3]):([0-5]\d)$/,
|
|
4187
|
+
errorMessage: "Please enter a valid 24-hour time (HH:MM)."
|
|
4188
|
+
},
|
|
4189
|
+
date: {
|
|
4190
|
+
// ISO-like date YYYY-MM-DD (loose)
|
|
4191
|
+
pattern: /^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/,
|
|
4192
|
+
errorMessage: "Please enter a valid date (YYYY-MM-DD)."
|
|
4193
|
+
},
|
|
4194
|
+
slug: {
|
|
4195
|
+
// URL slug (lowercase, hyphens, no spaces)
|
|
4196
|
+
pattern: /^[a-z0-9]+(?:-[a-z0-9]+)*$/,
|
|
4197
|
+
errorMessage: "Please enter a valid slug (lowercase, hyphens, no spaces)."
|
|
4198
|
+
},
|
|
4199
|
+
username: {
|
|
4200
|
+
// Username: 3-20 chars, alphanumeric, underscore, hyphen
|
|
4201
|
+
pattern: /^[a-zA-Z0-9_-]{3,20}$/,
|
|
4202
|
+
errorMessage: "Please enter a valid username (3-20 chars, letters, numbers, _ or -)."
|
|
4203
|
+
},
|
|
4204
|
+
macAddress: {
|
|
4205
|
+
// MAC address (colon or hyphen separated)
|
|
4206
|
+
pattern: /^([0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}$/,
|
|
4207
|
+
errorMessage: "Please enter a valid MAC address (e.g., 00:1B:44:11:3A:B7)."
|
|
4208
|
+
},
|
|
4209
|
+
semver: {
|
|
4210
|
+
// Semantic version (X.Y.Z with optional -prerelease)
|
|
4211
|
+
pattern: /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-[a-zA-Z0-9.-]+)?$/,
|
|
4212
|
+
errorMessage: "Please enter a valid semantic version (e.g., 1.2.3)."
|
|
4213
|
+
},
|
|
4214
|
+
base64: {
|
|
4215
|
+
// Base64 encoded string
|
|
4216
|
+
pattern: /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/,
|
|
4217
|
+
errorMessage: "Please enter a valid Base64 encoded string."
|
|
4083
4218
|
}
|
|
4084
4219
|
};
|
|
4220
|
+
function digits(params) {
|
|
4221
|
+
if (typeof params === "number") {
|
|
4222
|
+
return createDigits(params, params, `Please enter exactly ${params} digits.`);
|
|
4223
|
+
}
|
|
4224
|
+
const { length, min = 1, max = 20 } = params ?? {};
|
|
4225
|
+
const actualMin = length ?? min;
|
|
4226
|
+
const actualMax = length ?? max;
|
|
4227
|
+
if (actualMin === actualMax) {
|
|
4228
|
+
return createDigits(actualMin, actualMax, `Please enter exactly ${actualMin} digits.`);
|
|
4229
|
+
}
|
|
4230
|
+
return createDigits(actualMin, actualMax, `Please enter ${actualMin}-${actualMax} digits.`);
|
|
4231
|
+
}
|
|
4232
|
+
function _phoneFn(params) {
|
|
4233
|
+
const length = params?.length ?? 10;
|
|
4234
|
+
return createPhone(length);
|
|
4235
|
+
}
|
|
4236
|
+
function _creditCardFn(params) {
|
|
4237
|
+
const min = params?.min ?? 15;
|
|
4238
|
+
const max = params?.max ?? 16;
|
|
4239
|
+
return createCreditCard(min, max);
|
|
4240
|
+
}
|
|
4241
|
+
function _urlFn(params) {
|
|
4242
|
+
const protocol = params?.protocol ?? "all";
|
|
4243
|
+
return createUrl(protocol);
|
|
4244
|
+
}
|
|
4245
|
+
function _passwordFn(params) {
|
|
4246
|
+
return createPassword(params ?? {});
|
|
4247
|
+
}
|
|
4248
|
+
function _customFn(arg1, arg2) {
|
|
4249
|
+
if (arg1 instanceof RegExp) {
|
|
4250
|
+
return { pattern: arg1, errorMessage: arg2 ?? "Invalid value." };
|
|
4251
|
+
}
|
|
4252
|
+
return { pattern: arg1.pattern, errorMessage: arg1.errorMessage };
|
|
4253
|
+
}
|
|
4254
|
+
var phoneFn = _phoneFn;
|
|
4255
|
+
phoneFn.pattern = _regex.phone.pattern;
|
|
4256
|
+
phoneFn.errorMessage = _regex.phone.errorMessage;
|
|
4257
|
+
var creditCardFn = _creditCardFn;
|
|
4258
|
+
creditCardFn.pattern = _regex.creditCard.pattern;
|
|
4259
|
+
creditCardFn.errorMessage = _regex.creditCard.errorMessage;
|
|
4260
|
+
var urlFn = _urlFn;
|
|
4261
|
+
urlFn.pattern = _regex.url.pattern;
|
|
4262
|
+
urlFn.errorMessage = _regex.url.errorMessage;
|
|
4263
|
+
var digitsFn = digits;
|
|
4264
|
+
digitsFn.errorMessage = "Please enter a valid number.";
|
|
4265
|
+
var passwordFn = _passwordFn;
|
|
4266
|
+
passwordFn.errorMessage = "Please enter a valid password.";
|
|
4267
|
+
var customFn = _customFn;
|
|
4268
|
+
customFn.errorMessage = "Please enter a valid value.";
|
|
4269
|
+
var regex = {
|
|
4270
|
+
// Static entries (backward compatible - spread first)
|
|
4271
|
+
..._regex,
|
|
4272
|
+
// Parametric functions
|
|
4273
|
+
digits: digitsFn,
|
|
4274
|
+
phone: phoneFn,
|
|
4275
|
+
creditCard: creditCardFn,
|
|
4276
|
+
url: urlFn,
|
|
4277
|
+
password: passwordFn,
|
|
4278
|
+
custom: customFn
|
|
4279
|
+
};
|
|
4085
4280
|
|
|
4086
4281
|
// src/browser/global.ts
|
|
4087
4282
|
var Formatter = {
|
|
4088
4283
|
format,
|
|
4089
4284
|
regex,
|
|
4090
4285
|
// Keep in sync with `version` in `package.json`.
|
|
4091
|
-
version: "1.0
|
|
4286
|
+
version: "1.1.0"
|
|
4092
4287
|
};
|
|
4093
4288
|
if (typeof window !== "undefined") {
|
|
4094
4289
|
window.Formatter = Formatter;
|