mother-mask 2.0.1 → 2.0.2
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 +107 -10
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,23 +1,120 @@
|
|
|
1
|
-
#
|
|
1
|
+
# mother-mask
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Lightweight input mask library for browsers. Zero dependencies, TypeScript-first, ships ESM + CJS + UMD.
|
|
4
4
|
|
|
5
|
-
##
|
|
6
|
-
|
|
7
|
-
- Install dependencies:
|
|
5
|
+
## Install
|
|
8
6
|
|
|
9
7
|
```bash
|
|
10
|
-
npm install
|
|
8
|
+
npm install mother-mask
|
|
9
|
+
# or
|
|
10
|
+
pnpm add mother-mask
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
### `bind(input, mask, callback?)` — DOM binding
|
|
16
|
+
|
|
17
|
+
Attach a mask to any input element. Idempotent — calling `bind()` twice on the same element has no effect.
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { bind } from 'mother-mask'
|
|
21
|
+
|
|
22
|
+
const input = document.getElementById('phone') as HTMLInputElement
|
|
23
|
+
|
|
24
|
+
// Fixed mask
|
|
25
|
+
bind(input, '(99) 99999-9999')
|
|
26
|
+
|
|
27
|
+
// Dynamic mask — switches automatically as the user types
|
|
28
|
+
bind(input, ['(99) 9999-9999', '(99) 99999-9999'])
|
|
29
|
+
|
|
30
|
+
// Optional callback — receives the masked value on every change
|
|
31
|
+
bind(input, '999.999.999-99', (value) => {
|
|
32
|
+
console.log(value) // e.g. "123.456.789-01"
|
|
33
|
+
})
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### `process(value, mask)` — pure function
|
|
37
|
+
|
|
38
|
+
Apply a mask to a raw string without touching the DOM. Useful for formatting stored values.
|
|
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
|
+
```
|
|
47
|
+
|
|
48
|
+
## Pattern syntax
|
|
49
|
+
|
|
50
|
+
| Character | Matches |
|
|
51
|
+
|-----------|----------------|
|
|
52
|
+
| `9` | Digit (0–9) |
|
|
53
|
+
| `Z` | Letter (a–z, A–Z) |
|
|
54
|
+
| anything else | Literal — inserted automatically |
|
|
55
|
+
|
|
56
|
+
## Array masks
|
|
57
|
+
|
|
58
|
+
Pass an ordered array (shortest → longest) to support variable-length inputs. The mask is selected by comparing the current value length against each mask's total length.
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
// Brazilian phone: 8-digit → 9-digit landline / mobile
|
|
62
|
+
bind(input, ['(99) 9999-9999', '(99) 99999-9999'])
|
|
63
|
+
|
|
64
|
+
// CPF / CNPJ
|
|
65
|
+
bind(input, ['999.999.999-99', '99.999.999/9999-99'])
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## UMD / CDN
|
|
69
|
+
|
|
70
|
+
```html
|
|
71
|
+
<script src="https://unpkg.com/mother-mask/dist/mother-mask.umd.js"></script>
|
|
72
|
+
<script>
|
|
73
|
+
MotherMask.bind(document.getElementById('cpf'), '999.999.999-99')
|
|
74
|
+
</script>
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## API reference
|
|
78
|
+
|
|
79
|
+
```ts
|
|
80
|
+
// Apply mask to a string — no DOM required
|
|
81
|
+
process(value: string, mask: MaskPattern): string
|
|
82
|
+
|
|
83
|
+
// Bind a mask to an input element (idempotent)
|
|
84
|
+
bind(
|
|
85
|
+
input: HTMLInputElement | Element,
|
|
86
|
+
mask: MaskPattern,
|
|
87
|
+
callback?: ((value: string) => void) | null,
|
|
88
|
+
): void
|
|
89
|
+
|
|
90
|
+
// Build a Mask instance directly (advanced use)
|
|
91
|
+
buildMask(value: string, mask: MaskPattern, caret?: number): Mask
|
|
92
|
+
|
|
93
|
+
// Maximum allowed input length for a given mask
|
|
94
|
+
getMaxLength(mask: MaskPattern): number
|
|
95
|
+
|
|
96
|
+
// Pattern type
|
|
97
|
+
type MaskPattern = string | string[]
|
|
11
98
|
```
|
|
12
99
|
|
|
13
|
-
|
|
100
|
+
## Development
|
|
14
101
|
|
|
15
102
|
```bash
|
|
16
|
-
|
|
103
|
+
make install # install dependencies
|
|
104
|
+
make test # run tests + coverage
|
|
105
|
+
make build # build ESM + CJS + UMD
|
|
106
|
+
make dev # watch mode
|
|
107
|
+
make lint # lint source files
|
|
17
108
|
```
|
|
18
109
|
|
|
19
|
-
|
|
110
|
+
## Release
|
|
20
111
|
|
|
21
112
|
```bash
|
|
22
|
-
|
|
113
|
+
make publish # bump patch, publish, commit, tag, push
|
|
114
|
+
make publish BUMP=minor
|
|
115
|
+
make publish BUMP=major
|
|
23
116
|
```
|
|
117
|
+
|
|
118
|
+
## License
|
|
119
|
+
|
|
120
|
+
MIT — [Danilo Celestino de Castro](https://github.com/dan2dev)
|