mother-mask 3.10.0 → 3.12.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 +138 -4
- package/dist/mother-mask.cjs +1 -1
- package/dist/mother-mask.cjs.map +1 -1
- package/dist/mother-mask.d.cts +64 -13
- package/dist/mother-mask.d.mts +64 -13
- package/dist/mother-mask.mjs +1 -1
- package/dist/mother-mask.mjs.map +1 -1
- package/dist/mother-mask.umd.js +1 -1
- package/dist/mother-mask.umd.js.map +1 -1
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Lightweight input masks for browser forms. Zero runtime dependencies, written in TypeScript, and published with ESM, CJS, and UMD builds.
|
|
4
4
|
|
|
5
|
-
[npm](https://www.npmjs.com/package/mother-mask) | [Live demo](https://
|
|
5
|
+
[npm](https://www.npmjs.com/package/mother-mask) | [Live demo](https://dan2dev.github.io/mother-mask/)
|
|
6
6
|
|
|
7
7
|
## Install
|
|
8
8
|
|
|
@@ -76,13 +76,28 @@ bindDecimal(input, {
|
|
|
76
76
|
})
|
|
77
77
|
```
|
|
78
78
|
|
|
79
|
+
`prefix` and `suffix` are chrome, not content. Typing with the caret parked inside them lands the character at the nearest edge of the number, so every spot that looks like the start of the number behaves like it:
|
|
80
|
+
|
|
81
|
+
```ts
|
|
82
|
+
bindDecimal(input, { prefix: '$', decimalPlaces: 2 })
|
|
83
|
+
// "$0.00" — caret at the far left, type "2"
|
|
84
|
+
// → "$2|.00" same as typing just after the "$"
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
Their text is never read back as part of the number either, so an affix carrying a digit or the decimal separator stays out of the value:
|
|
88
|
+
|
|
89
|
+
```ts
|
|
90
|
+
bindDecimal(input, { prefix: 'Q1 ', decimalPlaces: 2 })
|
|
91
|
+
// typing 1234 → "Q1 1,234.00", and unmaskDecimal() reports 1234
|
|
92
|
+
```
|
|
93
|
+
|
|
79
94
|
## Pattern Syntax
|
|
80
95
|
|
|
81
96
|
| Character | Matches |
|
|
82
97
|
| --- | --- |
|
|
83
98
|
| `9` | Digit |
|
|
84
|
-
| `Z` |
|
|
85
|
-
| `A` |
|
|
99
|
+
| `Z` | ASCII letter |
|
|
100
|
+
| `A` | ASCII letter or digit |
|
|
86
101
|
| Anything else | Literal separator |
|
|
87
102
|
|
|
88
103
|
Examples:
|
|
@@ -93,6 +108,105 @@ bind(input, '99/99/9999')
|
|
|
93
108
|
bind(input, 'AA.AAA.AAA/AAAA-99')
|
|
94
109
|
```
|
|
95
110
|
|
|
111
|
+
## Custom Tokens and Transforms
|
|
112
|
+
|
|
113
|
+
Tokens are local to an operation or binding. A definition is a `RegExp`, a
|
|
114
|
+
`(char: string) => boolean` matcher, or `{ match, transform? }`:
|
|
115
|
+
|
|
116
|
+
```ts
|
|
117
|
+
bind(input, 'HH-HH', { tokens: { H: /[0-9A-Fa-f]/ } })
|
|
118
|
+
// "a1b2" → "a1-b2"; "g" is rejected
|
|
119
|
+
|
|
120
|
+
bind(input, 'UUU-999', {
|
|
121
|
+
tokens: {
|
|
122
|
+
U: { match: /[a-z]/i, transform: char => char.toUpperCase() },
|
|
123
|
+
},
|
|
124
|
+
})
|
|
125
|
+
// "abc123" → "ABC-123"
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
Keys are single Unicode code points; `\` is reserved. Custom definitions may
|
|
129
|
+
override `9`, `Z`, or `A` for that binding only. Definitions are snapshotted on
|
|
130
|
+
bind; dispose and rebind to change them. Matchers should be pure. RegExp `g`/`y`
|
|
131
|
+
flags are ignored on a private copy; the caller's `lastIndex` is never changed.
|
|
132
|
+
|
|
133
|
+
A transform **must return exactly one Unicode code point**, otherwise a
|
|
134
|
+
`RangeError` is thrown (for example, uppercasing `ß` to `SS` is not supported).
|
|
135
|
+
Use an idempotent transform whose output still matches the token. UTF-16 width
|
|
136
|
+
may change: the caret follows the source character, not the output's case or width.
|
|
137
|
+
|
|
138
|
+
Custom tokens work with ordered arrays, segmented editing, eager literals,
|
|
139
|
+
and all four APIs: `applyMask`, `process`, `buildMask`, and `bind`.
|
|
140
|
+
|
|
141
|
+
## Content-dependent Masks
|
|
142
|
+
|
|
143
|
+
```ts
|
|
144
|
+
bind(input, '9999 9999 9999 9999', {
|
|
145
|
+
resolveMask(value) {
|
|
146
|
+
return value.startsWith('34') || value.startsWith('37')
|
|
147
|
+
? '9999 999999 99999'
|
|
148
|
+
: '9999 9999 9999 9999'
|
|
149
|
+
},
|
|
150
|
+
})
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
`resolveMask` is called **once per masking application**, before transforms,
|
|
154
|
+
with candidate data: code points accepted by the slots of the supplied fallback
|
|
155
|
+
pattern (or any fallback array member). Complete fallback literal runs at their
|
|
156
|
+
slot boundaries (escaped runs also after a segment shrinks) and nonmatching characters are removed. Thus raw and formatted
|
|
157
|
+
card numbers give the same digit stream, and invalid letters cannot change the
|
|
158
|
+
prefix. Make the fallback alphabet cover every format your resolver can return.
|
|
159
|
+
The callback can return a string or an ordered array; arrays retain capacity-based
|
|
160
|
+
selection. No recursive resolution or caching of input values occurs.
|
|
161
|
+
|
|
162
|
+
Resolver masks describe **one continuous identifier**: old separators are removed
|
|
163
|
+
before rendering the selected layout, even with `segmented: true`. This prevents
|
|
164
|
+
stale boundaries when equal-capacity layouts switch. For independently editable
|
|
165
|
+
fields, use a static pattern/array and segmented mode instead. Eager mode still
|
|
166
|
+
applies; the caret tracks logical characters and already-crossed literal boundaries.
|
|
167
|
+
|
|
168
|
+
`bind` does not add `maxlength` for resolvers (the maximum is unknowable) or
|
|
169
|
+
custom tokens (IME drafts can exceed the final capacity). The engine still caps
|
|
170
|
+
slots. Author-supplied `maxlength` is preserved. Disposal removes attributes added
|
|
171
|
+
by the binding, so rebinding cannot inherit a library-created stale limit.
|
|
172
|
+
|
|
173
|
+
## Escaped Literals
|
|
174
|
+
|
|
175
|
+
```ts
|
|
176
|
+
bind(input, '\\A-999999') // "123456" → "A-123456"
|
|
177
|
+
bind(input, '\\9-99') // "12" → "9-12"
|
|
178
|
+
bind(input, '\\Z-99') // "12" → "Z-12"
|
|
179
|
+
bind(input, '\\\\99') // a literal backslash, then two digits
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
In the pattern, backslash escapes a built-in/custom token or another backslash.
|
|
183
|
+
Before any other character it remains literal; a trailing backslash also remains
|
|
184
|
+
literal. Existing masks that used a backslash immediately before a token or
|
|
185
|
+
backslash must double it to keep that backslash in the output.
|
|
186
|
+
|
|
187
|
+
Complete literal runs are treated as formatting at their boundary; escaped runs
|
|
188
|
+
remain formatting after a segment shrinks, rather than becoming slot data. If literal text also matches the data alphabet, raw
|
|
189
|
+
and already-formatted input can be ambiguous: use a distinct separator (such as
|
|
190
|
+
`'\\9-99'`) to distinguish the literal from user data. Resolver formats should
|
|
191
|
+
likewise avoid introducing data-looking literals absent from the fallback pattern.
|
|
192
|
+
|
|
193
|
+
## Unicode and Composition
|
|
194
|
+
|
|
195
|
+
```ts
|
|
196
|
+
bind(input, 'LLLL', { tokens: { L: /\p{L}/u } })
|
|
197
|
+
// accepts Á, Ç, É, ñ, ü, ø, Ж, λ, and supplementary letters such as 𐐀
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
Matching is by **Unicode code point**, not UTF-16 code unit or grapheme cluster.
|
|
201
|
+
Combining marks and joined emoji sequences therefore occupy separate slots if
|
|
202
|
+
accepted; no normalization or grapheme segmentation is performed. Caret offsets
|
|
203
|
+
remain DOM-compatible UTF-16 positions. Built-in `Z` and `A` remain ASCII-only.
|
|
204
|
+
|
|
205
|
+
With custom tokens, provisional IME text and selection are left untouched until
|
|
206
|
+
composition commits. This includes custom ASCII matchers: an arbitrary predicate's
|
|
207
|
+
alphabet cannot be safely inferred. Built-in-only masks keep live formatting during
|
|
208
|
+
Android autocorrect composition. No timeout or delayed commit is used.
|
|
209
|
+
|
|
96
210
|
## Segmented Editing
|
|
97
211
|
|
|
98
212
|
Masks are segmented by default. Separators behave like boundaries, which keeps fields such as dates from bleeding into each other while editing:
|
|
@@ -101,6 +215,22 @@ Masks are segmented by default. Separators behave like boundaries, which keeps f
|
|
|
101
215
|
bind(input, '99/99/9999')
|
|
102
216
|
```
|
|
103
217
|
|
|
218
|
+
Deleting all the data in an internal segment preserves its existing dividers
|
|
219
|
+
while later segments still contain data. For example, three Backspaces over
|
|
220
|
+
`222` in `(111) 222-3333` leave `(111) |-3333` (`|` marks the caret), ready to
|
|
221
|
+
type a replacement. This also works with `eager: false`. Trailing separators
|
|
222
|
+
still follow eager mode, and selecting everything and deleting clears the input.
|
|
223
|
+
|
|
224
|
+
Separators left in the value also anchor the characters around them, so an edit that replaces whole fields leaves the rest where it was:
|
|
225
|
+
|
|
226
|
+
```ts
|
|
227
|
+
bind(input, '999.999.999-99')
|
|
228
|
+
// "012.153.441-39" — select "012.153.441", type "015"
|
|
229
|
+
// → "015.|-39" the "-" keeps "39" in the last field
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
Anchoring is only as precise as the separators allow. A mask whose separators are all the same character can produce a genuinely ambiguous value — with `'99/99/9999'`, `"1/2025"` reads equally well as `1 / 20 / 25` — and resolves it to the earliest field that fits. Masks with distinct separators (CPF, CNPJ, phone numbers) have no such gap.
|
|
233
|
+
|
|
104
234
|
For classic reflow behavior, pass `segmented: false`:
|
|
105
235
|
|
|
106
236
|
```ts
|
|
@@ -127,7 +257,7 @@ Main exports:
|
|
|
127
257
|
- `applyMask(value, mask, inputCaret?, options?)`
|
|
128
258
|
- `process(value, mask, options?)`
|
|
129
259
|
- `buildMask(value, mask, caret?, options?)`
|
|
130
|
-
- `getMaxLength(mask)`
|
|
260
|
+
- `getMaxLength(mask, options?)` (formatted UTF-16 upper bound; `Infinity` with a resolver)
|
|
131
261
|
- `applyDecimalMask(value, inputCaret?, options?)`
|
|
132
262
|
- `processDecimal(value, options?)`
|
|
133
263
|
- `unmaskDecimal(value, options?)`
|
|
@@ -137,6 +267,10 @@ Main exports:
|
|
|
137
267
|
Exported types:
|
|
138
268
|
|
|
139
269
|
- `MaskPattern`
|
|
270
|
+
- `TokenMatcher`
|
|
271
|
+
- `MaskTokenDefinition`
|
|
272
|
+
- `MaskTokens`
|
|
273
|
+
- `MaskResolver`
|
|
140
274
|
- `MaskResult`
|
|
141
275
|
- `ApplyMaskOptions`
|
|
142
276
|
- `BindOptions`
|
package/dist/mother-mask.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});function e(e){return e>=`0`&&e<=`9`}function t(e){return e>=`a`&&e<=`z`||e>=`A`&&e<=`Z`}function n(e){return e===`9`||e===`Z`||e===`A`}function r(n,r){return r===`9`?e(n):r===`Z`?t(n):e(n)||t(n)}function i(e,t,n,i){let a=``,o=``,s=0,c=0,l=!1;for(let i=0;i<t.length;i++){let u=t[i];if(u!==`9`&&u!==`Z`&&u!==`A`){o+=u;continue}let d=!1;for(;s<e.length;){let t=e[s++];if(r(t,u)){a+=o+t,o=``,d=!0,l||(s<=n?c=a.length:l=!0);break}}if(!d)break}if(l||(c=a.length),i&&o){let e=c===a.length;a+=o,e&&(c=a.length)}return{value:a,caret:c}}const a=new Map;function o(e){let t=a.get(e);if(t)return t;let r=[],i=0;for(;i<e.length;){let t=i,a=n(e[i]);for(;i<e.length&&n(e[i])===a;)i++;let o=e.slice(t,i);r.push(a?{kind:`slots`,chars:o}:{kind:`literal`,text:o})}return a.set(e,r),r}function s(e,t){let n=0;for(let r=t;r<e.length;r++){let t=e[r];t.kind===`slots`&&(n+=t.chars.length)}return n}function c(n,r){let i=0;for(let a=r;a<n.length;a++){let r=n[a];(e(r)||t(r))&&i++}return i}function l(e,t,n,i){let a=o(t),l=``,u=``,d=0,f=0,p=!1,m=!1;for(let t=0;t<a.length&&!m;t++){let i=a[t];if(i.kind===`literal`){u+=i.text,e.startsWith(i.text,d)&&(d+=i.text.length);continue}let o=a[t+1],h=s(a,t+1);for(let t=0;t<i.chars.length;t++){let a=i.chars[t],s=!1;for(;d<e.length;){let t=e[d];if(r(t,a)){d++,l+=u+t,u=``,s=!0,p||(d<=n?f=l.length:p=!0);break}if(o?.kind===`literal`&&e.startsWith(o.text,d)&&c(e,d)<=h)break;d++}if(!s){d>=e.length&&(m=!0);break}}}if(p||(f=l.length),i&&u){let e=f===l.length;l+=u,e&&(f=l.length)}return{value:l,caret:f}}function u(e,t,n=0,r){if(!e)return{value:``,caret:0};let a=r?.eager!==!1;return r?.segmented===!1?i(e,t,n,a):l(e,t,n,a)}function d(e){let t=0;for(let n of e)(n>=`0`&&n<=`9`||n>=`a`&&n<=`z`||n>=`A`&&n<=`Z`)&&t++;return t}function f(e){let t=0;for(let n of e)(n===`9`||n===`Z`||n===`A`)&&t++;return t}function p(e,t){if(!Array.isArray(t))return t;let n=d(e),r=0;for(;r<t.length-1&&n>f(t[r]);)r++;return t[r]}function m(e){return Array.isArray(e)?e.length>0?Math.max(...e.map(e=>e.length)):0:e.length}var h=class{caret;_value;_mask;_options;constructor(e,t,n=0,r){this._value=e,this._mask=t,this.caret=n,this._options=r}process(){let e=u(this._value,this._mask,this.caret,this._options);return this.caret=e.caret,e.value}};function g(e,t,n=0,r){return new h(e,p(e,t),n,r)}function _(e,t,n){return g(e,t,0,n).process()}let v;function y(){return v===void 0&&(v=typeof navigator<`u`&&/iPad|iPhone|iPod/i.test(navigator.userAgent)),v}const b=`data-masked`;function x(e){return e==null?{}:typeof e==`function`?{onChange:e}:e}function S(e){try{return e.selectionStart??e.value.length}catch{return e.value.length}}function C(e,t){try{e.setSelectionRange(t,t)}catch{}}function w(e){return e===`deleteContentBackward`?`backspace`:e===`deleteContentForward`?`delete`:e&&e.startsWith(`insert`)?`insert`:`unidentified`}function T(e,t){return!t&&e}function E(e,t,n){if(e.getAttribute(b)!==null)return()=>{};let{onChange:r,segmented:i,eager:a}=x(n),o=[],s=(t,n)=>{e.hasAttribute(t)||(e.setAttribute(t,n),o.push(t))},c=m(t);e.setAttribute(b,Array.isArray(t)?t.join(`|`):t),s(`autocomplete`,`off`),s(`autocorrect`,`off`),s(`autocapitalize`,`off`),s(`spellcheck`,`false`),s(`maxlength`,String(c));let l=!1,u=!1,d=!1,f=e.value??``,p=y()?`keyup`:`keydown`,h=new Set,_=e=>{let t=requestAnimationFrame(()=>{h.delete(t),e()});h.add(t)},v=()=>{for(let e of h)cancelAnimationFrame(e);h.clear()},E=e=>{let n=e.target;_(()=>{let e=g(n.value,t,0,{segmented:i,eager:a});n.value=e.process(),r?.(n.value)})},D=e=>{let n=e,o=e.target;v(),l=!1,d=!0;let s=S(o),c=f.length,u=w(n.inputType),p=g(o.value,t,s,{segmented:i,eager:T(a,u===`backspace`||u===`delete`)});o.value=p.process(),u===`unidentified`?C(o,o.value.length>c?p.caret:s):u===`delete`?C(o,c===o.value.length?s+1:s):u===`backspace`?C(o,s):C(o,p.caret),f=o.value,r?.(o.value)},O=()=>{u=!0,v(),l=!1},k=e=>{u=!1,d=!0;let n=e.target,o=S(n),s=g(n.value,t,o,{segmented:i,eager:a});n.value=s.process(),C(n,s.caret),f=n.value,r?.(n.value)},A=e=>{let n=e,o=n.target,s=o.value;if(u)return;if(p===`keyup`&&d){d=!1;return}if(!n.key){l=!0,_(()=>{if(o.value===s&&o.selectionStart!==o.selectionEnd){l=!1;return}let e=o.selectionStart??999,n=o.value.length<s.length,r=g(o.value,t,e,{segmented:i,eager:T(a,n)});o.value=r.process(),o.setSelectionRange(r.caret,r.caret),_(()=>{l=!1})});return}if(n.key===`Meta`)return;let f=n.key===`Backspace`,m=n.key===`Delete`,h=n.key.length===1&&!n.ctrlKey&&!n.altKey&&!n.metaKey,v=n.key===`Unidentified`;if(h&&o.selectionStart===o.selectionEnd&&s.length>=c&&!y()){n.preventDefault();return}if(l){n.preventDefault();return}!f&&!m&&!h&&!v||_(()=>{if(o.value===s&&o.selectionStart!==o.selectionEnd)return;let e=o.selectionStart??999,n=g(o.value,t,e,{segmented:i,eager:T(a,f||m)});if(o.value=n.process(),v){let t=o.value.length>s.length?n.caret:e;o.setSelectionRange(t,t)}else if(m){let t=s.length===o.value.length?e+1:e;o.setSelectionRange(t,t)}else f?o.setSelectionRange(e,e):h&&o.setSelectionRange(n.caret,n.caret);r?.(o.value)})};return e.addEventListener(`paste`,E),e.addEventListener(`input`,D),e.addEventListener(`compositionstart`,O),e.addEventListener(`compositionend`,k),e.addEventListener(p,A),()=>{e.removeEventListener(`paste`,E),e.removeEventListener(`input`,D),e.removeEventListener(`compositionstart`,O),e.removeEventListener(`compositionend`,k),e.removeEventListener(p,A),e.removeAttribute(b);for(let t of o)e.removeAttribute(t);v()}}function D(e){return e>=`0`&&e<=`9`}function O(e){let t=e?.decimalPlaces,n=t!=null&&Number.isFinite(t)?Math.max(0,Math.floor(t)):void 0,r=e?.numberPlaces;return{decimalPlaces:n,numberPlaces:r!=null&&Number.isFinite(r)?Math.max(1,Math.floor(r)):void 0,segmented:e?.segmented??!0,separator:e?.separator??`,`,decimalSeparator:e?.decimalSeparator??`.`,prefix:e?.prefix??``,suffix:e?.suffix??``,allowNegative:e?.allowNegative??!1}}function k(e){let t=0;for(;t<e.length-1&&e[t]===`0`;)t++;return e.slice(t)}function A(e,t){if(!t||e.length<=3)return e;let n=[],r=e.length;for(;r>3;)n.unshift(e.slice(r-3,r)),r-=3;return n.unshift(e.slice(0,r)),n.join(t)}function j(e,t){if(t<=0)return 0;let n=0;for(let r=0;r<e.length;r++)if(D(e[r])&&(n++,n===t))return r+1;return e.length}function M(e,t){let n=``,r=``,i=!1,a=!1,o=t.decimalPlaces!==0;for(let s of e){if(D(s)){a?(t.decimalPlaces==null||r.length<t.decimalPlaces)&&(r+=s):(t.numberPlaces==null||n.length<t.numberPlaces)&&(n+=s);continue}if(o&&!a&&s===t.decimalSeparator){a=!0;continue}s===`-`&&t.allowNegative&&(i=!0)}return{isNegative:i,intDigits:n,fracDigits:r,hasSeparator:a}}function N(e,t,n){let r=n.decimalPlaces!==0,i=!1,a=0;for(let o=0;o<t;o++){let t=e[o];if(D(t)){i?(n.decimalPlaces==null||a<n.decimalPlaces)&&a++:(n.numberPlaces==null||a<n.numberPlaces)&&a++;continue}r&&!i&&t===n.decimalSeparator&&(i=!0,a=0)}return{inFraction:i,digitsBefore:a}}function P(e,t=0,n){let r=O(n);if(!e)return{value:``,caret:0};let{isNegative:i,intDigits:a,fracDigits:o,hasSeparator:s}=M(e,r);if(a===``&&o===``&&!s)return{value:``,caret:0};let c=k(a||`0`),l=r.numberPlaces==null?c:c.padStart(r.numberPlaces,`0`),u=r.segmented?A(l,r.separator):l,d=r.decimalPlaces!=null&&r.decimalPlaces>0?o.padEnd(r.decimalPlaces,`0`):o,f=u+(r.decimalPlaces!==0&&(r.decimalPlaces!=null||s)?r.decimalSeparator+d:``),p=i?`-`:``,m=p+r.prefix+f+r.suffix,{inFraction:h,digitsBefore:g}=N(e,Math.max(0,Math.min(t,e.length)),r),_=p.length+r.prefix.length,v=l.length-c.length;return{value:m,caret:h?_+u.length+r.decimalSeparator.length+g:_+j(u,g+v)}}function F(e,t){return P(e,e.length,t).value}function I(e,t){let{isNegative:n,intDigits:r,fracDigits:i}=M(e,O(t)),a=Number(i?`${r||`0`}.${i}`:r||`0`);return n?-a:a}function L(e,t){let n=O(t);if(n.decimalPlaces==null||n.decimalPlaces<=0)return null;let{isNegative:r,intDigits:i,hasSeparator:a}=M(e,n);if(a||i.length<n.decimalPlaces+1)return null;let o=i.length-n.decimalPlaces,s=i.slice(o),c=i.slice(0,o-1),l=r?`-`:``;return P(l+c+n.decimalSeparator+s,l.length+c.length,n)}function R(e,t,n,r){let i=O(r),a=t-1;if(a<0||e[a]!==n)return null;let{isNegative:o,intDigits:s,fracDigits:c,hasSeparator:l}=M(e.slice(0,a)+e.slice(t),i),u=k(s||`0`),d=u!==`0`,f=d&&i.numberPlaces!=null&&u.length<i.numberPlaces;if(d&&!f)return null;let p=+!!o+i.prefix.length;if(a<p||a>p+s.length)return null;let m=o?`-`:``,h=(d?u:``)+n;return P(m+h+(l?i.decimalSeparator+c:``),m.length+h.length,i)}function z(e,t){let n=O(t);if(!Number.isFinite(e))return``;let r=n.allowNegative&&e<0,i=Math.abs(e),a=n.decimalPlaces==null?String(i):i.toFixed(n.decimalPlaces),o=a.indexOf(`.`),s=o===-1?a:a.slice(0,o),c=o===-1?``:a.slice(o+1),l=k(s||`0`),u=n.numberPlaces==null?l:l.padStart(n.numberPlaces,`0`),d=(n.segmented?A(u,n.separator):u)+(n.decimalPlaces!==0&&c!==``?n.decimalSeparator+c:``);return(r?`-`:``)+n.prefix+d+n.suffix}const B=`data-masked`;function V(e){return e==null?{}:typeof e==`function`?{onChange:e}:e}function H(e){return e.length===1&&e>=`0`&&e<=`9`}function U(e){try{return e.selectionStart??e.value.length}catch{return e.value.length}}function W(e,t){try{e.setSelectionRange(t,t)}catch{}}function G(e,t){if(e.getAttribute(B)!==null)return()=>{};let{onChange:n,...r}=V(t),i=r,{decimalSeparator:a,decimalPlaces:o}=O(i),s=[],c=(t,n)=>{e.hasAttribute(t)||(e.setAttribute(t,n),s.push(t))};e.setAttribute(B,`decimal`),c(`autocomplete`,`off`),c(`autocorrect`,`off`),c(`autocapitalize`,`off`),c(`spellcheck`,`false`);let l=!1,u=!1,d=!1,f=null,p=y()?`keyup`:`keydown`,m=new Set,h=e=>{let t=requestAnimationFrame(()=>{m.delete(t),e()});m.add(t)},g=()=>{for(let e of m)cancelAnimationFrame(e);m.clear()},_=(e,t)=>{e.value=t.value,W(e,t.caret),n?.(t.value,I(t.value,i))},v=(e,t={})=>{let n=U(e),r=(t,r)=>{if(o===0||t.length!==1||t!==`.`&&t!==`,`||t===a)return!1;for(let i of r)if(i!=null&&i>=0&&e.value.slice(i,i+t.length)===t)return e.value=e.value.slice(0,i)+a+e.value.slice(i+t.length),n=i+t.length<=n?n+a.length-t.length:n,W(e,n),!0;return!1};if(f){let{text:e,starts:t}=f;f=null,r(e,t)}if(t.inputType===`deleteContentBackward`){let t=L(e.value,i);if(t){_(e,t);return}}let s=t.insertedText;s!=null&&r(s,[t.insertedAt,n-s.length]);let c=s!=null&&s.length===1&&H(s)?s:void 0,l=c?R(e.value,n,c,i):null;_(e,l??P(e.value,n,i))},b=e=>{let t=e.target;h(()=>{v(t)})},x=e=>{let t=e,n=e.target;g(),l=!1,f=null,d=!0,v(n,{insertedText:typeof t.data==`string`?t.data:null,inputType:t.inputType})},S=()=>{u=!0,g(),l=!1,f=null},C=e=>{u=!1,d=!0,v(e.target)},w=e=>{let t=e,n=t.target,r=U(n),i=n.value;if(u)return;if(p===`keyup`&&d){d=!1;return}if(!t.key){l=!0,h(()=>{if(n.value===i&&n.selectionStart!==n.selectionEnd){l=!1;return}v(n),h(()=>{l=!1})});return}if(t.key===`Meta`)return;if(l){t.preventDefault();return}let s=t.key===`Backspace`,c=t.key===`Delete`,m=t.key.length===1&&!t.ctrlKey&&!t.altKey&&!t.metaKey,g=t.key===`Unidentified`;m&&o!==0&&(t.key===`.`||t.key===`,`)&&t.key!==a&&(f={text:t.key,starts:[r,r-t.key.length]}),!(!s&&!c&&!m&&!g)&&h(()=>{(n.value!==i||n.selectionStart===n.selectionEnd)&&v(n,{insertedText:m?t.key:null,insertedAt:m?r:void 0,inputType:s?`deleteContentBackward`:c?`deleteContentForward`:void 0})})};return e.addEventListener(`paste`,b),e.addEventListener(`input`,x),e.addEventListener(`compositionstart`,S),e.addEventListener(`compositionend`,C),e.addEventListener(p,w),()=>{e.removeEventListener(`paste`,b),e.removeEventListener(`input`,x),e.removeEventListener(`compositionstart`,S),e.removeEventListener(`compositionend`,C),e.removeEventListener(p,w),e.removeAttribute(B);for(let t of s)e.removeAttribute(t);g()}}exports.Mask=h,exports.applyDecimalMask=P,exports.applyMask=u,exports.bind=E,exports.bindDecimal=G,exports.buildMask=g,exports.formatDecimalValue=z,exports.getMaxLength=m,exports.process=_,exports.processDecimal=F,exports.unmaskDecimal=I;
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});function e(e){return e>=`0`&&e<=`9`}function t(e){return e>=`a`&&e<=`z`||e>=`A`&&e<=`Z`}function n(n){return e(n)||t(n)}const r=[[`9`,{match:e,maxLength:1}],[`Z`,{match:t,maxLength:1}],[`A`,{match:n,maxLength:1}]];function i(e){if(typeof e==`function`)return e;let t=new RegExp(e.source,e.flags.replace(/[gy]/g,``));return e=>t.test(e)}function a(e,t){if(!t.transform)return e;let n=t.transform(e);if(typeof n!=`string`||Array.from(n).length!==1)throw RangeError(`A mask token transform must return exactly one Unicode code point`);return n}function o(e,t){let n=[],r=[],i=[],a=[],o=Array.from(e),s=0,c=!1;for(let e=0;e<o.length;e++){let l=o[e],u=!1;l===`\\`&&(o[e+1]===`\\`||t.has(o[e+1]))&&(l=o[++e],u=!0,c=!0);let d=u?void 0:t.get(l),f=n[n.length-1];if(d){if(s+=d.maxLength,f?.kind===`slots`)f.chars.push(d);else{let e=[d];r.push(i.length),a.push(n.length),i.push(e),n.push({kind:`slots`,chars:e})}}else s+=l.length,f?.kind===`literal`?f.text+=l:(r.push(-1),n.push({kind:`literal`,text:l}))}let l=i.length,u=Array(l),d=Array(l),f=Array(l+1),p=Array(n.length).fill(-1),m=Array(n.length).fill(-1),h=0;for(let e=0;e<l;e++)u[e]=h,h+=i[e].length,d[e]=a[e]>0?a[e]-1:-1;f[l]=0;for(let e=l-1;e>=0;e--)f[e]=f[e+1]+i[e].length;for(let e=0;e<n.length;e++)n[e].kind===`literal`&&(p[e]=e>0?r[e-1]:-1,m[e]=e+1<n.length?r[e+1]:-1);let g=[],_=[],v=new Set;for(let e=0;e<n.length;e++){let t=n[e];if(t.kind===`literal`)g.push(t),_.push({text:t.text,offset:m[e]<0?h:u[m[e]]});else for(let e of t.chars)g.push(e),v.add(e)}return{maxLength:s,hasEscapes:c,dataSlots:[...v],literals:_,parts:g,tokens:n,runChars:i,runOffset:u,literalBeforeRun:d,capacityFromRun:f,runOfToken:r,runBeforeLiteral:p,runAfterLiteral:m,totalSlots:h}}var s=class{definitions=new Map(r);cache=new Map;custom;constructor(e){this.custom=!!e&&Object.keys(e).length>0;for(let t of Object.keys(e??{})){let n=e[t];if(t===`\\`||Array.from(t).length!==1)throw RangeError(`Mask token keys must be one Unicode code point other than backslash`);let r=typeof n==`object`&&`match`in n?n:{match:n};this.definitions.set(t,{match:i(r.match),transform:r.transform,maxLength:2})}}compile(e){let t=this.cache.get(e);if(t)return t;let n=o(e,this.definitions);return this.cache.size===64&&this.cache.delete(this.cache.keys().next().value),this.cache.set(e,n),n}isData(e,t){if(t&&(this.custom||t.hasEscapes))return t.dataSlots.some(t=>t.match(e));for(let t of this.definitions.values())if(t.match(e))return!0;return!1}data(e,t,n,r=!0){let i=``,a=0,o=0,s=0,c=-1,l=!1,u,d=[];if(n){let e=new Set;for(let t of n){for(let n of t.dataSlots)e.add(n);if(r)for(let e of t.literals)d.push(e)}u=[...e]}let f=n?.some(e=>e.hasEscapes);for(;a<e.length;){let n=c!==s&&d?.find(t=>(t.offset===s||f)&&e.startsWith(t.text,a));if(n){a<t&&(l=!0),a+=n.text.length,c=s;continue}let r=String.fromCodePoint(e.codePointAt(a)),p=a;if(a+=r.length,!(u?u.some(e=>e.match(r)):this.isData(r))){p<t&&d?.some(t=>e.startsWith(t.text,p))&&(l=!0);continue}i+=r,s++,a<=t&&(o=i.length,l=!1)}return{value:i,caret:o,afterLiteral:l}}resolve(e,t,n=!0){if(!Array.isArray(t))return this.compile(t);let r=t.map(e=>this.compile(e)),i=Array.from(this.data(e,0,this.custom||r.some(e=>e.hasEscapes)?r:void 0,n).value).length,a=0;for(;a<r.length-1&&i>r[a].totalSlots;)a++;return r[a]??this.compile(``)}};const c=new s;function l(e,t){if(t?.resolveMask)return 1/0;let n=t?.tokens?new s(t.tokens):c,r=Array.isArray(e)?e:[e],i=0;for(let e of r)i=Math.max(i,n.compile(e).maxLength);return i}function u(e,t,n,r,i,o){let s=``,c=``,l=0,u=0,d=!1,f=t.tokens[0],p=!1;for(let r of t.parts){if(`kind`in r){c+=r.text,i&&e.startsWith(r.text,l)&&(l+=r.text.length,r===f&&(p=!0));continue}let m=!1;for(;l<e.length;){let h=i&&t.hasEscapes&&t.literals.find(t=>e.startsWith(t.text,l));if(h){l+=h.text.length;continue}if(i&&!p&&f?.kind===`literal`&&e.startsWith(f.text,l)){l+=f.text.length,p=!0;continue}let g=String.fromCodePoint(e.codePointAt(l));if(l+=g.length,r.match(g)){o&&!d&&l>n&&(u=s.length+c.length),s+=c+a(g,r),c=``,m=!0,d||(l<=n?u=s.length:d=!0);break}}if(!m)break}if(d||(u=s.length),r&&c){let e=u===s.length;s+=c,e&&(u=s.length)}return{value:s,caret:u}}function d(e,t){return e.tokens[e.literalBeforeRun[t]].text}function f(e,t,n,r){let i=0;for(let a=t;a<e.length;){let t=String.fromCodePoint(e.codePointAt(a));a+=t.length,n.isData(t,r)&&i++}return i}function p(e,t,n,r,i){for(let a=r+1;a<n.runChars.length;a++){let r=d(n,a);if(e.startsWith(r,t))return f(e,t+r.length,i,n)<=n.capacityFromRun[a]?a:-1}return-1}function m(e,t,n,r){let i=t.runChars.length,o=Array(t.totalSlots).fill(``),s=Array(t.totalSlots).fill(-1),c=Array(i).fill(0),l=Array(t.tokens.length).fill(-1),u=0,f=0,m=0,h=t.tokens[0];for(r&&h?.kind===`literal`&&e.startsWith(h.text)&&(l[0]=0,m=h.text.length);m<e.length&&u<i;){if(r&&l[0]<0&&h?.kind===`literal`&&e.startsWith(h.text,m)){l[0]=m,m+=h.text.length;continue}let g=t.runChars[u],_=String.fromCodePoint(e.codePointAt(m)),v=g[f].match(_),y=r&&(!v||t.hasEscapes)?p(e,m,t,u,n):-1;if(y>=0){l[t.literalBeforeRun[y]]=m,m+=d(t,y).length,u=y,f=0;continue}if(v){let n=t.runOffset[u]+f;if(o[n]=a(_,g[f]),s[n]=m+_.length,c[u]=f+1,m+=_.length,f++,f===g.length&&(u++,f=0,r&&u<i)){let n=d(t,u);e.startsWith(n,m)&&(l[t.literalBeforeRun[u]]=m,m+=n.length)}continue}m+=_.length}return{slotChar:o,slotSource:s,runFilled:c,literalSource:l}}function h(e,t,n){let{tokens:r,runBeforeLiteral:i,runAfterLiteral:a,literalBeforeRun:o,runChars:s}=e,{runFilled:c,literalSource:l}=t,u=Array(r.length).fill(!1),d=c.length-1;for(;d>=0&&c[d]===0;)d--;for(let e=0;e<r.length;e++){if(r[e].kind!==`literal`)continue;let t=a[e],o=i[e];u[e]=t>=0&&(c[t]>0||l[e]>=0&&t<d)||n&&(o<0||c[o]===s[o].length)}let f=-1;for(let e=0;e<s.length;e++){if(c[e]===0)continue;let t=o[e];if(t>=0){let n=r[t].text;for(let t=f+1;t<e;t++){let e=o[t];e<0||r[e].text===n&&(u[e]=!0)}}f=e}return u}function g(e,t,n,r,i,a){let{tokens:o,runChars:s,runOffset:c,runBeforeLiteral:l,runAfterLiteral:u,runOfToken:d}=e,{slotChar:f,slotSource:p,runFilled:m}=t,h=``,g=0,_=!1;for(let e=0;e<o.length;e++){let v=o[e];if(v.kind===`literal`){if(!n[e])continue;let o=l[e],c=u[e],d=c<0||m[c]===0,f=i&&(o<0||m[o]===s[o].length),p=t.literalSource[e],y=!_&&g===h.length;h+=v.text,y&&(a||f&&d||p>=0&&p<r)&&(g=h.length);continue}let y=d[e],b=c[y];for(let e=0;e<m[y];e++)h+=f[b+e],!_&&(p[b+e]<=r?g=h.length:_=!0)}return{value:h,caret:g}}function _(e,t,n,r,i,a,o){let s=m(e,t,i,a);return g(t,s,h(t,s,r),n,r,o)}function v(e,t,n,r,i){let a,o=!1;if(r?.resolveMask){let s=Array.isArray(t)?t:[t],c=i.data(e,n,s.map(e=>i.compile(e)));a=i.resolve(c.value,r.resolveMask(c.value),!1),e=c.value,n=c.caret,o=c.afterLiteral}else a=i.resolve(e,t);if(!e)return{value:``,caret:0};let s=r?.eager!==!1;return r?.segmented===!1?u(e,a,n,s,!r.resolveMask,o):_(e,a,n,s,i,!r?.resolveMask,o)}function y(e,t,n=0,r){return v(e,t,n,r,r?.tokens?new s(r.tokens):c)}let b;function x(){return b===void 0&&(b=typeof navigator<`u`&&/iPad|iPhone|iPod/i.test(navigator.userAgent)),b}const S=`data-masked`;function C(e){return e==null?{}:typeof e==`function`?{onChange:e}:e}function w(e){try{return e.selectionStart??e.value.length}catch{return e.value.length}}function T(e,t){try{t>0&&t<e.value.length&&e.value.charCodeAt(t)>=56320&&e.value.charCodeAt(t)<=57343&&e.value.charCodeAt(t-1)>=55296&&e.value.charCodeAt(t-1)<=56319&&t--,e.setSelectionRange(t,t)}catch{}}function E(e){return()=>{let t=e;e=void 0,t?.()}}function D(e){return e===`deleteContentBackward`?`backspace`:e===`deleteContentForward`?`delete`:e&&e.startsWith(`insert`)?`insert`:`unidentified`}function O(e,t){return!t&&e}function k(e,t,n){if(e.getAttribute(S)!==null)return()=>{};let{onChange:r,segmented:i,eager:a,tokens:o,resolveMask:c}=C(n),u=new s(o),d=(e,n,r=a)=>v(e,t,n,{tokens:o,resolveMask:c,segmented:i,eager:r},u),f=!!o&&Object.keys(o).length>0,p=[],m=(t,n)=>{e.hasAttribute(t)||(e.setAttribute(t,n),p.push(t))},h=c||f?1/0:l(t);e.setAttribute(S,Array.isArray(t)?t.join(`|`):t),m(`autocomplete`,`off`),m(`autocorrect`,`off`),m(`autocapitalize`,`off`),m(`spellcheck`,`false`),Number.isFinite(h)&&m(`maxlength`,String(h));let g=!1,_=!1,y=!1,b=e.value??``,k=x()?`keyup`:`keydown`,A=new Set,j=e=>{let t=requestAnimationFrame(()=>{A.delete(t),e()});A.add(t)},M=()=>{for(let e of A)cancelAnimationFrame(e);A.clear()},N=e=>{let t=e.target,n=t.value;j(()=>{if(f&&_||t.value===n&&t.selectionStart!==t.selectionEnd)return;let e=d(t.value,w(t));t.value=e.value,T(t,e.caret),b=t.value,r?.(t.value)})},P=e=>{let t=e,n=e.target;if(M(),g=!1,y=!0,f&&(_||t.isComposing)||n.value===b&&n.selectionStart!==n.selectionEnd)return;let i=w(n),o=b.length,s=D(t.inputType),l=n.value,u=d(l,i,O(a,s===`backspace`||s===`delete`));n.value=u.value,c&&u.value!==l&&u.value!==b?T(n,u.caret):s===`unidentified`?T(n,n.value.length>o?u.caret:i):s===`delete`?T(n,o===n.value.length?i+1:i):s===`backspace`?T(n,i):T(n,u.caret),b=n.value,r?.(n.value)},F=()=>{_=!0,M(),g=!1},I=e=>{_=!1,y=!0;let t=e.target,n=w(t),i=d(t.value,n);t.value=i.value,T(t,i.caret),b=t.value,r?.(t.value)},L=e=>{let t=e,n=t.target,i=n.value;if(_)return;if(k===`keyup`&&y){y=!1;return}if(!t.key){g=!0,j(()=>{if(n.value===i&&n.selectionStart!==n.selectionEnd){g=!1;return}let e=n.selectionStart??999,t=n.value.length<i.length,r=d(n.value,e,O(a,t));n.value=r.value,T(n,r.caret),b=n.value,j(()=>{g=!1})});return}if(t.key===`Meta`)return;let o=t.key===`Backspace`,s=t.key===`Delete`,l=Array.from(t.key).length===1&&!t.ctrlKey&&!t.altKey&&!t.metaKey,u=t.key===`Unidentified`;if(l&&n.selectionStart===n.selectionEnd&&i.length>=h&&!x()){t.preventDefault();return}if(g){t.preventDefault();return}!o&&!s&&!l&&!u||j(()=>{if(n.value===i&&n.selectionStart!==n.selectionEnd)return;let e=n.selectionStart??999,t=n.value,f=d(t,e,O(a,o||s));if(n.value=f.value,c&&f.value!==t&&f.value!==i)T(n,f.caret);else if(u){let t=n.value.length>i.length?f.caret:e;T(n,t)}else if(s){let t=i.length===n.value.length?e+1:e;T(n,t)}else o?T(n,e):l&&T(n,f.caret);b=n.value,r?.(n.value)})};return e.addEventListener(`paste`,N),e.addEventListener(`input`,P),e.addEventListener(`compositionstart`,F),e.addEventListener(`compositionend`,I),e.addEventListener(k,L),E(()=>{e.removeEventListener(`paste`,N),e.removeEventListener(`input`,P),e.removeEventListener(`compositionstart`,F),e.removeEventListener(`compositionend`,I),e.removeEventListener(k,L),e.removeAttribute(S);for(let t of p)e.removeAttribute(t);M()})}function A(e){return e>=`0`&&e<=`9`}function j(e){let t=e?.decimalPlaces,n=t!=null&&Number.isFinite(t)?Math.max(0,Math.floor(t)):void 0,r=e?.numberPlaces;return{decimalPlaces:n,numberPlaces:r!=null&&Number.isFinite(r)?Math.max(1,Math.floor(r)):void 0,segmented:e?.segmented??!0,separator:e?.separator??`,`,decimalSeparator:e?.decimalSeparator??`.`,prefix:e?.prefix??``,suffix:e?.suffix??``,allowNegative:e?.allowNegative??!1}}function M(e){let t=0;for(;t<e.length-1&&e[t]===`0`;)t++;return e.slice(t)}function N(e,t){if(!t||e.length<=3)return e;let n=[],r=e.length;for(;r>3;)n.unshift(e.slice(r-3,r)),r-=3;return n.unshift(e.slice(0,r)),n.join(t)}function P(e,t){if(t<=0)return 0;let n=0;for(let r=0;r<e.length;r++)if(A(e[r])&&(n++,n===t))return r+1;return e.length}function F(e,t){let n=0,r=``;t.prefix&&e.startsWith(t.prefix)?n=t.prefix.length:(t.allowNegative&&e[0]===`-`&&(r=`-`,n=1),t.prefix&&e.startsWith(t.prefix,n)&&(n+=t.prefix.length));let i=e.length;return t.suffix&&e.endsWith(t.suffix)&&i-t.suffix.length>=n&&(i-=t.suffix.length),{sign:r,body:e.slice(n,i),bodyStart:n}}function I(e,t){let n=F(e,t),r=``,i=``,a=n.sign===`-`,o=!1,s=t.decimalPlaces!==0;for(let e of n.body){if(A(e)){o?(t.decimalPlaces==null||i.length<t.decimalPlaces)&&(i+=e):(t.numberPlaces==null||r.length<t.numberPlaces)&&(r+=e);continue}if(s&&!o&&e===t.decimalSeparator){o=!0;continue}e===`-`&&t.allowNegative&&(a=!0)}return{isNegative:a,intDigits:r,fracDigits:i,hasSeparator:o}}function L(e,t,n){let r=n.decimalPlaces!==0,i=!1,a=0;for(let o=0;o<t;o++){let t=e[o];if(A(t)){i?(n.decimalPlaces==null||a<n.decimalPlaces)&&a++:(n.numberPlaces==null||a<n.numberPlaces)&&a++;continue}r&&!i&&t===n.decimalSeparator&&(i=!0,a=0)}return{inFraction:i,digitsBefore:a}}function R(e,t=0,n){let r=j(n);if(!e)return{value:``,caret:0};let{isNegative:i,intDigits:a,fracDigits:o,hasSeparator:s}=I(e,r);if(a===``&&o===``&&!s)return{value:``,caret:0};let c=M(a||`0`),l=r.numberPlaces==null?c:c.padStart(r.numberPlaces,`0`),u=r.segmented?N(l,r.separator):l,d=r.decimalPlaces!=null&&r.decimalPlaces>0?o.padEnd(r.decimalPlaces,`0`):o,f=u+(r.decimalPlaces!==0&&(r.decimalPlaces!=null||s)?r.decimalSeparator+d:``),p=i?`-`:``,m=p+r.prefix+f+r.suffix,h=F(e,r),g=Math.max(0,Math.min(t-h.bodyStart,h.body.length)),{inFraction:_,digitsBefore:v}=L(h.body,g,r),y=p.length+r.prefix.length,b=l.length-c.length;return{value:m,caret:_?y+u.length+r.decimalSeparator.length+v:y+P(u,v+b)}}function z(e,t){return R(e,e.length,t).value}function B(e,t){let{isNegative:n,intDigits:r,fracDigits:i}=I(e,j(t)),a=Number(i?`${r||`0`}.${i}`:r||`0`);return n?-a:a}function V(e,t){let n=j(t);if(n.decimalPlaces==null||n.decimalPlaces<=0)return null;let{isNegative:r,intDigits:i,hasSeparator:a}=I(e,n);if(a||i.length<n.decimalPlaces+1)return null;let o=i.length-n.decimalPlaces,s=i.slice(o),c=i.slice(0,o-1),l=r?`-`:``;return R(l+c+n.decimalSeparator+s,l.length+c.length,n)}function H(e,t,n,r){if(n<=0)return null;let i=t-n;if(i<0||t>e.length)return null;let a=e.slice(0,i)+e.slice(t),{bodyStart:o,body:s}=F(a,j(r)),c=o+s.length,l=i<o?o:i>c?c:-1;if(l<0)return null;let u=e.slice(i,t);return{value:a.slice(0,l)+u+a.slice(l),caret:l+n}}function U(e,t,n,r){let i=j(r),a=t-1;if(a<0||e[a]!==n)return null;let{isNegative:o,intDigits:s,fracDigits:c,hasSeparator:l}=I(e.slice(0,a)+e.slice(t),i),u=M(s||`0`),d=u!==`0`,f=d&&i.numberPlaces!=null&&u.length<i.numberPlaces;if(d&&!f)return null;let p=+!!o+i.prefix.length;if(a<p||a>p+s.length)return null;let m=o?`-`:``,h=(d?u:``)+n;return R(m+h+(l?i.decimalSeparator+c:``),m.length+h.length,i)}function W(e,t){let n=j(t);if(!Number.isFinite(e))return``;let r=n.allowNegative&&e<0,i=Math.abs(e),a=n.decimalPlaces==null?String(i):i.toFixed(n.decimalPlaces),o=a.indexOf(`.`),s=o===-1?a:a.slice(0,o),c=o===-1?``:a.slice(o+1),l=M(s||`0`),u=n.numberPlaces==null?l:l.padStart(n.numberPlaces,`0`),d=(n.segmented?N(u,n.separator):u)+(n.decimalPlaces!==0&&c!==``?n.decimalSeparator+c:``);return(r?`-`:``)+n.prefix+d+n.suffix}const G=`data-masked`;function K(e){return e==null?{}:typeof e==`function`?{onChange:e}:e}function q(e){return e.length===1&&e>=`0`&&e<=`9`}function J(e){try{return e.selectionStart??e.value.length}catch{return e.value.length}}function Y(e,t){try{e.setSelectionRange(t,t)}catch{}}function X(e,t){if(e.getAttribute(G)!==null)return()=>{};let{onChange:n,...r}=K(t),i=r,{decimalSeparator:a,decimalPlaces:o}=j(i),s=[],c=(t,n)=>{e.hasAttribute(t)||(e.setAttribute(t,n),s.push(t))};e.setAttribute(G,`decimal`),c(`autocomplete`,`off`),c(`autocorrect`,`off`),c(`autocapitalize`,`off`),c(`spellcheck`,`false`);let l=!1,u=!1,d=!1,f=null,p=x()?`keyup`:`keydown`,m=new Set,h=e=>{let t=requestAnimationFrame(()=>{m.delete(t),e()});m.add(t)},g=()=>{for(let e of m)cancelAnimationFrame(e);m.clear()},_=(e,t)=>{e.value=t.value,Y(e,t.caret),n?.(t.value,B(t.value,i))},v=(e,t={})=>{let n=J(e),r=(t,r)=>{if(o===0||t.length!==1||t!==`.`&&t!==`,`||t===a)return!1;for(let i of r)if(i!=null&&i>=0&&e.value.slice(i,i+t.length)===t)return e.value=e.value.slice(0,i)+a+e.value.slice(i+t.length),n=i+t.length<=n?n+a.length-t.length:n,Y(e,n),!0;return!1};if(f){let{text:e,starts:t}=f;f=null,r(e,t)}if(t.inputType===`deleteContentBackward`){let t=V(e.value,i);if(t){_(e,t);return}}let s=t.insertedText;if(s!=null&&r(s,[t.insertedAt,n-s.length]),s!=null&&s.length>0){let t=H(e.value,n,s.length,i);t&&(e.value=t.value,n=t.caret)}let c=s!=null&&s.length===1&&q(s)?s:void 0,l=c?U(e.value,n,c,i):null;_(e,l??R(e.value,n,i))},y=e=>{let t=e.target;h(()=>{v(t)})},b=e=>{let t=e,n=e.target;g(),l=!1,f=null,d=!0,v(n,{insertedText:typeof t.data==`string`?t.data:null,inputType:t.inputType})},S=()=>{u=!0,g(),l=!1,f=null},C=e=>{u=!1,d=!0,v(e.target)},w=e=>{let t=e,n=t.target,r=J(n),i=n.value;if(u)return;if(p===`keyup`&&d){d=!1;return}if(!t.key){l=!0,h(()=>{if(n.value===i&&n.selectionStart!==n.selectionEnd){l=!1;return}v(n),h(()=>{l=!1})});return}if(t.key===`Meta`)return;if(l){t.preventDefault();return}let s=t.key===`Backspace`,c=t.key===`Delete`,m=t.key.length===1&&!t.ctrlKey&&!t.altKey&&!t.metaKey,g=t.key===`Unidentified`;m&&o!==0&&(t.key===`.`||t.key===`,`)&&t.key!==a&&(f={text:t.key,starts:[r,r-t.key.length]}),!(!s&&!c&&!m&&!g)&&h(()=>{(n.value!==i||n.selectionStart===n.selectionEnd)&&v(n,{insertedText:m?t.key:null,insertedAt:m?r:void 0,inputType:s?`deleteContentBackward`:c?`deleteContentForward`:void 0})})};return e.addEventListener(`paste`,y),e.addEventListener(`input`,b),e.addEventListener(`compositionstart`,S),e.addEventListener(`compositionend`,C),e.addEventListener(p,w),()=>{e.removeEventListener(`paste`,y),e.removeEventListener(`input`,b),e.removeEventListener(`compositionstart`,S),e.removeEventListener(`compositionend`,C),e.removeEventListener(p,w),e.removeAttribute(G);for(let t of s)e.removeAttribute(t);g()}}var Z=class{caret;_value;_mask;_options;constructor(e,t,n=0,r){this._value=e,this._mask=t,this.caret=n,this._options=r}process(){let e=y(this._value,this._mask,this.caret,this._options);return this.caret=e.caret,e.value}};function Q(e,t,n=0,r){return new Z(e,t,n,r)}function $(e,t,n){return Q(e,t,0,n).process()}exports.Mask=Z,exports.applyDecimalMask=R,exports.applyMask=y,exports.bind=k,exports.bindDecimal=X,exports.buildMask=Q,exports.formatDecimalValue=W,exports.getMaxLength=l,exports.process=$,exports.processDecimal=z,exports.unmaskDecimal=B;
|
|
2
2
|
//# sourceMappingURL=mother-mask.cjs.map
|