@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
package/README.md
CHANGED
|
@@ -41,15 +41,15 @@ Requires Node 20+ when bundling. Runtime target is ES2020.
|
|
|
41
41
|
Use the browser build when you do not have a bundler and need to run the package directly in HTML, Shopify, WordPress, or any traditional template.
|
|
42
42
|
|
|
43
43
|
```html
|
|
44
|
-
<script src="https://unpkg.com/@samline/formatter@1.0
|
|
44
|
+
<script src="https://unpkg.com/@samline/formatter@1.1.0/dist/browser/global.global.js"></script>
|
|
45
45
|
```
|
|
46
46
|
|
|
47
|
-
> Pin the version in production. Replace `1.0
|
|
47
|
+
> Pin the version in production. Replace `1.1.0` with the version you ship.
|
|
48
48
|
|
|
49
49
|
The browser bundle exposes a single global: `window.Formatter`.
|
|
50
50
|
|
|
51
51
|
```html
|
|
52
|
-
<script src="https://unpkg.com/@samline/formatter@1.0
|
|
52
|
+
<script src="https://unpkg.com/@samline/formatter@1.1.0/dist/browser/global.global.js"></script>
|
|
53
53
|
<script>
|
|
54
54
|
const result = window.Formatter.format('5512345678', 'phone')
|
|
55
55
|
console.log(result.formatted) // '55 1234 5678'
|
|
@@ -111,7 +111,9 @@ format('4111111111111111', 'creditCardType')
|
|
|
111
111
|
| `'creditCard'` | Brand-aware grouping | `'4111 1111 1111 1111'`, digits-only raw |
|
|
112
112
|
| `'creditCardType'` | Returns the card brand name | Brand string, digits-only raw from input |
|
|
113
113
|
|
|
114
|
-
|
|
114
|
+
## Documentation
|
|
115
|
+
|
|
116
|
+
Full API reference, guides, and examples are available at **[samline.github.io/formatter](https://samline.github.io/formatter)**.
|
|
115
117
|
|
|
116
118
|
| Doc | Purpose |
|
|
117
119
|
| --- | --- |
|
package/dist/browser/global.d.ts
CHANGED
|
@@ -1,10 +1,149 @@
|
|
|
1
1
|
import { FormatGeneralOptions, FormatNumeralOptions, FormatDateOptions, FormatTimeOptions, FormatCreditCardOptions, DatePatternType, TimePatternType } from 'cleave-zen';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* # Regex Module
|
|
5
|
+
*
|
|
6
|
+
* Validation patterns exposed as public API for form consumers that pair
|
|
7
|
+
* formatting with validation. Each entry bundles the regex with a ready-to-use
|
|
8
|
+
* error message so consumers do not have to keep them in sync.
|
|
9
|
+
*
|
|
10
|
+
* ## Usage
|
|
11
|
+
*
|
|
12
|
+
* **Static (backward compatible):**
|
|
13
|
+
* ```ts
|
|
14
|
+
* regex.phone.pattern.test('5512345678') // true
|
|
15
|
+
* regex.email.errorMessage // 'Please enter a valid email address.'
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* **Parametric (dynamic):**
|
|
19
|
+
* ```ts
|
|
20
|
+
* regex.digits(10).pattern.test('1234567890') // true
|
|
21
|
+
* regex.digits(7).pattern.test('1234567') // true
|
|
22
|
+
* regex.phone({ length: 10 }).pattern.test('1234567890')
|
|
23
|
+
* regex.phone({ length: 7 }).pattern.test('1234567')
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* **Custom regex:**
|
|
27
|
+
* ```ts
|
|
28
|
+
* regex.custom(/^[A-Z]{5}$/, 'Must be 5 uppercase letters').pattern.test('HELLO')
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* ## Available Patterns
|
|
32
|
+
*
|
|
33
|
+
* | Key | Static | Parametric | Description |
|
|
34
|
+
* | --- | --- | --- | --- |
|
|
35
|
+
* | `phone` | ✅ | ✅ `({ length })` | Phone numbers |
|
|
36
|
+
* | `email` | ✅ | — | Email addresses |
|
|
37
|
+
* | `rfc` | ✅ | — | Mexican RFC |
|
|
38
|
+
* | `curp` | ✅ | — | Mexican CURP |
|
|
39
|
+
* | `cp` | ✅ | — | Mexican postal code (5 digits) |
|
|
40
|
+
* | `numeral` | ✅ | — | Numbers with separators |
|
|
41
|
+
* | `onlyNumbers` | ✅ | ✅ `({ length })` | Digits only |
|
|
42
|
+
* | `digits` | — | ✅ `({ length, min, max })` | Variable digit count |
|
|
43
|
+
* | `creditCard` | ✅ | ✅ `({ min, max })` | Card numbers |
|
|
44
|
+
* | `expirationDate` | ✅ | — | MM/YY format |
|
|
45
|
+
* | `cardCvc` | ✅ | — | 3-4 digit CVC |
|
|
46
|
+
* | `onlyLetters` | ✅ | — | Letters only |
|
|
47
|
+
* | `onlyAlphanumeric` | ✅ | — | Letters and numbers |
|
|
48
|
+
* | `url` | ✅ | ✅ `({ protocol })` | URLs |
|
|
49
|
+
* | `ipv4` | ✅ | — | IPv4 addresses |
|
|
50
|
+
* | `ipv6` | ✅ | — | IPv6 addresses |
|
|
51
|
+
* | `uuid` | ✅ | — | UUID v4 |
|
|
52
|
+
* | `hexColor` | ✅ | — | Hex color codes |
|
|
53
|
+
* | `hashtag` | ✅ | — | Social media hashtags |
|
|
54
|
+
* | `mention` | ✅ | — | Social media mentions |
|
|
55
|
+
* | `password` | — | ✅ `({ min, max, rules })` | Password strength |
|
|
56
|
+
* | `custom` | — | ✅ `({ pattern, errorMessage })` | User-defined regex |
|
|
57
|
+
*/
|
|
58
|
+
interface RegexEntry {
|
|
59
|
+
pattern: RegExp;
|
|
60
|
+
errorMessage: string;
|
|
61
|
+
}
|
|
62
|
+
interface PhoneParams {
|
|
63
|
+
length?: number;
|
|
64
|
+
}
|
|
65
|
+
interface DigitsParams {
|
|
66
|
+
length?: number;
|
|
67
|
+
min?: number;
|
|
68
|
+
max?: number;
|
|
69
|
+
}
|
|
70
|
+
interface CreditCardParams {
|
|
71
|
+
min?: number;
|
|
72
|
+
max?: number;
|
|
73
|
+
}
|
|
74
|
+
interface UrlParams {
|
|
75
|
+
protocol?: 'http' | 'https' | 'ftp' | 'all';
|
|
76
|
+
}
|
|
77
|
+
interface PasswordParams {
|
|
78
|
+
min?: number;
|
|
79
|
+
max?: number;
|
|
80
|
+
uppercase?: boolean;
|
|
81
|
+
lowercase?: boolean;
|
|
82
|
+
numbers?: boolean;
|
|
83
|
+
special?: boolean;
|
|
84
|
+
}
|
|
85
|
+
interface CustomParams {
|
|
86
|
+
pattern: RegExp;
|
|
87
|
+
errorMessage: string;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Digits with configurable length.
|
|
91
|
+
* @example
|
|
92
|
+
* regex.digits(10).pattern.test('1234567890') // true
|
|
93
|
+
* regex.digits({ length: 7 }).pattern.test('1234567') // true
|
|
94
|
+
* regex.digits({ min: 3, max: 10 }).pattern.test('12345') // true
|
|
95
|
+
*/
|
|
96
|
+
declare function digits(params?: number | DigitsParams): RegexEntry;
|
|
97
|
+
/**
|
|
98
|
+
* Phone with configurable digit count.
|
|
99
|
+
*
|
|
100
|
+
* Also available as static entry: regex.phone.pattern (10 digits)
|
|
101
|
+
* @example
|
|
102
|
+
* regex.phone().pattern.test('5512345678') // true (10 digits)
|
|
103
|
+
* regex.phone({ length: 7 }).pattern.test('1234567') // true (7 digits)
|
|
104
|
+
*/
|
|
105
|
+
declare function _phoneFn(params?: PhoneParams): RegexEntry;
|
|
106
|
+
/**
|
|
107
|
+
* Credit card with configurable digit range.
|
|
108
|
+
*
|
|
109
|
+
* Also available as static entry: regex.creditCard.pattern (15-16 digits)
|
|
110
|
+
* @example
|
|
111
|
+
* regex.creditCard().pattern.test('4111111111111111') // true (15-16)
|
|
112
|
+
* regex.creditCard({ min: 13, max: 19 }).pattern.test('1234567890123') // true
|
|
113
|
+
*/
|
|
114
|
+
declare function _creditCardFn(params?: CreditCardParams): RegexEntry;
|
|
115
|
+
/**
|
|
116
|
+
* URL with configurable protocol.
|
|
117
|
+
*
|
|
118
|
+
* Also available as static entry: regex.url.pattern (any protocol)
|
|
119
|
+
* @example
|
|
120
|
+
* regex.url().pattern.test('https://example.com') // true
|
|
121
|
+
* regex.url({ protocol: 'https' }).pattern.test('https://example.com') // true
|
|
122
|
+
* regex.url({ protocol: 'https' }).pattern.test('ftp://example.com') // false
|
|
123
|
+
*/
|
|
124
|
+
declare function _urlFn(params?: UrlParams): RegexEntry;
|
|
125
|
+
/**
|
|
126
|
+
* Password with configurable strength rules.
|
|
127
|
+
* @example
|
|
128
|
+
* regex.password().pattern.test('Passw0rd!') // true (default: 8+ chars, upper, lower, number)
|
|
129
|
+
* regex.password({ min: 12, special: true }).pattern.test('MyP@ssw0rd!') // true
|
|
130
|
+
*/
|
|
131
|
+
declare function _passwordFn(params?: PasswordParams): RegexEntry;
|
|
132
|
+
/**
|
|
133
|
+
* Custom regex provided by the user.
|
|
134
|
+
* @example
|
|
135
|
+
* regex.custom(/^[A-Z]{5}$/, 'Must be 5 uppercase letters').pattern.test('HELLO') // true
|
|
136
|
+
* regex.custom({ pattern: /^\d+$/, errorMessage: 'Numbers only' }).pattern.test('123') // true
|
|
137
|
+
*/
|
|
138
|
+
declare function _customFn(pattern: RegExp, errorMessage?: string): RegexEntry;
|
|
139
|
+
declare function _customFn(params: CustomParams): RegexEntry;
|
|
3
140
|
declare const regex: {
|
|
4
|
-
readonly
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
141
|
+
readonly digits: typeof digits & RegexEntry;
|
|
142
|
+
readonly phone: typeof _phoneFn & RegexEntry;
|
|
143
|
+
readonly creditCard: typeof _creditCardFn & RegexEntry;
|
|
144
|
+
readonly url: typeof _urlFn & RegexEntry;
|
|
145
|
+
readonly password: typeof _passwordFn & RegexEntry;
|
|
146
|
+
readonly custom: typeof _customFn & RegexEntry;
|
|
8
147
|
readonly email: {
|
|
9
148
|
readonly pattern: RegExp;
|
|
10
149
|
readonly errorMessage: "Please enter a valid email address.";
|
|
@@ -13,6 +152,14 @@ declare const regex: {
|
|
|
13
152
|
readonly pattern: RegExp;
|
|
14
153
|
readonly errorMessage: "Please enter a valid RFC.";
|
|
15
154
|
};
|
|
155
|
+
readonly curp: {
|
|
156
|
+
readonly pattern: RegExp;
|
|
157
|
+
readonly errorMessage: "Please enter a valid CURP.";
|
|
158
|
+
};
|
|
159
|
+
readonly cp: {
|
|
160
|
+
readonly pattern: RegExp;
|
|
161
|
+
readonly errorMessage: "Please enter a valid 5-digit postal code.";
|
|
162
|
+
};
|
|
16
163
|
readonly numeral: {
|
|
17
164
|
readonly pattern: RegExp;
|
|
18
165
|
readonly errorMessage: "Please enter a valid number.";
|
|
@@ -21,17 +168,13 @@ declare const regex: {
|
|
|
21
168
|
readonly pattern: RegExp;
|
|
22
169
|
readonly errorMessage: "Please enter only numbers.";
|
|
23
170
|
};
|
|
24
|
-
readonly creditCard: {
|
|
25
|
-
readonly pattern: RegExp;
|
|
26
|
-
readonly errorMessage: "Please enter a valid card number.";
|
|
27
|
-
};
|
|
28
171
|
readonly expirationDate: {
|
|
29
172
|
readonly pattern: RegExp;
|
|
30
|
-
readonly errorMessage: "Please enter a valid expiration date.";
|
|
173
|
+
readonly errorMessage: "Please enter a valid expiration date (MM/YY or MM/YYYY).";
|
|
31
174
|
};
|
|
32
175
|
readonly cardCvc: {
|
|
33
176
|
readonly pattern: RegExp;
|
|
34
|
-
readonly errorMessage: "Please enter a valid CVC.";
|
|
177
|
+
readonly errorMessage: "Please enter a valid CVC (3-4 digits).";
|
|
35
178
|
};
|
|
36
179
|
readonly onlyLetters: {
|
|
37
180
|
readonly pattern: RegExp;
|
|
@@ -41,6 +184,62 @@ declare const regex: {
|
|
|
41
184
|
readonly pattern: RegExp;
|
|
42
185
|
readonly errorMessage: "Please enter only letters and numbers.";
|
|
43
186
|
};
|
|
187
|
+
readonly ipv4: {
|
|
188
|
+
readonly pattern: RegExp;
|
|
189
|
+
readonly errorMessage: "Please enter a valid IPv4 address.";
|
|
190
|
+
};
|
|
191
|
+
readonly ipv6: {
|
|
192
|
+
readonly pattern: RegExp;
|
|
193
|
+
readonly errorMessage: "Please enter a valid IPv6 address.";
|
|
194
|
+
};
|
|
195
|
+
readonly uuid: {
|
|
196
|
+
readonly pattern: RegExp;
|
|
197
|
+
readonly errorMessage: "Please enter a valid UUID.";
|
|
198
|
+
};
|
|
199
|
+
readonly hexColor: {
|
|
200
|
+
readonly pattern: RegExp;
|
|
201
|
+
readonly errorMessage: "Please enter a valid hex color code (e.g., #FFF or #FFFFFF).";
|
|
202
|
+
};
|
|
203
|
+
readonly hashtag: {
|
|
204
|
+
readonly pattern: RegExp;
|
|
205
|
+
readonly errorMessage: "Please enter a valid hashtag (e.g., #example).";
|
|
206
|
+
};
|
|
207
|
+
readonly mention: {
|
|
208
|
+
readonly pattern: RegExp;
|
|
209
|
+
readonly errorMessage: "Please enter a valid mention (e.g., @username).";
|
|
210
|
+
};
|
|
211
|
+
readonly postalCode: {
|
|
212
|
+
readonly pattern: RegExp;
|
|
213
|
+
readonly errorMessage: "Please enter a valid postal code (e.g., 90210 or 90210-1234).";
|
|
214
|
+
};
|
|
215
|
+
readonly time24: {
|
|
216
|
+
readonly pattern: RegExp;
|
|
217
|
+
readonly errorMessage: "Please enter a valid 24-hour time (HH:MM).";
|
|
218
|
+
};
|
|
219
|
+
readonly date: {
|
|
220
|
+
readonly pattern: RegExp;
|
|
221
|
+
readonly errorMessage: "Please enter a valid date (YYYY-MM-DD).";
|
|
222
|
+
};
|
|
223
|
+
readonly slug: {
|
|
224
|
+
readonly pattern: RegExp;
|
|
225
|
+
readonly errorMessage: "Please enter a valid slug (lowercase, hyphens, no spaces).";
|
|
226
|
+
};
|
|
227
|
+
readonly username: {
|
|
228
|
+
readonly pattern: RegExp;
|
|
229
|
+
readonly errorMessage: "Please enter a valid username (3-20 chars, letters, numbers, _ or -).";
|
|
230
|
+
};
|
|
231
|
+
readonly macAddress: {
|
|
232
|
+
readonly pattern: RegExp;
|
|
233
|
+
readonly errorMessage: "Please enter a valid MAC address (e.g., 00:1B:44:11:3A:B7).";
|
|
234
|
+
};
|
|
235
|
+
readonly semver: {
|
|
236
|
+
readonly pattern: RegExp;
|
|
237
|
+
readonly errorMessage: "Please enter a valid semantic version (e.g., 1.2.3).";
|
|
238
|
+
};
|
|
239
|
+
readonly base64: {
|
|
240
|
+
readonly pattern: RegExp;
|
|
241
|
+
readonly errorMessage: "Please enter a valid Base64 encoded string.";
|
|
242
|
+
};
|
|
44
243
|
};
|
|
45
244
|
|
|
46
245
|
type FormatType = 'general' | 'phone' | 'numeral' | 'date' | 'time' | 'creditCard' | 'creditCardType';
|