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.
Files changed (2) hide show
  1. package/README.md +57 -57
  2. 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 + CJS + UMD.
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, callback?)` — DOM binding
21
+ ### `bind(input, mask, options?)`
16
22
 
17
- Attach a mask to any input element. Idempotent calling `bind()` twice on the same element has no effect.
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 — switches automatically as the user types
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
- // Optional callback receives the masked value on every change
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
- ### `process(value, mask)` pure function
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
- 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
- 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` | Digit (09) |
54
- | `Z` | Letter (az, AZ) |
55
- | `A` | Alphanumeric (0–9, a–z, A–Z) |
56
- | anything else | Literal — inserted automatically |
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 (shortest → longest) to support variable-length inputs. The mask is selected by comparing the current value length against each mask's total length.
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
- ```ts
82
- // Apply mask to a string — no DOM required
83
- process(value: string, mask: MaskPattern): string
83
+ ### `bind` (primary)
84
84
 
85
- // Bind a mask to an input element (idempotent)
86
- bind(
87
- input: HTMLInputElement | Element,
88
- mask: MaskPattern,
89
- callback?: ((value: string) => void) | null,
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
- // Build a Mask instance directly (advanced use)
93
- buildMask(value: string, mask: MaskPattern, caret?: number): Mask
91
+ ### Other exports
94
92
 
95
- // Maximum allowed input length for a given mask
96
- getMaxLength(mask: MaskPattern): number
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
- // Pattern type
99
- type MaskPattern = string | string[]
100
- ```
99
+ ### `Mask` class
101
100
 
102
- ## Development
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
- ```bash
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
- ## Release
105
+ ```ts
106
+ type MaskPattern = string | string[]
113
107
 
114
- ```bash
115
- make publish # bump patch, publish, commit, tag, push
116
- make publish BUMP=minor
117
- make publish BUMP=major
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)
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mother-mask",
3
3
  "private": false,
4
- "version": "2.0.4",
4
+ "version": "2.0.5",
5
5
  "type": "module",
6
6
  "description": "Lightweight input mask library for browsers",
7
7
  "author": "Danilo Celestino de Castro <dan2dev>",