mother-mask 2.0.4 → 2.0.5
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 +57 -57
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
# mother-mask
|
|
2
2
|
|
|
3
|
-
Lightweight input mask library for browsers. Zero dependencies, TypeScript-first, ships ESM
|
|
3
|
+
Lightweight input mask library for browsers. Zero runtime dependencies, TypeScript-first, ships **ESM**, **CJS**, and **UMD**.
|
|
4
|
+
|
|
5
|
+
Published as [`mother-mask` on npm](https://www.npmjs.com/package/mother-mask).
|
|
6
|
+
|
|
7
|
+
## Live demo
|
|
8
|
+
|
|
9
|
+
**[Try it on StackBlitz →](https://stackblitz.com/edit/mother-mask-simple-demo?file=src%2Fmain.ts)**
|
|
4
10
|
|
|
5
11
|
## Install
|
|
6
12
|
|
|
@@ -12,9 +18,13 @@ pnpm add mother-mask
|
|
|
12
18
|
|
|
13
19
|
## Usage
|
|
14
20
|
|
|
15
|
-
### `bind(input, mask,
|
|
21
|
+
### `bind(input, mask, options?)`
|
|
16
22
|
|
|
17
|
-
Attach a mask to any input element
|
|
23
|
+
Attach a mask to any input element — this is the main API.
|
|
24
|
+
|
|
25
|
+
- **Idempotent** — calling `bind()` again on the same element does nothing (the element is marked with `data-masked`).
|
|
26
|
+
- **Returns a dispose function** — call it to remove listeners and attributes so you can bind again later.
|
|
27
|
+
- Sets sensible defaults when missing: `autocomplete`, `autocorrect`, `autocapitalize`, `spellcheck`, and `maxlength` from the mask.
|
|
18
28
|
|
|
19
29
|
```ts
|
|
20
30
|
import { bind } from 'mother-mask'
|
|
@@ -22,48 +32,38 @@ import { bind } from 'mother-mask'
|
|
|
22
32
|
const input = document.getElementById('phone') as HTMLInputElement
|
|
23
33
|
|
|
24
34
|
// Fixed mask
|
|
25
|
-
bind(input, '(99) 99999-9999')
|
|
35
|
+
const dispose = bind(input, '(99) 99999-9999')
|
|
26
36
|
|
|
27
|
-
// Dynamic mask —
|
|
37
|
+
// Dynamic mask — picks the pattern from an ordered list (shortest → longest)
|
|
28
38
|
bind(input, ['(99) 9999-9999', '(99) 99999-9999'])
|
|
29
39
|
|
|
30
|
-
//
|
|
40
|
+
// Callback after paste or keyboard-driven changes
|
|
31
41
|
bind(input, '999.999.999-99', (value) => {
|
|
32
42
|
console.log(value) // e.g. "123.456.789-01"
|
|
33
43
|
})
|
|
34
|
-
```
|
|
35
44
|
|
|
36
|
-
|
|
45
|
+
// Or options object (same as callback for a single `onChange`)
|
|
46
|
+
bind(input, '999.999.999-99', { onChange: (value) => console.log(value) })
|
|
37
47
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
```ts
|
|
41
|
-
import { process } from 'mother-mask'
|
|
42
|
-
|
|
43
|
-
process('12345678901', '999.999.999-99') // → "123.456.789-01"
|
|
44
|
-
process('01012024', '99/99/9999') // → "01/01/2024"
|
|
45
|
-
process('01310100', '99999-999') // → "01310-100"
|
|
46
|
-
process('1AB2C3D45E6F78', 'AA.AAA.AAA/AAAA-99') // → "1A.B2C.3D4/5E6F-78"
|
|
48
|
+
// Later: allow rebinding
|
|
49
|
+
dispose()
|
|
47
50
|
```
|
|
48
51
|
|
|
49
52
|
## Pattern syntax
|
|
50
53
|
|
|
51
|
-
| Character | Matches
|
|
52
|
-
|
|
53
|
-
| `9`
|
|
54
|
-
| `Z`
|
|
55
|
-
| `A`
|
|
56
|
-
|
|
|
54
|
+
| Character | Matches |
|
|
55
|
+
|-----------|---------|
|
|
56
|
+
| `9` | Digit (`0`–`9`) |
|
|
57
|
+
| `Z` | Letter (`a`–`z`, `A`–`Z`) |
|
|
58
|
+
| `A` | Alphanumeric (digit or letter) |
|
|
59
|
+
| Anything else | Literal — inserted as the user fills slots |
|
|
57
60
|
|
|
58
61
|
## Array masks
|
|
59
62
|
|
|
60
|
-
Pass an ordered array
|
|
63
|
+
Pass an ordered array **shortest → longest** for variable-length inputs. The active mask is chosen from the **count of alphanumeric “data” characters** in the current value, so it works for both progressively masked input and fast typing.
|
|
61
64
|
|
|
62
65
|
```ts
|
|
63
|
-
// Brazilian phone: 8-digit → 9-digit landline / mobile
|
|
64
66
|
bind(input, ['(99) 9999-9999', '(99) 99999-9999'])
|
|
65
|
-
|
|
66
|
-
// CPF / CNPJ alfanumérico
|
|
67
67
|
bind(input, ['999.999.999-99', 'AA.AAA.AAA/AAAA-99'])
|
|
68
68
|
```
|
|
69
69
|
|
|
@@ -72,51 +72,51 @@ bind(input, ['999.999.999-99', 'AA.AAA.AAA/AAAA-99'])
|
|
|
72
72
|
```html
|
|
73
73
|
<script src="https://unpkg.com/mother-mask/dist/mother-mask.umd.js"></script>
|
|
74
74
|
<script>
|
|
75
|
-
MotherMask.bind(document.getElementById('cpf'), '999.999.999-99')
|
|
75
|
+
const dispose = MotherMask.bind(document.getElementById('cpf'), '999.999.999-99')
|
|
76
76
|
</script>
|
|
77
77
|
```
|
|
78
78
|
|
|
79
|
+
The global name is **`MotherMask`**.
|
|
80
|
+
|
|
79
81
|
## API reference
|
|
80
82
|
|
|
81
|
-
|
|
82
|
-
// Apply mask to a string — no DOM required
|
|
83
|
-
process(value: string, mask: MaskPattern): string
|
|
83
|
+
### `bind` (primary)
|
|
84
84
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
): void
|
|
85
|
+
| | |
|
|
86
|
+
|--|--|
|
|
87
|
+
| **Signature** | `bind(input, mask, options?)` |
|
|
88
|
+
| **Returns** | `() => void` — call to remove listeners and attributes so the input can be bound again |
|
|
89
|
+
| **Third argument** | `{ onChange?: (value: string) => void }`, or a legacy `(value) => void` callback |
|
|
91
90
|
|
|
92
|
-
|
|
93
|
-
buildMask(value: string, mask: MaskPattern, caret?: number): Mask
|
|
91
|
+
### Other exports
|
|
94
92
|
|
|
95
|
-
|
|
96
|
-
|
|
93
|
+
| Export | Description |
|
|
94
|
+
|--------|-------------|
|
|
95
|
+
| `buildMask(value, mask, caret?)` | Build a `Mask` instance (array `mask` is resolved to one string first). |
|
|
96
|
+
| `getMaxLength(mask)` | Maximum string length for the mask (for array masks, the longest pattern). |
|
|
97
|
+
| `applyMask(value, mask, inputCaret?)` | Low-level: apply a **single** mask string; returns `{ value, caret }`. |
|
|
97
98
|
|
|
98
|
-
|
|
99
|
-
type MaskPattern = string | string[]
|
|
100
|
-
```
|
|
99
|
+
### `Mask` class
|
|
101
100
|
|
|
102
|
-
|
|
101
|
+
`buildMask` returns a `Mask` for advanced use. The instance applies the pattern and keeps a `caret` position aligned with the masked output (see TypeScript definitions in the package).
|
|
103
102
|
|
|
104
|
-
|
|
105
|
-
make install # install dependencies
|
|
106
|
-
make test # run tests + coverage
|
|
107
|
-
make build # build ESM + CJS + UMD
|
|
108
|
-
make dev # watch mode
|
|
109
|
-
make lint # lint source files
|
|
110
|
-
```
|
|
103
|
+
### Types
|
|
111
104
|
|
|
112
|
-
|
|
105
|
+
```ts
|
|
106
|
+
type MaskPattern = string | string[]
|
|
113
107
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
108
|
+
interface MaskResult {
|
|
109
|
+
readonly value: string
|
|
110
|
+
readonly caret: number
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
interface BindOptions {
|
|
114
|
+
onChange?: (value: string) => void
|
|
115
|
+
}
|
|
118
116
|
```
|
|
119
117
|
|
|
118
|
+
`MaskPattern`, `MaskResult`, and `BindOptions` are exported as types.
|
|
119
|
+
|
|
120
120
|
## License
|
|
121
121
|
|
|
122
122
|
MIT — [Danilo Celestino de Castro](https://github.com/dan2dev)
|