@samline/formatter 1.0.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/LICENSE +21 -0
- package/README.md +128 -0
- package/dist/browser/global.d.ts +84 -0
- package/dist/browser/global.global.js +4098 -0
- package/dist/index.cjs +308 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +158 -0
- package/dist/index.d.ts +158 -0
- package/dist/index.js +295 -0
- package/dist/index.js.map +1 -0
- package/dist/vanilla/index.cjs +308 -0
- package/dist/vanilla/index.cjs.map +1 -0
- package/dist/vanilla/index.d.cts +158 -0
- package/dist/vanilla/index.d.ts +158 -0
- package/dist/vanilla/index.js +295 -0
- package/dist/vanilla/index.js.map +1 -0
- package/package.json +79 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Samuel Olvera
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
# Formatter
|
|
2
|
+
|
|
3
|
+
> Lightweight, framework-agnostic input formatter that returns split formatted/raw outputs.
|
|
4
|
+
|
|
5
|
+
> It formats strings for input fields (phones, numerals, dates, times, credit cards, generic masked inputs) and returns **two strings** in one call: the display-ready `formatted` value and the backend-ready `raw` value. The function is pure (no DOM, no I/O) so it works in Node, in React, in Vue, in Svelte, in Shopify themes, or anywhere JavaScript runs.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## Table of Contents
|
|
10
|
+
|
|
11
|
+
- [Installation](#installation)
|
|
12
|
+
- [CDN / Browser](#cdn--browser)
|
|
13
|
+
- [Entrypoints](#entrypoints)
|
|
14
|
+
- [Quick Start](#quick-start)
|
|
15
|
+
- [Supported format types](#supported-format-types)
|
|
16
|
+
- [Documentation](#documentation)
|
|
17
|
+
- [License](#license)
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
## Installation
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
npm install @samline/formatter
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
pnpm add @samline/formatter
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
bun add @samline/formatter
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Requires Node 20+ when bundling. Runtime target is ES2020.
|
|
36
|
+
|
|
37
|
+
---
|
|
38
|
+
|
|
39
|
+
## CDN / Browser
|
|
40
|
+
|
|
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
|
+
|
|
43
|
+
```html
|
|
44
|
+
<script src="https://unpkg.com/@samline/formatter@1.0.0/dist/browser/global.global.js"></script>
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
> Pin the version in production. Replace `1.0.0` with the version you ship.
|
|
48
|
+
|
|
49
|
+
The browser bundle exposes a single global: `window.Formatter`.
|
|
50
|
+
|
|
51
|
+
```html
|
|
52
|
+
<script src="https://unpkg.com/@samline/formatter@1.0.0/dist/browser/global.global.js"></script>
|
|
53
|
+
<script>
|
|
54
|
+
const result = window.Formatter.format('5512345678', 'phone')
|
|
55
|
+
console.log(result.formatted) // '55 1234 5678'
|
|
56
|
+
console.log(result.raw) // '5512345678'
|
|
57
|
+
</script>
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
See [docs/browser.md](docs/browser.md) for the full browser surface.
|
|
61
|
+
|
|
62
|
+
---
|
|
63
|
+
|
|
64
|
+
## Entrypoints
|
|
65
|
+
|
|
66
|
+
| Entrypoint | When to use |
|
|
67
|
+
| --- | --- |
|
|
68
|
+
| `@samline/formatter` | Main API for bundlers, ESM, or CJS consumers. |
|
|
69
|
+
| `@samline/formatter/vanilla` | Direct re-export of the base utility for non-framework consumers. |
|
|
70
|
+
| `@samline/formatter/browser` | Pre-bundled IIFE that registers `window.Formatter` for direct `<script>` usage. |
|
|
71
|
+
|
|
72
|
+
---
|
|
73
|
+
|
|
74
|
+
## Quick Start
|
|
75
|
+
|
|
76
|
+
```ts
|
|
77
|
+
import { format } from '@samline/formatter'
|
|
78
|
+
|
|
79
|
+
// Phone (Mexico by default, space-delimited)
|
|
80
|
+
format('5512345678', 'phone')
|
|
81
|
+
// => { formatted: '55 1234 5678', raw: '5512345678', type: 'phone' }
|
|
82
|
+
|
|
83
|
+
// Numeral (thousand separators)
|
|
84
|
+
format('1234567', 'numeral')
|
|
85
|
+
// => { formatted: '1,234,567', raw: '1234567', type: 'numeral' }
|
|
86
|
+
|
|
87
|
+
// Date (raw Y-m-d -> display d/m/Y)
|
|
88
|
+
format('2026-05-12', 'date')
|
|
89
|
+
// => { formatted: '12/05/2026', raw: '2026-05-12', type: 'date' }
|
|
90
|
+
|
|
91
|
+
// Credit card (grouped by brand, digits-only raw)
|
|
92
|
+
format('4111111111111111', 'creditCard')
|
|
93
|
+
// => { formatted: '4111 1111 1111 1111', raw: '4111111111111111', type: 'creditCard' }
|
|
94
|
+
|
|
95
|
+
// Card brand detection
|
|
96
|
+
format('4111111111111111', 'creditCardType')
|
|
97
|
+
// => { formatted: 'visa', raw: '4111111111111111', type: 'creditCardType' }
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
---
|
|
101
|
+
|
|
102
|
+
## Supported format types
|
|
103
|
+
|
|
104
|
+
| `formatType` | What it does | Default output |
|
|
105
|
+
| --- | --- | --- |
|
|
106
|
+
| `'general'` | Block-based masking with custom delimiter/delimiters | Digits-only raw |
|
|
107
|
+
| `'phone'` | Country-aware phone formatting via `libphonenumber-js` | Space-delimited, digits-only raw (preserves leading `+`) |
|
|
108
|
+
| `'numeral'` | Thousand separators, optional decimals | `'1,234.56'` style raw |
|
|
109
|
+
| `'date'` | Raw `Y-m-d` → display pattern | `'12/05/2026'`, raw `'2026-05-12'` |
|
|
110
|
+
| `'time'` | Raw `h:m` → display pattern | `'14:30:00'`, raw `'14:30'` |
|
|
111
|
+
| `'creditCard'` | Brand-aware grouping | `'4111 1111 1111 1111'`, digits-only raw |
|
|
112
|
+
| `'creditCardType'` | Returns the card brand name | Brand string, digits-only raw from input |
|
|
113
|
+
|
|
114
|
+
For the full options reference and per-type behavior, see:
|
|
115
|
+
|
|
116
|
+
| Doc | Purpose |
|
|
117
|
+
| --- | --- |
|
|
118
|
+
| [docs/getting-started.md](docs/getting-started.md) | Concepts, observable contract, and lifecycle overview. |
|
|
119
|
+
| [docs/options.md](docs/options.md) | Full `FormatOptions` reference. |
|
|
120
|
+
| [docs/typescript.md](docs/typescript.md) | Every exported TypeScript type, with examples. |
|
|
121
|
+
| [docs/vanilla.md](docs/vanilla.md) | Vanilla surface for non-framework consumers. |
|
|
122
|
+
| [docs/browser.md](docs/browser.md) | Browser global (`window.Formatter`) usage. |
|
|
123
|
+
|
|
124
|
+
---
|
|
125
|
+
|
|
126
|
+
## License
|
|
127
|
+
|
|
128
|
+
MIT
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { FormatGeneralOptions, FormatNumeralOptions, FormatDateOptions, FormatTimeOptions, FormatCreditCardOptions, DatePatternType, TimePatternType } from 'cleave-zen';
|
|
2
|
+
|
|
3
|
+
declare const regex: {
|
|
4
|
+
readonly phone: {
|
|
5
|
+
readonly pattern: RegExp;
|
|
6
|
+
readonly errorMessage: "Please enter a valid 10-digit phone number.";
|
|
7
|
+
};
|
|
8
|
+
readonly email: {
|
|
9
|
+
readonly pattern: RegExp;
|
|
10
|
+
readonly errorMessage: "Please enter a valid email address.";
|
|
11
|
+
};
|
|
12
|
+
readonly rfc: {
|
|
13
|
+
readonly pattern: RegExp;
|
|
14
|
+
readonly errorMessage: "Please enter a valid RFC.";
|
|
15
|
+
};
|
|
16
|
+
readonly numeral: {
|
|
17
|
+
readonly pattern: RegExp;
|
|
18
|
+
readonly errorMessage: "Please enter a valid number.";
|
|
19
|
+
};
|
|
20
|
+
readonly onlyNumbers: {
|
|
21
|
+
readonly pattern: RegExp;
|
|
22
|
+
readonly errorMessage: "Please enter only numbers.";
|
|
23
|
+
};
|
|
24
|
+
readonly creditCard: {
|
|
25
|
+
readonly pattern: RegExp;
|
|
26
|
+
readonly errorMessage: "Please enter a valid card number.";
|
|
27
|
+
};
|
|
28
|
+
readonly expirationDate: {
|
|
29
|
+
readonly pattern: RegExp;
|
|
30
|
+
readonly errorMessage: "Please enter a valid expiration date.";
|
|
31
|
+
};
|
|
32
|
+
readonly cardCvc: {
|
|
33
|
+
readonly pattern: RegExp;
|
|
34
|
+
readonly errorMessage: "Please enter a valid CVC.";
|
|
35
|
+
};
|
|
36
|
+
readonly onlyLetters: {
|
|
37
|
+
readonly pattern: RegExp;
|
|
38
|
+
readonly errorMessage: "Please enter only letters.";
|
|
39
|
+
};
|
|
40
|
+
readonly onlyAlphanumeric: {
|
|
41
|
+
readonly pattern: RegExp;
|
|
42
|
+
readonly errorMessage: "Please enter only letters and numbers.";
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
type FormatType = 'general' | 'phone' | 'numeral' | 'date' | 'time' | 'creditCard' | 'creditCardType';
|
|
47
|
+
type ExtraFormatOptions = {
|
|
48
|
+
country?: string;
|
|
49
|
+
dateRawPattern?: DatePatternType;
|
|
50
|
+
dateRawPatternDelimiter?: string;
|
|
51
|
+
timeRawPattern?: TimePatternType;
|
|
52
|
+
timeRawPatternDelimiter?: string;
|
|
53
|
+
tailPrefix?: boolean;
|
|
54
|
+
};
|
|
55
|
+
type FormatOptions = Partial<FormatGeneralOptions & FormatNumeralOptions & FormatDateOptions & FormatTimeOptions & FormatCreditCardOptions> & ExtraFormatOptions;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Result returned by `format()`. `formatted` is the display string
|
|
59
|
+
* (with delimiters, prefix, etc.); `raw` is the canonical backend-ready
|
|
60
|
+
* representation (digits only, no separators); `type` echoes the requested
|
|
61
|
+
* format type for chaining or debugging.
|
|
62
|
+
*/
|
|
63
|
+
interface FormatterResult {
|
|
64
|
+
readonly formatted: string;
|
|
65
|
+
readonly raw: string;
|
|
66
|
+
readonly type: FormatType;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
interface FormatterGlobal {
|
|
70
|
+
/** Format any value using the core formatter. */
|
|
71
|
+
format: (value: unknown, formatType: FormatType, options?: FormatOptions) => FormatterResult;
|
|
72
|
+
/** Validation regex patterns with paired error messages. */
|
|
73
|
+
regex: typeof regex;
|
|
74
|
+
/** Library version, mirrors the `version` field in `package.json`. */
|
|
75
|
+
version: string;
|
|
76
|
+
}
|
|
77
|
+
declare global {
|
|
78
|
+
interface Window {
|
|
79
|
+
Formatter?: FormatterGlobal;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
declare const Formatter: FormatterGlobal;
|
|
83
|
+
|
|
84
|
+
export { type FormatterGlobal, Formatter as default };
|