@getflip/swirl-components 0.340.1 → 0.341.1

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 (35) hide show
  1. package/components.json +1 -1
  2. package/dist/cjs/{maska-BX2QG_K8.js → index-BOqO2Grl.js} +3593 -17
  3. package/dist/cjs/loader.cjs.js +1 -1
  4. package/dist/cjs/swirl-components.cjs.js +1 -1
  5. package/dist/cjs/swirl-date-input.cjs.entry.js +92 -37
  6. package/dist/cjs/swirl-file-viewer_8.cjs.entry.js +10 -3
  7. package/dist/cjs/swirl-time-input.cjs.entry.js +78 -49
  8. package/dist/collection/components/swirl-date-input/swirl-date-input.js +88 -33
  9. package/dist/collection/components/swirl-date-input/swirl-date-input.stories.js +1 -1
  10. package/dist/collection/components/swirl-file-viewer/viewers/swirl-file-viewer-pdf/swirl-file-viewer-pdf.js +10 -3
  11. package/dist/collection/components/swirl-time-input/swirl-time-input.js +77 -46
  12. package/dist/collection/components/swirl-time-input/swirl-time-input.stories.js +1 -1
  13. package/dist/components/assets/pdfjs/pdf.worker.min.mjs +4 -1
  14. package/dist/components/{maska.js → index3.js} +3593 -17
  15. package/dist/components/swirl-date-input.js +88 -33
  16. package/dist/components/swirl-file-viewer-pdf2.js +10 -3
  17. package/dist/components/swirl-time-input.js +78 -48
  18. package/dist/esm/{maska-CSSJQ9ZE.js → index-D3UoejeY.js} +3593 -17
  19. package/dist/esm/loader.js +1 -1
  20. package/dist/esm/swirl-components.js +1 -1
  21. package/dist/esm/swirl-date-input.entry.js +88 -33
  22. package/dist/esm/swirl-file-viewer_8.entry.js +10 -3
  23. package/dist/esm/swirl-time-input.entry.js +76 -47
  24. package/dist/swirl-components/{p-ca695458.entry.js → p-01748667.entry.js} +1 -1
  25. package/dist/swirl-components/p-1bbb3a4b.entry.js +1 -0
  26. package/dist/swirl-components/p-96441b7e.entry.js +1 -0
  27. package/dist/swirl-components/p-D3UoejeY.js +1 -0
  28. package/dist/swirl-components/swirl-components.esm.js +1 -1
  29. package/dist/types/components/swirl-date-input/swirl-date-input.d.ts +2 -2
  30. package/dist/types/components/swirl-file-viewer/viewers/swirl-file-viewer-pdf/swirl-file-viewer-pdf.d.ts +1 -0
  31. package/dist/types/components/swirl-time-input/swirl-time-input.d.ts +1 -2
  32. package/package.json +2 -2
  33. package/dist/swirl-components/p-1217fdca.entry.js +0 -1
  34. package/dist/swirl-components/p-CSSJQ9ZE.js +0 -6
  35. package/dist/swirl-components/p-b96b8029.entry.js +0 -1
@@ -6240,29 +6240,3605 @@ function cleanEscapedString(input) {
6240
6240
  return input.match(escapedStringRegExp)[1].replace(doubleQuoteRegExp, "'");
6241
6241
  }
6242
6242
 
6243
- var maska$1 = {exports: {}};
6243
+ /** Checks if value is string */
6244
+ function isString(str) {
6245
+ return typeof str === 'string' || str instanceof String;
6246
+ }
6247
+
6248
+ /** Checks if value is object */
6249
+ function isObject(obj) {
6250
+ var _obj$constructor;
6251
+ return typeof obj === 'object' && obj != null && (obj == null || (_obj$constructor = obj.constructor) == null ? void 0 : _obj$constructor.name) === 'Object';
6252
+ }
6253
+ function pick(obj, keys) {
6254
+ if (Array.isArray(keys)) return pick(obj, (_, k) => keys.includes(k));
6255
+ return Object.entries(obj).reduce((acc, _ref) => {
6256
+ let [k, v] = _ref;
6257
+ if (keys(v, k)) acc[k] = v;
6258
+ return acc;
6259
+ }, {});
6260
+ }
6261
+
6262
+ /** Direction */
6263
+ const DIRECTION = {
6264
+ NONE: 'NONE',
6265
+ LEFT: 'LEFT',
6266
+ FORCE_LEFT: 'FORCE_LEFT',
6267
+ RIGHT: 'RIGHT',
6268
+ FORCE_RIGHT: 'FORCE_RIGHT'
6269
+ };
6270
+
6271
+ /** Direction */
6272
+
6273
+ function forceDirection(direction) {
6274
+ switch (direction) {
6275
+ case DIRECTION.LEFT:
6276
+ return DIRECTION.FORCE_LEFT;
6277
+ case DIRECTION.RIGHT:
6278
+ return DIRECTION.FORCE_RIGHT;
6279
+ default:
6280
+ return direction;
6281
+ }
6282
+ }
6283
+
6284
+ /** Escapes regular expression control chars */
6285
+ function escapeRegExp(str) {
6286
+ return str.replace(/([.*+?^=!:${}()|[\]/\\])/g, '\\$1');
6287
+ }
6288
+
6289
+ // cloned from https://github.com/epoberezkin/fast-deep-equal with small changes
6290
+ function objectIncludes(b, a) {
6291
+ if (a === b) return true;
6292
+ const arrA = Array.isArray(a),
6293
+ arrB = Array.isArray(b);
6294
+ let i;
6295
+ if (arrA && arrB) {
6296
+ if (a.length != b.length) return false;
6297
+ for (i = 0; i < a.length; i++) if (!objectIncludes(a[i], b[i])) return false;
6298
+ return true;
6299
+ }
6300
+ if (arrA != arrB) return false;
6301
+ if (a && b && typeof a === 'object' && typeof b === 'object') {
6302
+ const dateA = a instanceof Date,
6303
+ dateB = b instanceof Date;
6304
+ if (dateA && dateB) return a.getTime() == b.getTime();
6305
+ if (dateA != dateB) return false;
6306
+ const regexpA = a instanceof RegExp,
6307
+ regexpB = b instanceof RegExp;
6308
+ if (regexpA && regexpB) return a.toString() == b.toString();
6309
+ if (regexpA != regexpB) return false;
6310
+ const keys = Object.keys(a);
6311
+ // if (keys.length !== Object.keys(b).length) return false;
6312
+
6313
+ for (i = 0; i < keys.length; i++) if (!Object.prototype.hasOwnProperty.call(b, keys[i])) return false;
6314
+ for (i = 0; i < keys.length; i++) if (!objectIncludes(b[keys[i]], a[keys[i]])) return false;
6315
+ return true;
6316
+ } else if (a && b && typeof a === 'function' && typeof b === 'function') {
6317
+ return a.toString() === b.toString();
6318
+ }
6319
+ return false;
6320
+ }
6321
+
6322
+ /** Provides details of changing input */
6323
+ class ActionDetails {
6324
+ /** Current input value */
6325
+
6326
+ /** Current cursor position */
6327
+
6328
+ /** Old input value */
6329
+
6330
+ /** Old selection */
6331
+
6332
+ constructor(opts) {
6333
+ Object.assign(this, opts);
6334
+
6335
+ // double check if left part was changed (autofilling, other non-standard input triggers)
6336
+ while (this.value.slice(0, this.startChangePos) !== this.oldValue.slice(0, this.startChangePos)) {
6337
+ --this.oldSelection.start;
6338
+ }
6339
+ if (this.insertedCount) {
6340
+ // double check right part
6341
+ while (this.value.slice(this.cursorPos) !== this.oldValue.slice(this.oldSelection.end)) {
6342
+ if (this.value.length - this.cursorPos < this.oldValue.length - this.oldSelection.end) ++this.oldSelection.end;else ++this.cursorPos;
6343
+ }
6344
+ }
6345
+ }
6346
+
6347
+ /** Start changing position */
6348
+ get startChangePos() {
6349
+ return Math.min(this.cursorPos, this.oldSelection.start);
6350
+ }
6351
+
6352
+ /** Inserted symbols count */
6353
+ get insertedCount() {
6354
+ return this.cursorPos - this.startChangePos;
6355
+ }
6356
+
6357
+ /** Inserted symbols */
6358
+ get inserted() {
6359
+ return this.value.substr(this.startChangePos, this.insertedCount);
6360
+ }
6361
+
6362
+ /** Removed symbols count */
6363
+ get removedCount() {
6364
+ // Math.max for opposite operation
6365
+ return Math.max(this.oldSelection.end - this.startChangePos ||
6366
+ // for Delete
6367
+ this.oldValue.length - this.value.length, 0);
6368
+ }
6369
+
6370
+ /** Removed symbols */
6371
+ get removed() {
6372
+ return this.oldValue.substr(this.startChangePos, this.removedCount);
6373
+ }
6374
+
6375
+ /** Unchanged head symbols */
6376
+ get head() {
6377
+ return this.value.substring(0, this.startChangePos);
6378
+ }
6379
+
6380
+ /** Unchanged tail symbols */
6381
+ get tail() {
6382
+ return this.value.substring(this.startChangePos + this.insertedCount);
6383
+ }
6384
+
6385
+ /** Remove direction */
6386
+ get removeDirection() {
6387
+ if (!this.removedCount || this.insertedCount) return DIRECTION.NONE;
6388
+
6389
+ // align right if delete at right
6390
+ return (this.oldSelection.end === this.cursorPos || this.oldSelection.start === this.cursorPos) &&
6391
+ // if not range removed (event with backspace)
6392
+ this.oldSelection.end === this.oldSelection.start ? DIRECTION.RIGHT : DIRECTION.LEFT;
6393
+ }
6394
+ }
6395
+
6396
+ /** Applies mask on element */
6397
+ function IMask(el, opts) {
6398
+ // currently available only for input-like elements
6399
+ return new IMask.InputMask(el, opts);
6400
+ }
6401
+
6402
+ // TODO can't use overloads here because of https://github.com/microsoft/TypeScript/issues/50754
6403
+ // export function maskedClass(mask: string): typeof MaskedPattern;
6404
+ // export function maskedClass(mask: DateConstructor): typeof MaskedDate;
6405
+ // export function maskedClass(mask: NumberConstructor): typeof MaskedNumber;
6406
+ // export function maskedClass(mask: Array<any> | ArrayConstructor): typeof MaskedDynamic;
6407
+ // export function maskedClass(mask: MaskedDate): typeof MaskedDate;
6408
+ // export function maskedClass(mask: MaskedNumber): typeof MaskedNumber;
6409
+ // export function maskedClass(mask: MaskedEnum): typeof MaskedEnum;
6410
+ // export function maskedClass(mask: MaskedRange): typeof MaskedRange;
6411
+ // export function maskedClass(mask: MaskedRegExp): typeof MaskedRegExp;
6412
+ // export function maskedClass(mask: MaskedFunction): typeof MaskedFunction;
6413
+ // export function maskedClass(mask: MaskedPattern): typeof MaskedPattern;
6414
+ // export function maskedClass(mask: MaskedDynamic): typeof MaskedDynamic;
6415
+ // export function maskedClass(mask: Masked): typeof Masked;
6416
+ // export function maskedClass(mask: typeof Masked): typeof Masked;
6417
+ // export function maskedClass(mask: typeof MaskedDate): typeof MaskedDate;
6418
+ // export function maskedClass(mask: typeof MaskedNumber): typeof MaskedNumber;
6419
+ // export function maskedClass(mask: typeof MaskedEnum): typeof MaskedEnum;
6420
+ // export function maskedClass(mask: typeof MaskedRange): typeof MaskedRange;
6421
+ // export function maskedClass(mask: typeof MaskedRegExp): typeof MaskedRegExp;
6422
+ // export function maskedClass(mask: typeof MaskedFunction): typeof MaskedFunction;
6423
+ // export function maskedClass(mask: typeof MaskedPattern): typeof MaskedPattern;
6424
+ // export function maskedClass(mask: typeof MaskedDynamic): typeof MaskedDynamic;
6425
+ // export function maskedClass<Mask extends typeof Masked> (mask: Mask): Mask;
6426
+ // export function maskedClass(mask: RegExp): typeof MaskedRegExp;
6427
+ // export function maskedClass(mask: (value: string, ...args: any[]) => boolean): typeof MaskedFunction;
6428
+
6429
+ /** Get Masked class by mask type */
6430
+ function maskedClass(mask) /* TODO */{
6431
+ if (mask == null) throw new Error('mask property should be defined');
6432
+ if (mask instanceof RegExp) return IMask.MaskedRegExp;
6433
+ if (isString(mask)) return IMask.MaskedPattern;
6434
+ if (mask === Date) return IMask.MaskedDate;
6435
+ if (mask === Number) return IMask.MaskedNumber;
6436
+ if (Array.isArray(mask) || mask === Array) return IMask.MaskedDynamic;
6437
+ if (IMask.Masked && mask.prototype instanceof IMask.Masked) return mask;
6438
+ if (IMask.Masked && mask instanceof IMask.Masked) return mask.constructor;
6439
+ if (mask instanceof Function) return IMask.MaskedFunction;
6440
+ console.warn('Mask not found for mask', mask); // eslint-disable-line no-console
6441
+ return IMask.Masked;
6442
+ }
6443
+ function normalizeOpts(opts) {
6444
+ if (!opts) throw new Error('Options in not defined');
6445
+ if (IMask.Masked) {
6446
+ if (opts.prototype instanceof IMask.Masked) return {
6447
+ mask: opts
6448
+ };
6449
+
6450
+ /*
6451
+ handle cases like:
6452
+ 1) opts = Masked
6453
+ 2) opts = { mask: Masked, ...instanceOpts }
6454
+ */
6455
+ const {
6456
+ mask = undefined,
6457
+ ...instanceOpts
6458
+ } = opts instanceof IMask.Masked ? {
6459
+ mask: opts
6460
+ } : isObject(opts) && opts.mask instanceof IMask.Masked ? opts : {};
6461
+ if (mask) {
6462
+ const _mask = mask.mask;
6463
+ return {
6464
+ ...pick(mask, (_, k) => !k.startsWith('_')),
6465
+ mask: mask.constructor,
6466
+ _mask,
6467
+ ...instanceOpts
6468
+ };
6469
+ }
6470
+ }
6471
+ if (!isObject(opts)) return {
6472
+ mask: opts
6473
+ };
6474
+ return {
6475
+ ...opts
6476
+ };
6477
+ }
6478
+
6479
+ // TODO can't use overloads here because of https://github.com/microsoft/TypeScript/issues/50754
6480
+
6481
+ // From masked
6482
+ // export default function createMask<Opts extends Masked, ReturnMasked=Opts> (opts: Opts): ReturnMasked;
6483
+ // // From masked class
6484
+ // export default function createMask<Opts extends MaskedOptions<typeof Masked>, ReturnMasked extends Masked=InstanceType<Opts['mask']>> (opts: Opts): ReturnMasked;
6485
+ // export default function createMask<Opts extends MaskedOptions<typeof MaskedDate>, ReturnMasked extends MaskedDate=MaskedDate<Opts['parent']>> (opts: Opts): ReturnMasked;
6486
+ // export default function createMask<Opts extends MaskedOptions<typeof MaskedNumber>, ReturnMasked extends MaskedNumber=MaskedNumber<Opts['parent']>> (opts: Opts): ReturnMasked;
6487
+ // export default function createMask<Opts extends MaskedOptions<typeof MaskedEnum>, ReturnMasked extends MaskedEnum=MaskedEnum<Opts['parent']>> (opts: Opts): ReturnMasked;
6488
+ // export default function createMask<Opts extends MaskedOptions<typeof MaskedRange>, ReturnMasked extends MaskedRange=MaskedRange<Opts['parent']>> (opts: Opts): ReturnMasked;
6489
+ // export default function createMask<Opts extends MaskedOptions<typeof MaskedRegExp>, ReturnMasked extends MaskedRegExp=MaskedRegExp<Opts['parent']>> (opts: Opts): ReturnMasked;
6490
+ // export default function createMask<Opts extends MaskedOptions<typeof MaskedFunction>, ReturnMasked extends MaskedFunction=MaskedFunction<Opts['parent']>> (opts: Opts): ReturnMasked;
6491
+ // export default function createMask<Opts extends MaskedOptions<typeof MaskedPattern>, ReturnMasked extends MaskedPattern=MaskedPattern<Opts['parent']>> (opts: Opts): ReturnMasked;
6492
+ // export default function createMask<Opts extends MaskedOptions<typeof MaskedDynamic>, ReturnMasked extends MaskedDynamic=MaskedDynamic<Opts['parent']>> (opts: Opts): ReturnMasked;
6493
+ // // From mask opts
6494
+ // export default function createMask<Opts extends MaskedOptions<Masked>, ReturnMasked=Opts extends MaskedOptions<infer M> ? M : never> (opts: Opts): ReturnMasked;
6495
+ // export default function createMask<Opts extends MaskedNumberOptions, ReturnMasked extends MaskedNumber=MaskedNumber<Opts['parent']>> (opts: Opts): ReturnMasked;
6496
+ // export default function createMask<Opts extends MaskedDateFactoryOptions, ReturnMasked extends MaskedDate=MaskedDate<Opts['parent']>> (opts: Opts): ReturnMasked;
6497
+ // export default function createMask<Opts extends MaskedEnumOptions, ReturnMasked extends MaskedEnum=MaskedEnum<Opts['parent']>> (opts: Opts): ReturnMasked;
6498
+ // export default function createMask<Opts extends MaskedRangeOptions, ReturnMasked extends MaskedRange=MaskedRange<Opts['parent']>> (opts: Opts): ReturnMasked;
6499
+ // export default function createMask<Opts extends MaskedPatternOptions, ReturnMasked extends MaskedPattern=MaskedPattern<Opts['parent']>> (opts: Opts): ReturnMasked;
6500
+ // export default function createMask<Opts extends MaskedDynamicOptions, ReturnMasked extends MaskedDynamic=MaskedDynamic<Opts['parent']>> (opts: Opts): ReturnMasked;
6501
+ // export default function createMask<Opts extends MaskedOptions<RegExp>, ReturnMasked extends MaskedRegExp=MaskedRegExp<Opts['parent']>> (opts: Opts): ReturnMasked;
6502
+ // export default function createMask<Opts extends MaskedOptions<Function>, ReturnMasked extends MaskedFunction=MaskedFunction<Opts['parent']>> (opts: Opts): ReturnMasked;
6503
+
6504
+ /** Creates new {@link Masked} depending on mask type */
6505
+ function createMask(opts) {
6506
+ if (IMask.Masked && opts instanceof IMask.Masked) return opts;
6507
+ const nOpts = normalizeOpts(opts);
6508
+ const MaskedClass = maskedClass(nOpts.mask);
6509
+ if (!MaskedClass) throw new Error("Masked class is not found for provided mask " + nOpts.mask + ", appropriate module needs to be imported manually before creating mask.");
6510
+ if (nOpts.mask === MaskedClass) delete nOpts.mask;
6511
+ if (nOpts._mask) {
6512
+ nOpts.mask = nOpts._mask;
6513
+ delete nOpts._mask;
6514
+ }
6515
+ return new MaskedClass(nOpts);
6516
+ }
6517
+ IMask.createMask = createMask;
6518
+
6519
+ /** Generic element API to use with mask */
6520
+ class MaskElement {
6521
+ /** */
6522
+
6523
+ /** */
6524
+
6525
+ /** */
6526
+
6527
+ /** Safely returns selection start */
6528
+ get selectionStart() {
6529
+ let start;
6530
+ try {
6531
+ start = this._unsafeSelectionStart;
6532
+ } catch {}
6533
+ return start != null ? start : this.value.length;
6534
+ }
6535
+
6536
+ /** Safely returns selection end */
6537
+ get selectionEnd() {
6538
+ let end;
6539
+ try {
6540
+ end = this._unsafeSelectionEnd;
6541
+ } catch {}
6542
+ return end != null ? end : this.value.length;
6543
+ }
6544
+
6545
+ /** Safely sets element selection */
6546
+ select(start, end) {
6547
+ if (start == null || end == null || start === this.selectionStart && end === this.selectionEnd) return;
6548
+ try {
6549
+ this._unsafeSelect(start, end);
6550
+ } catch {}
6551
+ }
6552
+
6553
+ /** */
6554
+ get isActive() {
6555
+ return false;
6556
+ }
6557
+ /** */
6558
+
6559
+ /** */
6560
+
6561
+ /** */
6562
+ }
6563
+ IMask.MaskElement = MaskElement;
6564
+
6565
+ const KEY_Z = 90;
6566
+ const KEY_Y = 89;
6567
+
6568
+ /** Bridge between HTMLElement and {@link Masked} */
6569
+ class HTMLMaskElement extends MaskElement {
6570
+ /** HTMLElement to use mask on */
6571
+
6572
+ constructor(input) {
6573
+ super();
6574
+ this.input = input;
6575
+ this._onKeydown = this._onKeydown.bind(this);
6576
+ this._onInput = this._onInput.bind(this);
6577
+ this._onBeforeinput = this._onBeforeinput.bind(this);
6578
+ this._onCompositionEnd = this._onCompositionEnd.bind(this);
6579
+ }
6580
+ get rootElement() {
6581
+ var _this$input$getRootNo, _this$input$getRootNo2, _this$input;
6582
+ return (_this$input$getRootNo = (_this$input$getRootNo2 = (_this$input = this.input).getRootNode) == null ? void 0 : _this$input$getRootNo2.call(_this$input)) != null ? _this$input$getRootNo : document;
6583
+ }
6584
+
6585
+ /** Is element in focus */
6586
+ get isActive() {
6587
+ return this.input === this.rootElement.activeElement;
6588
+ }
6589
+
6590
+ /** Binds HTMLElement events to mask internal events */
6591
+ bindEvents(handlers) {
6592
+ this.input.addEventListener('keydown', this._onKeydown);
6593
+ this.input.addEventListener('input', this._onInput);
6594
+ this.input.addEventListener('beforeinput', this._onBeforeinput);
6595
+ this.input.addEventListener('compositionend', this._onCompositionEnd);
6596
+ this.input.addEventListener('drop', handlers.drop);
6597
+ this.input.addEventListener('click', handlers.click);
6598
+ this.input.addEventListener('focus', handlers.focus);
6599
+ this.input.addEventListener('blur', handlers.commit);
6600
+ this._handlers = handlers;
6601
+ }
6602
+ _onKeydown(e) {
6603
+ if (this._handlers.redo && (e.keyCode === KEY_Z && e.shiftKey && (e.metaKey || e.ctrlKey) || e.keyCode === KEY_Y && e.ctrlKey)) {
6604
+ e.preventDefault();
6605
+ return this._handlers.redo(e);
6606
+ }
6607
+ if (this._handlers.undo && e.keyCode === KEY_Z && (e.metaKey || e.ctrlKey)) {
6608
+ e.preventDefault();
6609
+ return this._handlers.undo(e);
6610
+ }
6611
+ if (!e.isComposing) this._handlers.selectionChange(e);
6612
+ }
6613
+ _onBeforeinput(e) {
6614
+ if (e.inputType === 'historyUndo' && this._handlers.undo) {
6615
+ e.preventDefault();
6616
+ return this._handlers.undo(e);
6617
+ }
6618
+ if (e.inputType === 'historyRedo' && this._handlers.redo) {
6619
+ e.preventDefault();
6620
+ return this._handlers.redo(e);
6621
+ }
6622
+ }
6623
+ _onCompositionEnd(e) {
6624
+ this._handlers.input(e);
6625
+ }
6626
+ _onInput(e) {
6627
+ if (!e.isComposing) this._handlers.input(e);
6628
+ }
6629
+
6630
+ /** Unbinds HTMLElement events to mask internal events */
6631
+ unbindEvents() {
6632
+ this.input.removeEventListener('keydown', this._onKeydown);
6633
+ this.input.removeEventListener('input', this._onInput);
6634
+ this.input.removeEventListener('beforeinput', this._onBeforeinput);
6635
+ this.input.removeEventListener('compositionend', this._onCompositionEnd);
6636
+ this.input.removeEventListener('drop', this._handlers.drop);
6637
+ this.input.removeEventListener('click', this._handlers.click);
6638
+ this.input.removeEventListener('focus', this._handlers.focus);
6639
+ this.input.removeEventListener('blur', this._handlers.commit);
6640
+ this._handlers = {};
6641
+ }
6642
+ }
6643
+ IMask.HTMLMaskElement = HTMLMaskElement;
6644
+
6645
+ /** Bridge between InputElement and {@link Masked} */
6646
+ class HTMLInputMaskElement extends HTMLMaskElement {
6647
+ /** InputElement to use mask on */
6648
+
6649
+ constructor(input) {
6650
+ super(input);
6651
+ this.input = input;
6652
+ }
6653
+
6654
+ /** Returns InputElement selection start */
6655
+ get _unsafeSelectionStart() {
6656
+ return this.input.selectionStart != null ? this.input.selectionStart : this.value.length;
6657
+ }
6658
+
6659
+ /** Returns InputElement selection end */
6660
+ get _unsafeSelectionEnd() {
6661
+ return this.input.selectionEnd;
6662
+ }
6663
+
6664
+ /** Sets InputElement selection */
6665
+ _unsafeSelect(start, end) {
6666
+ this.input.setSelectionRange(start, end);
6667
+ }
6668
+ get value() {
6669
+ return this.input.value;
6670
+ }
6671
+ set value(value) {
6672
+ this.input.value = value;
6673
+ }
6674
+ }
6675
+ IMask.HTMLMaskElement = HTMLMaskElement;
6676
+
6677
+ class HTMLContenteditableMaskElement extends HTMLMaskElement {
6678
+ /** Returns HTMLElement selection start */
6679
+ get _unsafeSelectionStart() {
6680
+ const root = this.rootElement;
6681
+ const selection = root.getSelection && root.getSelection();
6682
+ const anchorOffset = selection && selection.anchorOffset;
6683
+ const focusOffset = selection && selection.focusOffset;
6684
+ if (focusOffset == null || anchorOffset == null || anchorOffset < focusOffset) {
6685
+ return anchorOffset;
6686
+ }
6687
+ return focusOffset;
6688
+ }
6689
+
6690
+ /** Returns HTMLElement selection end */
6691
+ get _unsafeSelectionEnd() {
6692
+ const root = this.rootElement;
6693
+ const selection = root.getSelection && root.getSelection();
6694
+ const anchorOffset = selection && selection.anchorOffset;
6695
+ const focusOffset = selection && selection.focusOffset;
6696
+ if (focusOffset == null || anchorOffset == null || anchorOffset > focusOffset) {
6697
+ return anchorOffset;
6698
+ }
6699
+ return focusOffset;
6700
+ }
6701
+
6702
+ /** Sets HTMLElement selection */
6703
+ _unsafeSelect(start, end) {
6704
+ if (!this.rootElement.createRange) return;
6705
+ const range = this.rootElement.createRange();
6706
+ range.setStart(this.input.firstChild || this.input, start);
6707
+ range.setEnd(this.input.lastChild || this.input, end);
6708
+ const root = this.rootElement;
6709
+ const selection = root.getSelection && root.getSelection();
6710
+ if (selection) {
6711
+ selection.removeAllRanges();
6712
+ selection.addRange(range);
6713
+ }
6714
+ }
6715
+
6716
+ /** HTMLElement value */
6717
+ get value() {
6718
+ return this.input.textContent || '';
6719
+ }
6720
+ set value(value) {
6721
+ this.input.textContent = value;
6722
+ }
6723
+ }
6724
+ IMask.HTMLContenteditableMaskElement = HTMLContenteditableMaskElement;
6725
+
6726
+ class InputHistory {
6727
+ constructor() {
6728
+ this.states = [];
6729
+ this.currentIndex = 0;
6730
+ }
6731
+ get currentState() {
6732
+ return this.states[this.currentIndex];
6733
+ }
6734
+ get isEmpty() {
6735
+ return this.states.length === 0;
6736
+ }
6737
+ push(state) {
6738
+ // if current index points before the last element then remove the future
6739
+ if (this.currentIndex < this.states.length - 1) this.states.length = this.currentIndex + 1;
6740
+ this.states.push(state);
6741
+ if (this.states.length > InputHistory.MAX_LENGTH) this.states.shift();
6742
+ this.currentIndex = this.states.length - 1;
6743
+ }
6744
+ go(steps) {
6745
+ this.currentIndex = Math.min(Math.max(this.currentIndex + steps, 0), this.states.length - 1);
6746
+ return this.currentState;
6747
+ }
6748
+ undo() {
6749
+ return this.go(-1);
6750
+ }
6751
+ redo() {
6752
+ return this.go(1);
6753
+ }
6754
+ clear() {
6755
+ this.states.length = 0;
6756
+ this.currentIndex = 0;
6757
+ }
6758
+ }
6759
+ InputHistory.MAX_LENGTH = 100;
6760
+
6761
+ /** Listens to element events and controls changes between element and {@link Masked} */
6762
+ class InputMask {
6763
+ /**
6764
+ View element
6765
+ */
6766
+
6767
+ /** Internal {@link Masked} model */
6768
+
6769
+ constructor(el, opts) {
6770
+ this.el = el instanceof MaskElement ? el : el.isContentEditable && el.tagName !== 'INPUT' && el.tagName !== 'TEXTAREA' ? new HTMLContenteditableMaskElement(el) : new HTMLInputMaskElement(el);
6771
+ this.masked = createMask(opts);
6772
+ this._listeners = {};
6773
+ this._value = '';
6774
+ this._unmaskedValue = '';
6775
+ this._rawInputValue = '';
6776
+ this.history = new InputHistory();
6777
+ this._saveSelection = this._saveSelection.bind(this);
6778
+ this._onInput = this._onInput.bind(this);
6779
+ this._onChange = this._onChange.bind(this);
6780
+ this._onDrop = this._onDrop.bind(this);
6781
+ this._onFocus = this._onFocus.bind(this);
6782
+ this._onClick = this._onClick.bind(this);
6783
+ this._onUndo = this._onUndo.bind(this);
6784
+ this._onRedo = this._onRedo.bind(this);
6785
+ this.alignCursor = this.alignCursor.bind(this);
6786
+ this.alignCursorFriendly = this.alignCursorFriendly.bind(this);
6787
+ this._bindEvents();
6788
+
6789
+ // refresh
6790
+ this.updateValue();
6791
+ this._onChange();
6792
+ }
6793
+ maskEquals(mask) {
6794
+ var _this$masked;
6795
+ return mask == null || ((_this$masked = this.masked) == null ? void 0 : _this$masked.maskEquals(mask));
6796
+ }
6797
+
6798
+ /** Masked */
6799
+ get mask() {
6800
+ return this.masked.mask;
6801
+ }
6802
+ set mask(mask) {
6803
+ if (this.maskEquals(mask)) return;
6804
+ if (!(mask instanceof IMask.Masked) && this.masked.constructor === maskedClass(mask)) {
6805
+ // TODO "any" no idea
6806
+ this.masked.updateOptions({
6807
+ mask
6808
+ });
6809
+ return;
6810
+ }
6811
+ const masked = mask instanceof IMask.Masked ? mask : createMask({
6812
+ mask
6813
+ });
6814
+ masked.unmaskedValue = this.masked.unmaskedValue;
6815
+ this.masked = masked;
6816
+ }
6817
+
6818
+ /** Raw value */
6819
+ get value() {
6820
+ return this._value;
6821
+ }
6822
+ set value(str) {
6823
+ if (this.value === str) return;
6824
+ this.masked.value = str;
6825
+ this.updateControl('auto');
6826
+ }
6827
+
6828
+ /** Unmasked value */
6829
+ get unmaskedValue() {
6830
+ return this._unmaskedValue;
6831
+ }
6832
+ set unmaskedValue(str) {
6833
+ if (this.unmaskedValue === str) return;
6834
+ this.masked.unmaskedValue = str;
6835
+ this.updateControl('auto');
6836
+ }
6837
+
6838
+ /** Raw input value */
6839
+ get rawInputValue() {
6840
+ return this._rawInputValue;
6841
+ }
6842
+ set rawInputValue(str) {
6843
+ if (this.rawInputValue === str) return;
6844
+ this.masked.rawInputValue = str;
6845
+ this.updateControl();
6846
+ this.alignCursor();
6847
+ }
6848
+
6849
+ /** Typed unmasked value */
6850
+ get typedValue() {
6851
+ return this.masked.typedValue;
6852
+ }
6853
+ set typedValue(val) {
6854
+ if (this.masked.typedValueEquals(val)) return;
6855
+ this.masked.typedValue = val;
6856
+ this.updateControl('auto');
6857
+ }
6858
+
6859
+ /** Display value */
6860
+ get displayValue() {
6861
+ return this.masked.displayValue;
6862
+ }
6863
+
6864
+ /** Starts listening to element events */
6865
+ _bindEvents() {
6866
+ this.el.bindEvents({
6867
+ selectionChange: this._saveSelection,
6868
+ input: this._onInput,
6869
+ drop: this._onDrop,
6870
+ click: this._onClick,
6871
+ focus: this._onFocus,
6872
+ commit: this._onChange,
6873
+ undo: this._onUndo,
6874
+ redo: this._onRedo
6875
+ });
6876
+ }
6877
+
6878
+ /** Stops listening to element events */
6879
+ _unbindEvents() {
6880
+ if (this.el) this.el.unbindEvents();
6881
+ }
6882
+
6883
+ /** Fires custom event */
6884
+ _fireEvent(ev, e) {
6885
+ const listeners = this._listeners[ev];
6886
+ if (!listeners) return;
6887
+ listeners.forEach(l => l(e));
6888
+ }
6889
+
6890
+ /** Current selection start */
6891
+ get selectionStart() {
6892
+ return this._cursorChanging ? this._changingCursorPos : this.el.selectionStart;
6893
+ }
6894
+
6895
+ /** Current cursor position */
6896
+ get cursorPos() {
6897
+ return this._cursorChanging ? this._changingCursorPos : this.el.selectionEnd;
6898
+ }
6899
+ set cursorPos(pos) {
6900
+ if (!this.el || !this.el.isActive) return;
6901
+ this.el.select(pos, pos);
6902
+ this._saveSelection();
6903
+ }
6904
+
6905
+ /** Stores current selection */
6906
+ _saveSelection( /* ev */
6907
+ ) {
6908
+ if (this.displayValue !== this.el.value) {
6909
+ console.warn('Element value was changed outside of mask. Syncronize mask using `mask.updateValue()` to work properly.'); // eslint-disable-line no-console
6910
+ }
6911
+ this._selection = {
6912
+ start: this.selectionStart,
6913
+ end: this.cursorPos
6914
+ };
6915
+ }
6916
+
6917
+ /** Syncronizes model value from view */
6918
+ updateValue() {
6919
+ this.masked.value = this.el.value;
6920
+ this._value = this.masked.value;
6921
+ this._unmaskedValue = this.masked.unmaskedValue;
6922
+ this._rawInputValue = this.masked.rawInputValue;
6923
+ }
6924
+
6925
+ /** Syncronizes view from model value, fires change events */
6926
+ updateControl(cursorPos) {
6927
+ const newUnmaskedValue = this.masked.unmaskedValue;
6928
+ const newValue = this.masked.value;
6929
+ const newRawInputValue = this.masked.rawInputValue;
6930
+ const newDisplayValue = this.displayValue;
6931
+ const isChanged = this.unmaskedValue !== newUnmaskedValue || this.value !== newValue || this._rawInputValue !== newRawInputValue;
6932
+ this._unmaskedValue = newUnmaskedValue;
6933
+ this._value = newValue;
6934
+ this._rawInputValue = newRawInputValue;
6935
+ if (this.el.value !== newDisplayValue) this.el.value = newDisplayValue;
6936
+ if (cursorPos === 'auto') this.alignCursor();else if (cursorPos != null) this.cursorPos = cursorPos;
6937
+ if (isChanged) this._fireChangeEvents();
6938
+ if (!this._historyChanging && (isChanged || this.history.isEmpty)) this.history.push({
6939
+ unmaskedValue: newUnmaskedValue,
6940
+ selection: {
6941
+ start: this.selectionStart,
6942
+ end: this.cursorPos
6943
+ }
6944
+ });
6945
+ }
6946
+
6947
+ /** Updates options with deep equal check, recreates {@link Masked} model if mask type changes */
6948
+ updateOptions(opts) {
6949
+ const {
6950
+ mask,
6951
+ ...restOpts
6952
+ } = opts; // TODO types, yes, mask is optional
6953
+
6954
+ const updateMask = !this.maskEquals(mask);
6955
+ const updateOpts = this.masked.optionsIsChanged(restOpts);
6956
+ if (updateMask) this.mask = mask;
6957
+ if (updateOpts) this.masked.updateOptions(restOpts); // TODO
6958
+
6959
+ if (updateMask || updateOpts) this.updateControl();
6960
+ }
6961
+
6962
+ /** Updates cursor */
6963
+ updateCursor(cursorPos) {
6964
+ if (cursorPos == null) return;
6965
+ this.cursorPos = cursorPos;
6966
+
6967
+ // also queue change cursor for mobile browsers
6968
+ this._delayUpdateCursor(cursorPos);
6969
+ }
6970
+
6971
+ /** Delays cursor update to support mobile browsers */
6972
+ _delayUpdateCursor(cursorPos) {
6973
+ this._abortUpdateCursor();
6974
+ this._changingCursorPos = cursorPos;
6975
+ this._cursorChanging = setTimeout(() => {
6976
+ if (!this.el) return; // if was destroyed
6977
+ this.cursorPos = this._changingCursorPos;
6978
+ this._abortUpdateCursor();
6979
+ }, 10);
6980
+ }
6981
+
6982
+ /** Fires custom events */
6983
+ _fireChangeEvents() {
6984
+ this._fireEvent('accept', this._inputEvent);
6985
+ if (this.masked.isComplete) this._fireEvent('complete', this._inputEvent);
6986
+ }
6987
+
6988
+ /** Aborts delayed cursor update */
6989
+ _abortUpdateCursor() {
6990
+ if (this._cursorChanging) {
6991
+ clearTimeout(this._cursorChanging);
6992
+ delete this._cursorChanging;
6993
+ }
6994
+ }
6995
+
6996
+ /** Aligns cursor to nearest available position */
6997
+ alignCursor() {
6998
+ this.cursorPos = this.masked.nearestInputPos(this.masked.nearestInputPos(this.cursorPos, DIRECTION.LEFT));
6999
+ }
7000
+
7001
+ /** Aligns cursor only if selection is empty */
7002
+ alignCursorFriendly() {
7003
+ if (this.selectionStart !== this.cursorPos) return; // skip if range is selected
7004
+ this.alignCursor();
7005
+ }
7006
+
7007
+ /** Adds listener on custom event */
7008
+ on(ev, handler) {
7009
+ if (!this._listeners[ev]) this._listeners[ev] = [];
7010
+ this._listeners[ev].push(handler);
7011
+ return this;
7012
+ }
7013
+
7014
+ /** Removes custom event listener */
7015
+ off(ev, handler) {
7016
+ if (!this._listeners[ev]) return this;
7017
+ if (!handler) {
7018
+ delete this._listeners[ev];
7019
+ return this;
7020
+ }
7021
+ const hIndex = this._listeners[ev].indexOf(handler);
7022
+ if (hIndex >= 0) this._listeners[ev].splice(hIndex, 1);
7023
+ return this;
7024
+ }
7025
+
7026
+ /** Handles view input event */
7027
+ _onInput(e) {
7028
+ this._inputEvent = e;
7029
+ this._abortUpdateCursor();
7030
+ const details = new ActionDetails({
7031
+ // new state
7032
+ value: this.el.value,
7033
+ cursorPos: this.cursorPos,
7034
+ // old state
7035
+ oldValue: this.displayValue,
7036
+ oldSelection: this._selection
7037
+ });
7038
+ const oldRawValue = this.masked.rawInputValue;
7039
+ const offset = this.masked.splice(details.startChangePos, details.removed.length, details.inserted, details.removeDirection, {
7040
+ input: true,
7041
+ raw: true
7042
+ }).offset;
7043
+
7044
+ // force align in remove direction only if no input chars were removed
7045
+ // otherwise we still need to align with NONE (to get out from fixed symbols for instance)
7046
+ const removeDirection = oldRawValue === this.masked.rawInputValue ? details.removeDirection : DIRECTION.NONE;
7047
+ let cursorPos = this.masked.nearestInputPos(details.startChangePos + offset, removeDirection);
7048
+ if (removeDirection !== DIRECTION.NONE) cursorPos = this.masked.nearestInputPos(cursorPos, DIRECTION.NONE);
7049
+ this.updateControl(cursorPos);
7050
+ delete this._inputEvent;
7051
+ }
7052
+
7053
+ /** Handles view change event and commits model value */
7054
+ _onChange() {
7055
+ if (this.displayValue !== this.el.value) this.updateValue();
7056
+ this.masked.doCommit();
7057
+ this.updateControl();
7058
+ this._saveSelection();
7059
+ }
7060
+
7061
+ /** Handles view drop event, prevents by default */
7062
+ _onDrop(ev) {
7063
+ ev.preventDefault();
7064
+ ev.stopPropagation();
7065
+ }
7066
+
7067
+ /** Restore last selection on focus */
7068
+ _onFocus(ev) {
7069
+ this.alignCursorFriendly();
7070
+ }
7071
+
7072
+ /** Restore last selection on focus */
7073
+ _onClick(ev) {
7074
+ this.alignCursorFriendly();
7075
+ }
7076
+ _onUndo() {
7077
+ this._applyHistoryState(this.history.undo());
7078
+ }
7079
+ _onRedo() {
7080
+ this._applyHistoryState(this.history.redo());
7081
+ }
7082
+ _applyHistoryState(state) {
7083
+ if (!state) return;
7084
+ this._historyChanging = true;
7085
+ this.unmaskedValue = state.unmaskedValue;
7086
+ this.el.select(state.selection.start, state.selection.end);
7087
+ this._saveSelection();
7088
+ this._historyChanging = false;
7089
+ }
7090
+
7091
+ /** Unbind view events and removes element reference */
7092
+ destroy() {
7093
+ this._unbindEvents();
7094
+ this._listeners.length = 0;
7095
+ delete this.el;
7096
+ }
7097
+ }
7098
+ IMask.InputMask = InputMask;
7099
+
7100
+ /** Provides details of changing model value */
7101
+ class ChangeDetails {
7102
+ /** Inserted symbols */
7103
+
7104
+ /** Additional offset if any changes occurred before tail */
7105
+
7106
+ /** Raw inserted is used by dynamic mask */
7107
+
7108
+ /** Can skip chars */
7109
+
7110
+ static normalize(prep) {
7111
+ return Array.isArray(prep) ? prep : [prep, new ChangeDetails()];
7112
+ }
7113
+ constructor(details) {
7114
+ Object.assign(this, {
7115
+ inserted: '',
7116
+ rawInserted: '',
7117
+ tailShift: 0,
7118
+ skip: false
7119
+ }, details);
7120
+ }
7121
+
7122
+ /** Aggregate changes */
7123
+ aggregate(details) {
7124
+ this.inserted += details.inserted;
7125
+ this.rawInserted += details.rawInserted;
7126
+ this.tailShift += details.tailShift;
7127
+ this.skip = this.skip || details.skip;
7128
+ return this;
7129
+ }
7130
+
7131
+ /** Total offset considering all changes */
7132
+ get offset() {
7133
+ return this.tailShift + this.inserted.length;
7134
+ }
7135
+ get consumed() {
7136
+ return Boolean(this.rawInserted) || this.skip;
7137
+ }
7138
+ equals(details) {
7139
+ return this.inserted === details.inserted && this.tailShift === details.tailShift && this.rawInserted === details.rawInserted && this.skip === details.skip;
7140
+ }
7141
+ }
7142
+ IMask.ChangeDetails = ChangeDetails;
7143
+
7144
+ /** Provides details of continuous extracted tail */
7145
+ class ContinuousTailDetails {
7146
+ /** Tail value as string */
7147
+
7148
+ /** Tail start position */
7149
+
7150
+ /** Start position */
7151
+
7152
+ constructor(value, from, stop) {
7153
+ if (value === void 0) {
7154
+ value = '';
7155
+ }
7156
+ if (from === void 0) {
7157
+ from = 0;
7158
+ }
7159
+ this.value = value;
7160
+ this.from = from;
7161
+ this.stop = stop;
7162
+ }
7163
+ toString() {
7164
+ return this.value;
7165
+ }
7166
+ extend(tail) {
7167
+ this.value += String(tail);
7168
+ }
7169
+ appendTo(masked) {
7170
+ return masked.append(this.toString(), {
7171
+ tail: true
7172
+ }).aggregate(masked._appendPlaceholder());
7173
+ }
7174
+ get state() {
7175
+ return {
7176
+ value: this.value,
7177
+ from: this.from,
7178
+ stop: this.stop
7179
+ };
7180
+ }
7181
+ set state(state) {
7182
+ Object.assign(this, state);
7183
+ }
7184
+ unshift(beforePos) {
7185
+ if (!this.value.length || beforePos != null && this.from >= beforePos) return '';
7186
+ const shiftChar = this.value[0];
7187
+ this.value = this.value.slice(1);
7188
+ return shiftChar;
7189
+ }
7190
+ shift() {
7191
+ if (!this.value.length) return '';
7192
+ const shiftChar = this.value[this.value.length - 1];
7193
+ this.value = this.value.slice(0, -1);
7194
+ return shiftChar;
7195
+ }
7196
+ }
7197
+
7198
+ /** Append flags */
7199
+
7200
+ /** Extract flags */
7201
+
7202
+ // see https://github.com/microsoft/TypeScript/issues/6223
7203
+
7204
+ /** Provides common masking stuff */
7205
+ class Masked {
7206
+ /** */
7207
+
7208
+ /** */
7209
+
7210
+ /** Transforms value before mask processing */
7211
+
7212
+ /** Transforms each char before mask processing */
7213
+
7214
+ /** Validates if value is acceptable */
7215
+
7216
+ /** Does additional processing at the end of editing */
7217
+
7218
+ /** Format typed value to string */
7219
+
7220
+ /** Parse string to get typed value */
7221
+
7222
+ /** Enable characters overwriting */
7223
+
7224
+ /** */
7225
+
7226
+ /** */
7227
+
7228
+ /** */
7229
+
7230
+ /** */
7231
+
7232
+ constructor(opts) {
7233
+ this._value = '';
7234
+ this._update({
7235
+ ...Masked.DEFAULTS,
7236
+ ...opts
7237
+ });
7238
+ this._initialized = true;
7239
+ }
7240
+
7241
+ /** Sets and applies new options */
7242
+ updateOptions(opts) {
7243
+ if (!this.optionsIsChanged(opts)) return;
7244
+ this.withValueRefresh(this._update.bind(this, opts));
7245
+ }
7246
+
7247
+ /** Sets new options */
7248
+ _update(opts) {
7249
+ Object.assign(this, opts);
7250
+ }
7251
+
7252
+ /** Mask state */
7253
+ get state() {
7254
+ return {
7255
+ _value: this.value,
7256
+ _rawInputValue: this.rawInputValue
7257
+ };
7258
+ }
7259
+ set state(state) {
7260
+ this._value = state._value;
7261
+ }
7262
+
7263
+ /** Resets value */
7264
+ reset() {
7265
+ this._value = '';
7266
+ }
7267
+ get value() {
7268
+ return this._value;
7269
+ }
7270
+ set value(value) {
7271
+ this.resolve(value, {
7272
+ input: true
7273
+ });
7274
+ }
7275
+
7276
+ /** Resolve new value */
7277
+ resolve(value, flags) {
7278
+ if (flags === void 0) {
7279
+ flags = {
7280
+ input: true
7281
+ };
7282
+ }
7283
+ this.reset();
7284
+ this.append(value, flags, '');
7285
+ this.doCommit();
7286
+ }
7287
+ get unmaskedValue() {
7288
+ return this.value;
7289
+ }
7290
+ set unmaskedValue(value) {
7291
+ this.resolve(value, {});
7292
+ }
7293
+ get typedValue() {
7294
+ return this.parse ? this.parse(this.value, this) : this.unmaskedValue;
7295
+ }
7296
+ set typedValue(value) {
7297
+ if (this.format) {
7298
+ this.value = this.format(value, this);
7299
+ } else {
7300
+ this.unmaskedValue = String(value);
7301
+ }
7302
+ }
7303
+
7304
+ /** Value that includes raw user input */
7305
+ get rawInputValue() {
7306
+ return this.extractInput(0, this.displayValue.length, {
7307
+ raw: true
7308
+ });
7309
+ }
7310
+ set rawInputValue(value) {
7311
+ this.resolve(value, {
7312
+ raw: true
7313
+ });
7314
+ }
7315
+ get displayValue() {
7316
+ return this.value;
7317
+ }
7318
+ get isComplete() {
7319
+ return true;
7320
+ }
7321
+ get isFilled() {
7322
+ return this.isComplete;
7323
+ }
7324
+
7325
+ /** Finds nearest input position in direction */
7326
+ nearestInputPos(cursorPos, direction) {
7327
+ return cursorPos;
7328
+ }
7329
+ totalInputPositions(fromPos, toPos) {
7330
+ if (fromPos === void 0) {
7331
+ fromPos = 0;
7332
+ }
7333
+ if (toPos === void 0) {
7334
+ toPos = this.displayValue.length;
7335
+ }
7336
+ return Math.min(this.displayValue.length, toPos - fromPos);
7337
+ }
7338
+
7339
+ /** Extracts value in range considering flags */
7340
+ extractInput(fromPos, toPos, flags) {
7341
+ if (fromPos === void 0) {
7342
+ fromPos = 0;
7343
+ }
7344
+ if (toPos === void 0) {
7345
+ toPos = this.displayValue.length;
7346
+ }
7347
+ return this.displayValue.slice(fromPos, toPos);
7348
+ }
7349
+
7350
+ /** Extracts tail in range */
7351
+ extractTail(fromPos, toPos) {
7352
+ if (fromPos === void 0) {
7353
+ fromPos = 0;
7354
+ }
7355
+ if (toPos === void 0) {
7356
+ toPos = this.displayValue.length;
7357
+ }
7358
+ return new ContinuousTailDetails(this.extractInput(fromPos, toPos), fromPos);
7359
+ }
7360
+
7361
+ /** Appends tail */
7362
+ appendTail(tail) {
7363
+ if (isString(tail)) tail = new ContinuousTailDetails(String(tail));
7364
+ return tail.appendTo(this);
7365
+ }
7366
+
7367
+ /** Appends char */
7368
+ _appendCharRaw(ch, flags) {
7369
+ if (!ch) return new ChangeDetails();
7370
+ this._value += ch;
7371
+ return new ChangeDetails({
7372
+ inserted: ch,
7373
+ rawInserted: ch
7374
+ });
7375
+ }
7376
+
7377
+ /** Appends char */
7378
+ _appendChar(ch, flags, checkTail) {
7379
+ if (flags === void 0) {
7380
+ flags = {};
7381
+ }
7382
+ const consistentState = this.state;
7383
+ let details;
7384
+ [ch, details] = this.doPrepareChar(ch, flags);
7385
+ if (ch) {
7386
+ details = details.aggregate(this._appendCharRaw(ch, flags));
7387
+
7388
+ // TODO handle `skip`?
7389
+
7390
+ // try `autofix` lookahead
7391
+ if (!details.rawInserted && this.autofix === 'pad') {
7392
+ const noFixState = this.state;
7393
+ this.state = consistentState;
7394
+ let fixDetails = this.pad(flags);
7395
+ const chDetails = this._appendCharRaw(ch, flags);
7396
+ fixDetails = fixDetails.aggregate(chDetails);
7397
+
7398
+ // if fix was applied or
7399
+ // if details are equal use skip restoring state optimization
7400
+ if (chDetails.rawInserted || fixDetails.equals(details)) {
7401
+ details = fixDetails;
7402
+ } else {
7403
+ this.state = noFixState;
7404
+ }
7405
+ }
7406
+ }
7407
+ if (details.inserted) {
7408
+ let consistentTail;
7409
+ let appended = this.doValidate(flags) !== false;
7410
+ if (appended && checkTail != null) {
7411
+ // validation ok, check tail
7412
+ const beforeTailState = this.state;
7413
+ if (this.overwrite === true) {
7414
+ consistentTail = checkTail.state;
7415
+ for (let i = 0; i < details.rawInserted.length; ++i) {
7416
+ checkTail.unshift(this.displayValue.length - details.tailShift);
7417
+ }
7418
+ }
7419
+ let tailDetails = this.appendTail(checkTail);
7420
+ appended = tailDetails.rawInserted.length === checkTail.toString().length;
7421
+
7422
+ // not ok, try shift
7423
+ if (!(appended && tailDetails.inserted) && this.overwrite === 'shift') {
7424
+ this.state = beforeTailState;
7425
+ consistentTail = checkTail.state;
7426
+ for (let i = 0; i < details.rawInserted.length; ++i) {
7427
+ checkTail.shift();
7428
+ }
7429
+ tailDetails = this.appendTail(checkTail);
7430
+ appended = tailDetails.rawInserted.length === checkTail.toString().length;
7431
+ }
7432
+
7433
+ // if ok, rollback state after tail
7434
+ if (appended && tailDetails.inserted) this.state = beforeTailState;
7435
+ }
7436
+
7437
+ // revert all if something went wrong
7438
+ if (!appended) {
7439
+ details = new ChangeDetails();
7440
+ this.state = consistentState;
7441
+ if (checkTail && consistentTail) checkTail.state = consistentTail;
7442
+ }
7443
+ }
7444
+ return details;
7445
+ }
7446
+
7447
+ /** Appends optional placeholder at the end */
7448
+ _appendPlaceholder() {
7449
+ return new ChangeDetails();
7450
+ }
7451
+
7452
+ /** Appends optional eager placeholder at the end */
7453
+ _appendEager() {
7454
+ return new ChangeDetails();
7455
+ }
7456
+
7457
+ /** Appends symbols considering flags */
7458
+ append(str, flags, tail) {
7459
+ if (!isString(str)) throw new Error('value should be string');
7460
+ const checkTail = isString(tail) ? new ContinuousTailDetails(String(tail)) : tail;
7461
+ if (flags != null && flags.tail) flags._beforeTailState = this.state;
7462
+ let details;
7463
+ [str, details] = this.doPrepare(str, flags);
7464
+ for (let ci = 0; ci < str.length; ++ci) {
7465
+ const d = this._appendChar(str[ci], flags, checkTail);
7466
+ if (!d.rawInserted && !this.doSkipInvalid(str[ci], flags, checkTail)) break;
7467
+ details.aggregate(d);
7468
+ }
7469
+ if ((this.eager === true || this.eager === 'append') && flags != null && flags.input && str) {
7470
+ details.aggregate(this._appendEager());
7471
+ }
7472
+
7473
+ // append tail but aggregate only tailShift
7474
+ if (checkTail != null) {
7475
+ details.tailShift += this.appendTail(checkTail).tailShift;
7476
+ // TODO it's a good idea to clear state after appending ends
7477
+ // but it causes bugs when one append calls another (when dynamic dispatch set rawInputValue)
7478
+ // this._resetBeforeTailState();
7479
+ }
7480
+ return details;
7481
+ }
7482
+ remove(fromPos, toPos) {
7483
+ if (fromPos === void 0) {
7484
+ fromPos = 0;
7485
+ }
7486
+ if (toPos === void 0) {
7487
+ toPos = this.displayValue.length;
7488
+ }
7489
+ this._value = this.displayValue.slice(0, fromPos) + this.displayValue.slice(toPos);
7490
+ return new ChangeDetails();
7491
+ }
7492
+
7493
+ /** Calls function and reapplies current value */
7494
+ withValueRefresh(fn) {
7495
+ if (this._refreshing || !this._initialized) return fn();
7496
+ this._refreshing = true;
7497
+ const rawInput = this.rawInputValue;
7498
+ const value = this.value;
7499
+ const ret = fn();
7500
+ this.rawInputValue = rawInput;
7501
+ // append lost trailing chars at the end
7502
+ if (this.value && this.value !== value && value.indexOf(this.value) === 0) {
7503
+ this.append(value.slice(this.displayValue.length), {}, '');
7504
+ this.doCommit();
7505
+ }
7506
+ delete this._refreshing;
7507
+ return ret;
7508
+ }
7509
+ runIsolated(fn) {
7510
+ if (this._isolated || !this._initialized) return fn(this);
7511
+ this._isolated = true;
7512
+ const state = this.state;
7513
+ const ret = fn(this);
7514
+ this.state = state;
7515
+ delete this._isolated;
7516
+ return ret;
7517
+ }
7518
+ doSkipInvalid(ch, flags, checkTail) {
7519
+ return Boolean(this.skipInvalid);
7520
+ }
7521
+
7522
+ /** Prepares string before mask processing */
7523
+ doPrepare(str, flags) {
7524
+ if (flags === void 0) {
7525
+ flags = {};
7526
+ }
7527
+ return ChangeDetails.normalize(this.prepare ? this.prepare(str, this, flags) : str);
7528
+ }
7529
+
7530
+ /** Prepares each char before mask processing */
7531
+ doPrepareChar(str, flags) {
7532
+ if (flags === void 0) {
7533
+ flags = {};
7534
+ }
7535
+ return ChangeDetails.normalize(this.prepareChar ? this.prepareChar(str, this, flags) : str);
7536
+ }
7537
+
7538
+ /** Validates if value is acceptable */
7539
+ doValidate(flags) {
7540
+ return (!this.validate || this.validate(this.value, this, flags)) && (!this.parent || this.parent.doValidate(flags));
7541
+ }
7542
+
7543
+ /** Does additional processing at the end of editing */
7544
+ doCommit() {
7545
+ if (this.commit) this.commit(this.value, this);
7546
+ }
7547
+ splice(start, deleteCount, inserted, removeDirection, flags) {
7548
+ if (inserted === void 0) {
7549
+ inserted = '';
7550
+ }
7551
+ if (removeDirection === void 0) {
7552
+ removeDirection = DIRECTION.NONE;
7553
+ }
7554
+ if (flags === void 0) {
7555
+ flags = {
7556
+ input: true
7557
+ };
7558
+ }
7559
+ const tailPos = start + deleteCount;
7560
+ const tail = this.extractTail(tailPos);
7561
+ const eagerRemove = this.eager === true || this.eager === 'remove';
7562
+ let oldRawValue;
7563
+ if (eagerRemove) {
7564
+ removeDirection = forceDirection(removeDirection);
7565
+ oldRawValue = this.extractInput(0, tailPos, {
7566
+ raw: true
7567
+ });
7568
+ }
7569
+ let startChangePos = start;
7570
+ const details = new ChangeDetails();
7571
+
7572
+ // if it is just deletion without insertion
7573
+ if (removeDirection !== DIRECTION.NONE) {
7574
+ startChangePos = this.nearestInputPos(start, deleteCount > 1 && start !== 0 && !eagerRemove ? DIRECTION.NONE : removeDirection);
7575
+
7576
+ // adjust tailShift if start was aligned
7577
+ details.tailShift = startChangePos - start;
7578
+ }
7579
+ details.aggregate(this.remove(startChangePos));
7580
+ if (eagerRemove && removeDirection !== DIRECTION.NONE && oldRawValue === this.rawInputValue) {
7581
+ if (removeDirection === DIRECTION.FORCE_LEFT) {
7582
+ let valLength;
7583
+ while (oldRawValue === this.rawInputValue && (valLength = this.displayValue.length)) {
7584
+ details.aggregate(new ChangeDetails({
7585
+ tailShift: -1
7586
+ })).aggregate(this.remove(valLength - 1));
7587
+ }
7588
+ } else if (removeDirection === DIRECTION.FORCE_RIGHT) {
7589
+ tail.unshift();
7590
+ }
7591
+ }
7592
+ return details.aggregate(this.append(inserted, flags, tail));
7593
+ }
7594
+ maskEquals(mask) {
7595
+ return this.mask === mask;
7596
+ }
7597
+ optionsIsChanged(opts) {
7598
+ return !objectIncludes(this, opts);
7599
+ }
7600
+ typedValueEquals(value) {
7601
+ const tval = this.typedValue;
7602
+ return value === tval || Masked.EMPTY_VALUES.includes(value) && Masked.EMPTY_VALUES.includes(tval) || (this.format ? this.format(value, this) === this.format(this.typedValue, this) : false);
7603
+ }
7604
+ pad(flags) {
7605
+ return new ChangeDetails();
7606
+ }
7607
+ }
7608
+ Masked.DEFAULTS = {
7609
+ skipInvalid: true
7610
+ };
7611
+ Masked.EMPTY_VALUES = [undefined, null, ''];
7612
+ IMask.Masked = Masked;
7613
+
7614
+ class ChunksTailDetails {
7615
+ /** */
7616
+
7617
+ constructor(chunks, from) {
7618
+ if (chunks === void 0) {
7619
+ chunks = [];
7620
+ }
7621
+ if (from === void 0) {
7622
+ from = 0;
7623
+ }
7624
+ this.chunks = chunks;
7625
+ this.from = from;
7626
+ }
7627
+ toString() {
7628
+ return this.chunks.map(String).join('');
7629
+ }
7630
+ extend(tailChunk) {
7631
+ if (!String(tailChunk)) return;
7632
+ tailChunk = isString(tailChunk) ? new ContinuousTailDetails(String(tailChunk)) : tailChunk;
7633
+ const lastChunk = this.chunks[this.chunks.length - 1];
7634
+ const extendLast = lastChunk && (
7635
+ // if stops are same or tail has no stop
7636
+ lastChunk.stop === tailChunk.stop || tailChunk.stop == null) &&
7637
+ // if tail chunk goes just after last chunk
7638
+ tailChunk.from === lastChunk.from + lastChunk.toString().length;
7639
+ if (tailChunk instanceof ContinuousTailDetails) {
7640
+ // check the ability to extend previous chunk
7641
+ if (extendLast) {
7642
+ // extend previous chunk
7643
+ lastChunk.extend(tailChunk.toString());
7644
+ } else {
7645
+ // append new chunk
7646
+ this.chunks.push(tailChunk);
7647
+ }
7648
+ } else if (tailChunk instanceof ChunksTailDetails) {
7649
+ if (tailChunk.stop == null) {
7650
+ // unwrap floating chunks to parent, keeping `from` pos
7651
+ let firstTailChunk;
7652
+ while (tailChunk.chunks.length && tailChunk.chunks[0].stop == null) {
7653
+ firstTailChunk = tailChunk.chunks.shift(); // not possible to be `undefined` because length was checked above
7654
+ firstTailChunk.from += tailChunk.from;
7655
+ this.extend(firstTailChunk);
7656
+ }
7657
+ }
7658
+
7659
+ // if tail chunk still has value
7660
+ if (tailChunk.toString()) {
7661
+ // if chunks contains stops, then popup stop to container
7662
+ tailChunk.stop = tailChunk.blockIndex;
7663
+ this.chunks.push(tailChunk);
7664
+ }
7665
+ }
7666
+ }
7667
+ appendTo(masked) {
7668
+ if (!(masked instanceof IMask.MaskedPattern)) {
7669
+ const tail = new ContinuousTailDetails(this.toString());
7670
+ return tail.appendTo(masked);
7671
+ }
7672
+ const details = new ChangeDetails();
7673
+ for (let ci = 0; ci < this.chunks.length; ++ci) {
7674
+ const chunk = this.chunks[ci];
7675
+ const lastBlockIter = masked._mapPosToBlock(masked.displayValue.length);
7676
+ const stop = chunk.stop;
7677
+ let chunkBlock;
7678
+ if (stop != null && (
7679
+ // if block not found or stop is behind lastBlock
7680
+ !lastBlockIter || lastBlockIter.index <= stop)) {
7681
+ if (chunk instanceof ChunksTailDetails ||
7682
+ // for continuous block also check if stop is exist
7683
+ masked._stops.indexOf(stop) >= 0) {
7684
+ details.aggregate(masked._appendPlaceholder(stop));
7685
+ }
7686
+ chunkBlock = chunk instanceof ChunksTailDetails && masked._blocks[stop];
7687
+ }
7688
+ if (chunkBlock) {
7689
+ const tailDetails = chunkBlock.appendTail(chunk);
7690
+ details.aggregate(tailDetails);
7691
+
7692
+ // get not inserted chars
7693
+ const remainChars = chunk.toString().slice(tailDetails.rawInserted.length);
7694
+ if (remainChars) details.aggregate(masked.append(remainChars, {
7695
+ tail: true
7696
+ }));
7697
+ } else {
7698
+ details.aggregate(masked.append(chunk.toString(), {
7699
+ tail: true
7700
+ }));
7701
+ }
7702
+ }
7703
+ return details;
7704
+ }
7705
+ get state() {
7706
+ return {
7707
+ chunks: this.chunks.map(c => c.state),
7708
+ from: this.from,
7709
+ stop: this.stop,
7710
+ blockIndex: this.blockIndex
7711
+ };
7712
+ }
7713
+ set state(state) {
7714
+ const {
7715
+ chunks,
7716
+ ...props
7717
+ } = state;
7718
+ Object.assign(this, props);
7719
+ this.chunks = chunks.map(cstate => {
7720
+ const chunk = "chunks" in cstate ? new ChunksTailDetails() : new ContinuousTailDetails();
7721
+ chunk.state = cstate;
7722
+ return chunk;
7723
+ });
7724
+ }
7725
+ unshift(beforePos) {
7726
+ if (!this.chunks.length || beforePos != null && this.from >= beforePos) return '';
7727
+ const chunkShiftPos = beforePos != null ? beforePos - this.from : beforePos;
7728
+ let ci = 0;
7729
+ while (ci < this.chunks.length) {
7730
+ const chunk = this.chunks[ci];
7731
+ const shiftChar = chunk.unshift(chunkShiftPos);
7732
+ if (chunk.toString()) {
7733
+ // chunk still contains value
7734
+ // but not shifted - means no more available chars to shift
7735
+ if (!shiftChar) break;
7736
+ ++ci;
7737
+ } else {
7738
+ // clean if chunk has no value
7739
+ this.chunks.splice(ci, 1);
7740
+ }
7741
+ if (shiftChar) return shiftChar;
7742
+ }
7743
+ return '';
7744
+ }
7745
+ shift() {
7746
+ if (!this.chunks.length) return '';
7747
+ let ci = this.chunks.length - 1;
7748
+ while (0 <= ci) {
7749
+ const chunk = this.chunks[ci];
7750
+ const shiftChar = chunk.shift();
7751
+ if (chunk.toString()) {
7752
+ // chunk still contains value
7753
+ // but not shifted - means no more available chars to shift
7754
+ if (!shiftChar) break;
7755
+ --ci;
7756
+ } else {
7757
+ // clean if chunk has no value
7758
+ this.chunks.splice(ci, 1);
7759
+ }
7760
+ if (shiftChar) return shiftChar;
7761
+ }
7762
+ return '';
7763
+ }
7764
+ }
7765
+
7766
+ class PatternCursor {
7767
+ constructor(masked, pos) {
7768
+ this.masked = masked;
7769
+ this._log = [];
7770
+ const {
7771
+ offset,
7772
+ index
7773
+ } = masked._mapPosToBlock(pos) || (pos < 0 ?
7774
+ // first
7775
+ {
7776
+ index: 0,
7777
+ offset: 0
7778
+ } :
7779
+ // last
7780
+ {
7781
+ index: this.masked._blocks.length,
7782
+ offset: 0
7783
+ });
7784
+ this.offset = offset;
7785
+ this.index = index;
7786
+ this.ok = false;
7787
+ }
7788
+ get block() {
7789
+ return this.masked._blocks[this.index];
7790
+ }
7791
+ get pos() {
7792
+ return this.masked._blockStartPos(this.index) + this.offset;
7793
+ }
7794
+ get state() {
7795
+ return {
7796
+ index: this.index,
7797
+ offset: this.offset,
7798
+ ok: this.ok
7799
+ };
7800
+ }
7801
+ set state(s) {
7802
+ Object.assign(this, s);
7803
+ }
7804
+ pushState() {
7805
+ this._log.push(this.state);
7806
+ }
7807
+ popState() {
7808
+ const s = this._log.pop();
7809
+ if (s) this.state = s;
7810
+ return s;
7811
+ }
7812
+ bindBlock() {
7813
+ if (this.block) return;
7814
+ if (this.index < 0) {
7815
+ this.index = 0;
7816
+ this.offset = 0;
7817
+ }
7818
+ if (this.index >= this.masked._blocks.length) {
7819
+ this.index = this.masked._blocks.length - 1;
7820
+ this.offset = this.block.displayValue.length; // TODO this is stupid type error, `block` depends on index that was changed above
7821
+ }
7822
+ }
7823
+ _pushLeft(fn) {
7824
+ this.pushState();
7825
+ for (this.bindBlock(); 0 <= this.index; --this.index, this.offset = ((_this$block = this.block) == null ? void 0 : _this$block.displayValue.length) || 0) {
7826
+ var _this$block;
7827
+ if (fn()) return this.ok = true;
7828
+ }
7829
+ return this.ok = false;
7830
+ }
7831
+ _pushRight(fn) {
7832
+ this.pushState();
7833
+ for (this.bindBlock(); this.index < this.masked._blocks.length; ++this.index, this.offset = 0) {
7834
+ if (fn()) return this.ok = true;
7835
+ }
7836
+ return this.ok = false;
7837
+ }
7838
+ pushLeftBeforeFilled() {
7839
+ return this._pushLeft(() => {
7840
+ if (this.block.isFixed || !this.block.value) return;
7841
+ this.offset = this.block.nearestInputPos(this.offset, DIRECTION.FORCE_LEFT);
7842
+ if (this.offset !== 0) return true;
7843
+ });
7844
+ }
7845
+ pushLeftBeforeInput() {
7846
+ // cases:
7847
+ // filled input: 00|
7848
+ // optional empty input: 00[]|
7849
+ // nested block: XX<[]>|
7850
+ return this._pushLeft(() => {
7851
+ if (this.block.isFixed) return;
7852
+ this.offset = this.block.nearestInputPos(this.offset, DIRECTION.LEFT);
7853
+ return true;
7854
+ });
7855
+ }
7856
+ pushLeftBeforeRequired() {
7857
+ return this._pushLeft(() => {
7858
+ if (this.block.isFixed || this.block.isOptional && !this.block.value) return;
7859
+ this.offset = this.block.nearestInputPos(this.offset, DIRECTION.LEFT);
7860
+ return true;
7861
+ });
7862
+ }
7863
+ pushRightBeforeFilled() {
7864
+ return this._pushRight(() => {
7865
+ if (this.block.isFixed || !this.block.value) return;
7866
+ this.offset = this.block.nearestInputPos(this.offset, DIRECTION.FORCE_RIGHT);
7867
+ if (this.offset !== this.block.value.length) return true;
7868
+ });
7869
+ }
7870
+ pushRightBeforeInput() {
7871
+ return this._pushRight(() => {
7872
+ if (this.block.isFixed) return;
7873
+
7874
+ // const o = this.offset;
7875
+ this.offset = this.block.nearestInputPos(this.offset, DIRECTION.NONE);
7876
+ // HACK cases like (STILL DOES NOT WORK FOR NESTED)
7877
+ // aa|X
7878
+ // aa<X|[]>X_ - this will not work
7879
+ // if (o && o === this.offset && this.block instanceof PatternInputDefinition) continue;
7880
+ return true;
7881
+ });
7882
+ }
7883
+ pushRightBeforeRequired() {
7884
+ return this._pushRight(() => {
7885
+ if (this.block.isFixed || this.block.isOptional && !this.block.value) return;
7886
+
7887
+ // TODO check |[*]XX_
7888
+ this.offset = this.block.nearestInputPos(this.offset, DIRECTION.NONE);
7889
+ return true;
7890
+ });
7891
+ }
7892
+ }
7893
+
7894
+ class PatternFixedDefinition {
7895
+ /** */
7896
+
7897
+ /** */
7898
+
7899
+ /** */
7900
+
7901
+ /** */
7902
+
7903
+ /** */
7904
+
7905
+ /** */
7906
+
7907
+ constructor(opts) {
7908
+ Object.assign(this, opts);
7909
+ this._value = '';
7910
+ this.isFixed = true;
7911
+ }
7912
+ get value() {
7913
+ return this._value;
7914
+ }
7915
+ get unmaskedValue() {
7916
+ return this.isUnmasking ? this.value : '';
7917
+ }
7918
+ get rawInputValue() {
7919
+ return this._isRawInput ? this.value : '';
7920
+ }
7921
+ get displayValue() {
7922
+ return this.value;
7923
+ }
7924
+ reset() {
7925
+ this._isRawInput = false;
7926
+ this._value = '';
7927
+ }
7928
+ remove(fromPos, toPos) {
7929
+ if (fromPos === void 0) {
7930
+ fromPos = 0;
7931
+ }
7932
+ if (toPos === void 0) {
7933
+ toPos = this._value.length;
7934
+ }
7935
+ this._value = this._value.slice(0, fromPos) + this._value.slice(toPos);
7936
+ if (!this._value) this._isRawInput = false;
7937
+ return new ChangeDetails();
7938
+ }
7939
+ nearestInputPos(cursorPos, direction) {
7940
+ if (direction === void 0) {
7941
+ direction = DIRECTION.NONE;
7942
+ }
7943
+ const minPos = 0;
7944
+ const maxPos = this._value.length;
7945
+ switch (direction) {
7946
+ case DIRECTION.LEFT:
7947
+ case DIRECTION.FORCE_LEFT:
7948
+ return minPos;
7949
+ case DIRECTION.NONE:
7950
+ case DIRECTION.RIGHT:
7951
+ case DIRECTION.FORCE_RIGHT:
7952
+ default:
7953
+ return maxPos;
7954
+ }
7955
+ }
7956
+ totalInputPositions(fromPos, toPos) {
7957
+ if (fromPos === void 0) {
7958
+ fromPos = 0;
7959
+ }
7960
+ if (toPos === void 0) {
7961
+ toPos = this._value.length;
7962
+ }
7963
+ return this._isRawInput ? toPos - fromPos : 0;
7964
+ }
7965
+ extractInput(fromPos, toPos, flags) {
7966
+ if (fromPos === void 0) {
7967
+ fromPos = 0;
7968
+ }
7969
+ if (toPos === void 0) {
7970
+ toPos = this._value.length;
7971
+ }
7972
+ if (flags === void 0) {
7973
+ flags = {};
7974
+ }
7975
+ return flags.raw && this._isRawInput && this._value.slice(fromPos, toPos) || '';
7976
+ }
7977
+ get isComplete() {
7978
+ return true;
7979
+ }
7980
+ get isFilled() {
7981
+ return Boolean(this._value);
7982
+ }
7983
+ _appendChar(ch, flags) {
7984
+ if (flags === void 0) {
7985
+ flags = {};
7986
+ }
7987
+ if (this.isFilled) return new ChangeDetails();
7988
+ const appendEager = this.eager === true || this.eager === 'append';
7989
+ const appended = this.char === ch;
7990
+ const isResolved = appended && (this.isUnmasking || flags.input || flags.raw) && (!flags.raw || !appendEager) && !flags.tail;
7991
+ const details = new ChangeDetails({
7992
+ inserted: this.char,
7993
+ rawInserted: isResolved ? this.char : ''
7994
+ });
7995
+ this._value = this.char;
7996
+ this._isRawInput = isResolved && (flags.raw || flags.input);
7997
+ return details;
7998
+ }
7999
+ _appendEager() {
8000
+ return this._appendChar(this.char, {
8001
+ tail: true
8002
+ });
8003
+ }
8004
+ _appendPlaceholder() {
8005
+ const details = new ChangeDetails();
8006
+ if (this.isFilled) return details;
8007
+ this._value = details.inserted = this.char;
8008
+ return details;
8009
+ }
8010
+ extractTail() {
8011
+ return new ContinuousTailDetails('');
8012
+ }
8013
+ appendTail(tail) {
8014
+ if (isString(tail)) tail = new ContinuousTailDetails(String(tail));
8015
+ return tail.appendTo(this);
8016
+ }
8017
+ append(str, flags, tail) {
8018
+ const details = this._appendChar(str[0], flags);
8019
+ if (tail != null) {
8020
+ details.tailShift += this.appendTail(tail).tailShift;
8021
+ }
8022
+ return details;
8023
+ }
8024
+ doCommit() {}
8025
+ get state() {
8026
+ return {
8027
+ _value: this._value,
8028
+ _rawInputValue: this.rawInputValue
8029
+ };
8030
+ }
8031
+ set state(state) {
8032
+ this._value = state._value;
8033
+ this._isRawInput = Boolean(state._rawInputValue);
8034
+ }
8035
+ pad(flags) {
8036
+ return this._appendPlaceholder();
8037
+ }
8038
+ }
8039
+
8040
+ class PatternInputDefinition {
8041
+ /** */
8042
+
8043
+ /** */
8044
+
8045
+ /** */
8046
+
8047
+ /** */
8048
+
8049
+ /** */
8050
+
8051
+ /** */
8052
+
8053
+ /** */
8054
+
8055
+ /** */
8056
+
8057
+ constructor(opts) {
8058
+ const {
8059
+ parent,
8060
+ isOptional,
8061
+ placeholderChar,
8062
+ displayChar,
8063
+ lazy,
8064
+ eager,
8065
+ ...maskOpts
8066
+ } = opts;
8067
+ this.masked = createMask(maskOpts);
8068
+ Object.assign(this, {
8069
+ parent,
8070
+ isOptional,
8071
+ placeholderChar,
8072
+ displayChar,
8073
+ lazy,
8074
+ eager
8075
+ });
8076
+ }
8077
+ reset() {
8078
+ this.isFilled = false;
8079
+ this.masked.reset();
8080
+ }
8081
+ remove(fromPos, toPos) {
8082
+ if (fromPos === void 0) {
8083
+ fromPos = 0;
8084
+ }
8085
+ if (toPos === void 0) {
8086
+ toPos = this.value.length;
8087
+ }
8088
+ if (fromPos === 0 && toPos >= 1) {
8089
+ this.isFilled = false;
8090
+ return this.masked.remove(fromPos, toPos);
8091
+ }
8092
+ return new ChangeDetails();
8093
+ }
8094
+ get value() {
8095
+ return this.masked.value || (this.isFilled && !this.isOptional ? this.placeholderChar : '');
8096
+ }
8097
+ get unmaskedValue() {
8098
+ return this.masked.unmaskedValue;
8099
+ }
8100
+ get rawInputValue() {
8101
+ return this.masked.rawInputValue;
8102
+ }
8103
+ get displayValue() {
8104
+ return this.masked.value && this.displayChar || this.value;
8105
+ }
8106
+ get isComplete() {
8107
+ return Boolean(this.masked.value) || this.isOptional;
8108
+ }
8109
+ _appendChar(ch, flags) {
8110
+ if (flags === void 0) {
8111
+ flags = {};
8112
+ }
8113
+ if (this.isFilled) return new ChangeDetails();
8114
+ const state = this.masked.state;
8115
+ // simulate input
8116
+ let details = this.masked._appendChar(ch, this.currentMaskFlags(flags));
8117
+ if (details.inserted && this.doValidate(flags) === false) {
8118
+ details = new ChangeDetails();
8119
+ this.masked.state = state;
8120
+ }
8121
+ if (!details.inserted && !this.isOptional && !this.lazy && !flags.input) {
8122
+ details.inserted = this.placeholderChar;
8123
+ }
8124
+ details.skip = !details.inserted && !this.isOptional;
8125
+ this.isFilled = Boolean(details.inserted);
8126
+ return details;
8127
+ }
8128
+ append(str, flags, tail) {
8129
+ // TODO probably should be done via _appendChar
8130
+ return this.masked.append(str, this.currentMaskFlags(flags), tail);
8131
+ }
8132
+ _appendPlaceholder() {
8133
+ if (this.isFilled || this.isOptional) return new ChangeDetails();
8134
+ this.isFilled = true;
8135
+ return new ChangeDetails({
8136
+ inserted: this.placeholderChar
8137
+ });
8138
+ }
8139
+ _appendEager() {
8140
+ return new ChangeDetails();
8141
+ }
8142
+ extractTail(fromPos, toPos) {
8143
+ return this.masked.extractTail(fromPos, toPos);
8144
+ }
8145
+ appendTail(tail) {
8146
+ return this.masked.appendTail(tail);
8147
+ }
8148
+ extractInput(fromPos, toPos, flags) {
8149
+ if (fromPos === void 0) {
8150
+ fromPos = 0;
8151
+ }
8152
+ if (toPos === void 0) {
8153
+ toPos = this.value.length;
8154
+ }
8155
+ return this.masked.extractInput(fromPos, toPos, flags);
8156
+ }
8157
+ nearestInputPos(cursorPos, direction) {
8158
+ if (direction === void 0) {
8159
+ direction = DIRECTION.NONE;
8160
+ }
8161
+ const minPos = 0;
8162
+ const maxPos = this.value.length;
8163
+ const boundPos = Math.min(Math.max(cursorPos, minPos), maxPos);
8164
+ switch (direction) {
8165
+ case DIRECTION.LEFT:
8166
+ case DIRECTION.FORCE_LEFT:
8167
+ return this.isComplete ? boundPos : minPos;
8168
+ case DIRECTION.RIGHT:
8169
+ case DIRECTION.FORCE_RIGHT:
8170
+ return this.isComplete ? boundPos : maxPos;
8171
+ case DIRECTION.NONE:
8172
+ default:
8173
+ return boundPos;
8174
+ }
8175
+ }
8176
+ totalInputPositions(fromPos, toPos) {
8177
+ if (fromPos === void 0) {
8178
+ fromPos = 0;
8179
+ }
8180
+ if (toPos === void 0) {
8181
+ toPos = this.value.length;
8182
+ }
8183
+ return this.value.slice(fromPos, toPos).length;
8184
+ }
8185
+ doValidate(flags) {
8186
+ return this.masked.doValidate(this.currentMaskFlags(flags)) && (!this.parent || this.parent.doValidate(this.currentMaskFlags(flags)));
8187
+ }
8188
+ doCommit() {
8189
+ this.masked.doCommit();
8190
+ }
8191
+ get state() {
8192
+ return {
8193
+ _value: this.value,
8194
+ _rawInputValue: this.rawInputValue,
8195
+ masked: this.masked.state,
8196
+ isFilled: this.isFilled
8197
+ };
8198
+ }
8199
+ set state(state) {
8200
+ this.masked.state = state.masked;
8201
+ this.isFilled = state.isFilled;
8202
+ }
8203
+ currentMaskFlags(flags) {
8204
+ var _flags$_beforeTailSta;
8205
+ return {
8206
+ ...flags,
8207
+ _beforeTailState: (flags == null || (_flags$_beforeTailSta = flags._beforeTailState) == null ? void 0 : _flags$_beforeTailSta.masked) || (flags == null ? void 0 : flags._beforeTailState)
8208
+ };
8209
+ }
8210
+ pad(flags) {
8211
+ return new ChangeDetails();
8212
+ }
8213
+ }
8214
+ PatternInputDefinition.DEFAULT_DEFINITIONS = {
8215
+ '0': /\d/,
8216
+ 'a': /[\u0041-\u005A\u0061-\u007A\u00AA\u00B5\u00BA\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02C1\u02C6-\u02D1\u02E0-\u02E4\u02EC\u02EE\u0370-\u0374\u0376\u0377\u037A-\u037D\u0386\u0388-\u038A\u038C\u038E-\u03A1\u03A3-\u03F5\u03F7-\u0481\u048A-\u0527\u0531-\u0556\u0559\u0561-\u0587\u05D0-\u05EA\u05F0-\u05F2\u0620-\u064A\u066E\u066F\u0671-\u06D3\u06D5\u06E5\u06E6\u06EE\u06EF\u06FA-\u06FC\u06FF\u0710\u0712-\u072F\u074D-\u07A5\u07B1\u07CA-\u07EA\u07F4\u07F5\u07FA\u0800-\u0815\u081A\u0824\u0828\u0840-\u0858\u08A0\u08A2-\u08AC\u0904-\u0939\u093D\u0950\u0958-\u0961\u0971-\u0977\u0979-\u097F\u0985-\u098C\u098F\u0990\u0993-\u09A8\u09AA-\u09B0\u09B2\u09B6-\u09B9\u09BD\u09CE\u09DC\u09DD\u09DF-\u09E1\u09F0\u09F1\u0A05-\u0A0A\u0A0F\u0A10\u0A13-\u0A28\u0A2A-\u0A30\u0A32\u0A33\u0A35\u0A36\u0A38\u0A39\u0A59-\u0A5C\u0A5E\u0A72-\u0A74\u0A85-\u0A8D\u0A8F-\u0A91\u0A93-\u0AA8\u0AAA-\u0AB0\u0AB2\u0AB3\u0AB5-\u0AB9\u0ABD\u0AD0\u0AE0\u0AE1\u0B05-\u0B0C\u0B0F\u0B10\u0B13-\u0B28\u0B2A-\u0B30\u0B32\u0B33\u0B35-\u0B39\u0B3D\u0B5C\u0B5D\u0B5F-\u0B61\u0B71\u0B83\u0B85-\u0B8A\u0B8E-\u0B90\u0B92-\u0B95\u0B99\u0B9A\u0B9C\u0B9E\u0B9F\u0BA3\u0BA4\u0BA8-\u0BAA\u0BAE-\u0BB9\u0BD0\u0C05-\u0C0C\u0C0E-\u0C10\u0C12-\u0C28\u0C2A-\u0C33\u0C35-\u0C39\u0C3D\u0C58\u0C59\u0C60\u0C61\u0C85-\u0C8C\u0C8E-\u0C90\u0C92-\u0CA8\u0CAA-\u0CB3\u0CB5-\u0CB9\u0CBD\u0CDE\u0CE0\u0CE1\u0CF1\u0CF2\u0D05-\u0D0C\u0D0E-\u0D10\u0D12-\u0D3A\u0D3D\u0D4E\u0D60\u0D61\u0D7A-\u0D7F\u0D85-\u0D96\u0D9A-\u0DB1\u0DB3-\u0DBB\u0DBD\u0DC0-\u0DC6\u0E01-\u0E30\u0E32\u0E33\u0E40-\u0E46\u0E81\u0E82\u0E84\u0E87\u0E88\u0E8A\u0E8D\u0E94-\u0E97\u0E99-\u0E9F\u0EA1-\u0EA3\u0EA5\u0EA7\u0EAA\u0EAB\u0EAD-\u0EB0\u0EB2\u0EB3\u0EBD\u0EC0-\u0EC4\u0EC6\u0EDC-\u0EDF\u0F00\u0F40-\u0F47\u0F49-\u0F6C\u0F88-\u0F8C\u1000-\u102A\u103F\u1050-\u1055\u105A-\u105D\u1061\u1065\u1066\u106E-\u1070\u1075-\u1081\u108E\u10A0-\u10C5\u10C7\u10CD\u10D0-\u10FA\u10FC-\u1248\u124A-\u124D\u1250-\u1256\u1258\u125A-\u125D\u1260-\u1288\u128A-\u128D\u1290-\u12B0\u12B2-\u12B5\u12B8-\u12BE\u12C0\u12C2-\u12C5\u12C8-\u12D6\u12D8-\u1310\u1312-\u1315\u1318-\u135A\u1380-\u138F\u13A0-\u13F4\u1401-\u166C\u166F-\u167F\u1681-\u169A\u16A0-\u16EA\u1700-\u170C\u170E-\u1711\u1720-\u1731\u1740-\u1751\u1760-\u176C\u176E-\u1770\u1780-\u17B3\u17D7\u17DC\u1820-\u1877\u1880-\u18A8\u18AA\u18B0-\u18F5\u1900-\u191C\u1950-\u196D\u1970-\u1974\u1980-\u19AB\u19C1-\u19C7\u1A00-\u1A16\u1A20-\u1A54\u1AA7\u1B05-\u1B33\u1B45-\u1B4B\u1B83-\u1BA0\u1BAE\u1BAF\u1BBA-\u1BE5\u1C00-\u1C23\u1C4D-\u1C4F\u1C5A-\u1C7D\u1CE9-\u1CEC\u1CEE-\u1CF1\u1CF5\u1CF6\u1D00-\u1DBF\u1E00-\u1F15\u1F18-\u1F1D\u1F20-\u1F45\u1F48-\u1F4D\u1F50-\u1F57\u1F59\u1F5B\u1F5D\u1F5F-\u1F7D\u1F80-\u1FB4\u1FB6-\u1FBC\u1FBE\u1FC2-\u1FC4\u1FC6-\u1FCC\u1FD0-\u1FD3\u1FD6-\u1FDB\u1FE0-\u1FEC\u1FF2-\u1FF4\u1FF6-\u1FFC\u2071\u207F\u2090-\u209C\u2102\u2107\u210A-\u2113\u2115\u2119-\u211D\u2124\u2126\u2128\u212A-\u212D\u212F-\u2139\u213C-\u213F\u2145-\u2149\u214E\u2183\u2184\u2C00-\u2C2E\u2C30-\u2C5E\u2C60-\u2CE4\u2CEB-\u2CEE\u2CF2\u2CF3\u2D00-\u2D25\u2D27\u2D2D\u2D30-\u2D67\u2D6F\u2D80-\u2D96\u2DA0-\u2DA6\u2DA8-\u2DAE\u2DB0-\u2DB6\u2DB8-\u2DBE\u2DC0-\u2DC6\u2DC8-\u2DCE\u2DD0-\u2DD6\u2DD8-\u2DDE\u2E2F\u3005\u3006\u3031-\u3035\u303B\u303C\u3041-\u3096\u309D-\u309F\u30A1-\u30FA\u30FC-\u30FF\u3105-\u312D\u3131-\u318E\u31A0-\u31BA\u31F0-\u31FF\u3400-\u4DB5\u4E00-\u9FCC\uA000-\uA48C\uA4D0-\uA4FD\uA500-\uA60C\uA610-\uA61F\uA62A\uA62B\uA640-\uA66E\uA67F-\uA697\uA6A0-\uA6E5\uA717-\uA71F\uA722-\uA788\uA78B-\uA78E\uA790-\uA793\uA7A0-\uA7AA\uA7F8-\uA801\uA803-\uA805\uA807-\uA80A\uA80C-\uA822\uA840-\uA873\uA882-\uA8B3\uA8F2-\uA8F7\uA8FB\uA90A-\uA925\uA930-\uA946\uA960-\uA97C\uA984-\uA9B2\uA9CF\uAA00-\uAA28\uAA40-\uAA42\uAA44-\uAA4B\uAA60-\uAA76\uAA7A\uAA80-\uAAAF\uAAB1\uAAB5\uAAB6\uAAB9-\uAABD\uAAC0\uAAC2\uAADB-\uAADD\uAAE0-\uAAEA\uAAF2-\uAAF4\uAB01-\uAB06\uAB09-\uAB0E\uAB11-\uAB16\uAB20-\uAB26\uAB28-\uAB2E\uABC0-\uABE2\uAC00-\uD7A3\uD7B0-\uD7C6\uD7CB-\uD7FB\uF900-\uFA6D\uFA70-\uFAD9\uFB00-\uFB06\uFB13-\uFB17\uFB1D\uFB1F-\uFB28\uFB2A-\uFB36\uFB38-\uFB3C\uFB3E\uFB40\uFB41\uFB43\uFB44\uFB46-\uFBB1\uFBD3-\uFD3D\uFD50-\uFD8F\uFD92-\uFDC7\uFDF0-\uFDFB\uFE70-\uFE74\uFE76-\uFEFC\uFF21-\uFF3A\uFF41-\uFF5A\uFF66-\uFFBE\uFFC2-\uFFC7\uFFCA-\uFFCF\uFFD2-\uFFD7\uFFDA-\uFFDC]/,
8217
+ // http://stackoverflow.com/a/22075070
8218
+ '*': /./
8219
+ };
8220
+
8221
+ /** Masking by RegExp */
8222
+ class MaskedRegExp extends Masked {
8223
+ /** */
8224
+
8225
+ /** Enable characters overwriting */
8226
+
8227
+ /** */
8228
+
8229
+ /** */
8230
+
8231
+ /** */
8232
+
8233
+ updateOptions(opts) {
8234
+ super.updateOptions(opts);
8235
+ }
8236
+ _update(opts) {
8237
+ const mask = opts.mask;
8238
+ if (mask) opts.validate = value => value.search(mask) >= 0;
8239
+ super._update(opts);
8240
+ }
8241
+ }
8242
+ IMask.MaskedRegExp = MaskedRegExp;
8243
+
8244
+ /** Pattern mask */
8245
+ class MaskedPattern extends Masked {
8246
+ /** */
8247
+
8248
+ /** */
8249
+
8250
+ /** Single char for empty input */
8251
+
8252
+ /** Single char for filled input */
8253
+
8254
+ /** Show placeholder only when needed */
8255
+
8256
+ /** Enable characters overwriting */
8257
+
8258
+ /** */
8259
+
8260
+ /** */
8261
+
8262
+ /** */
8263
+
8264
+ constructor(opts) {
8265
+ super({
8266
+ ...MaskedPattern.DEFAULTS,
8267
+ ...opts,
8268
+ definitions: Object.assign({}, PatternInputDefinition.DEFAULT_DEFINITIONS, opts == null ? void 0 : opts.definitions)
8269
+ });
8270
+ }
8271
+ updateOptions(opts) {
8272
+ super.updateOptions(opts);
8273
+ }
8274
+ _update(opts) {
8275
+ opts.definitions = Object.assign({}, this.definitions, opts.definitions);
8276
+ super._update(opts);
8277
+ this._rebuildMask();
8278
+ }
8279
+ _rebuildMask() {
8280
+ const defs = this.definitions;
8281
+ this._blocks = [];
8282
+ this.exposeBlock = undefined;
8283
+ this._stops = [];
8284
+ this._maskedBlocks = {};
8285
+ const pattern = this.mask;
8286
+ if (!pattern || !defs) return;
8287
+ let unmaskingBlock = false;
8288
+ let optionalBlock = false;
8289
+ for (let i = 0; i < pattern.length; ++i) {
8290
+ if (this.blocks) {
8291
+ const p = pattern.slice(i);
8292
+ const bNames = Object.keys(this.blocks).filter(bName => p.indexOf(bName) === 0);
8293
+ // order by key length
8294
+ bNames.sort((a, b) => b.length - a.length);
8295
+ // use block name with max length
8296
+ const bName = bNames[0];
8297
+ if (bName) {
8298
+ const {
8299
+ expose,
8300
+ repeat,
8301
+ ...bOpts
8302
+ } = normalizeOpts(this.blocks[bName]); // TODO type Opts<Arg & Extra>
8303
+ const blockOpts = {
8304
+ lazy: this.lazy,
8305
+ eager: this.eager,
8306
+ placeholderChar: this.placeholderChar,
8307
+ displayChar: this.displayChar,
8308
+ overwrite: this.overwrite,
8309
+ autofix: this.autofix,
8310
+ ...bOpts,
8311
+ repeat,
8312
+ parent: this
8313
+ };
8314
+ const maskedBlock = repeat != null ? new IMask.RepeatBlock(blockOpts /* TODO */) : createMask(blockOpts);
8315
+ if (maskedBlock) {
8316
+ this._blocks.push(maskedBlock);
8317
+ if (expose) this.exposeBlock = maskedBlock;
8318
+
8319
+ // store block index
8320
+ if (!this._maskedBlocks[bName]) this._maskedBlocks[bName] = [];
8321
+ this._maskedBlocks[bName].push(this._blocks.length - 1);
8322
+ }
8323
+ i += bName.length - 1;
8324
+ continue;
8325
+ }
8326
+ }
8327
+ let char = pattern[i];
8328
+ let isInput = (char in defs);
8329
+ if (char === MaskedPattern.STOP_CHAR) {
8330
+ this._stops.push(this._blocks.length);
8331
+ continue;
8332
+ }
8333
+ if (char === '{' || char === '}') {
8334
+ unmaskingBlock = !unmaskingBlock;
8335
+ continue;
8336
+ }
8337
+ if (char === '[' || char === ']') {
8338
+ optionalBlock = !optionalBlock;
8339
+ continue;
8340
+ }
8341
+ if (char === MaskedPattern.ESCAPE_CHAR) {
8342
+ ++i;
8343
+ char = pattern[i];
8344
+ if (!char) break;
8345
+ isInput = false;
8346
+ }
8347
+ const def = isInput ? new PatternInputDefinition({
8348
+ isOptional: optionalBlock,
8349
+ lazy: this.lazy,
8350
+ eager: this.eager,
8351
+ placeholderChar: this.placeholderChar,
8352
+ displayChar: this.displayChar,
8353
+ ...normalizeOpts(defs[char]),
8354
+ parent: this
8355
+ }) : new PatternFixedDefinition({
8356
+ char,
8357
+ eager: this.eager,
8358
+ isUnmasking: unmaskingBlock
8359
+ });
8360
+ this._blocks.push(def);
8361
+ }
8362
+ }
8363
+ get state() {
8364
+ return {
8365
+ ...super.state,
8366
+ _blocks: this._blocks.map(b => b.state)
8367
+ };
8368
+ }
8369
+ set state(state) {
8370
+ if (!state) {
8371
+ this.reset();
8372
+ return;
8373
+ }
8374
+ const {
8375
+ _blocks,
8376
+ ...maskedState
8377
+ } = state;
8378
+ this._blocks.forEach((b, bi) => b.state = _blocks[bi]);
8379
+ super.state = maskedState;
8380
+ }
8381
+ reset() {
8382
+ super.reset();
8383
+ this._blocks.forEach(b => b.reset());
8384
+ }
8385
+ get isComplete() {
8386
+ return this.exposeBlock ? this.exposeBlock.isComplete : this._blocks.every(b => b.isComplete);
8387
+ }
8388
+ get isFilled() {
8389
+ return this._blocks.every(b => b.isFilled);
8390
+ }
8391
+ get isFixed() {
8392
+ return this._blocks.every(b => b.isFixed);
8393
+ }
8394
+ get isOptional() {
8395
+ return this._blocks.every(b => b.isOptional);
8396
+ }
8397
+ doCommit() {
8398
+ this._blocks.forEach(b => b.doCommit());
8399
+ super.doCommit();
8400
+ }
8401
+ get unmaskedValue() {
8402
+ return this.exposeBlock ? this.exposeBlock.unmaskedValue : this._blocks.reduce((str, b) => str += b.unmaskedValue, '');
8403
+ }
8404
+ set unmaskedValue(unmaskedValue) {
8405
+ if (this.exposeBlock) {
8406
+ const tail = this.extractTail(this._blockStartPos(this._blocks.indexOf(this.exposeBlock)) + this.exposeBlock.displayValue.length);
8407
+ this.exposeBlock.unmaskedValue = unmaskedValue;
8408
+ this.appendTail(tail);
8409
+ this.doCommit();
8410
+ } else super.unmaskedValue = unmaskedValue;
8411
+ }
8412
+ get value() {
8413
+ return this.exposeBlock ? this.exposeBlock.value :
8414
+ // TODO return _value when not in change?
8415
+ this._blocks.reduce((str, b) => str += b.value, '');
8416
+ }
8417
+ set value(value) {
8418
+ if (this.exposeBlock) {
8419
+ const tail = this.extractTail(this._blockStartPos(this._blocks.indexOf(this.exposeBlock)) + this.exposeBlock.displayValue.length);
8420
+ this.exposeBlock.value = value;
8421
+ this.appendTail(tail);
8422
+ this.doCommit();
8423
+ } else super.value = value;
8424
+ }
8425
+ get typedValue() {
8426
+ return this.exposeBlock ? this.exposeBlock.typedValue : super.typedValue;
8427
+ }
8428
+ set typedValue(value) {
8429
+ if (this.exposeBlock) {
8430
+ const tail = this.extractTail(this._blockStartPos(this._blocks.indexOf(this.exposeBlock)) + this.exposeBlock.displayValue.length);
8431
+ this.exposeBlock.typedValue = value;
8432
+ this.appendTail(tail);
8433
+ this.doCommit();
8434
+ } else super.typedValue = value;
8435
+ }
8436
+ get displayValue() {
8437
+ return this._blocks.reduce((str, b) => str += b.displayValue, '');
8438
+ }
8439
+ appendTail(tail) {
8440
+ return super.appendTail(tail).aggregate(this._appendPlaceholder());
8441
+ }
8442
+ _appendEager() {
8443
+ var _this$_mapPosToBlock;
8444
+ const details = new ChangeDetails();
8445
+ let startBlockIndex = (_this$_mapPosToBlock = this._mapPosToBlock(this.displayValue.length)) == null ? void 0 : _this$_mapPosToBlock.index;
8446
+ if (startBlockIndex == null) return details;
8447
+
8448
+ // TODO test if it works for nested pattern masks
8449
+ if (this._blocks[startBlockIndex].isFilled) ++startBlockIndex;
8450
+ for (let bi = startBlockIndex; bi < this._blocks.length; ++bi) {
8451
+ const d = this._blocks[bi]._appendEager();
8452
+ if (!d.inserted) break;
8453
+ details.aggregate(d);
8454
+ }
8455
+ return details;
8456
+ }
8457
+ _appendCharRaw(ch, flags) {
8458
+ if (flags === void 0) {
8459
+ flags = {};
8460
+ }
8461
+ const blockIter = this._mapPosToBlock(this.displayValue.length);
8462
+ const details = new ChangeDetails();
8463
+ if (!blockIter) return details;
8464
+ for (let bi = blockIter.index, block; block = this._blocks[bi]; ++bi) {
8465
+ var _flags$_beforeTailSta;
8466
+ const blockDetails = block._appendChar(ch, {
8467
+ ...flags,
8468
+ _beforeTailState: (_flags$_beforeTailSta = flags._beforeTailState) == null || (_flags$_beforeTailSta = _flags$_beforeTailSta._blocks) == null ? void 0 : _flags$_beforeTailSta[bi]
8469
+ });
8470
+ details.aggregate(blockDetails);
8471
+ if (blockDetails.consumed) break; // go next char
8472
+ }
8473
+ return details;
8474
+ }
8475
+ extractTail(fromPos, toPos) {
8476
+ if (fromPos === void 0) {
8477
+ fromPos = 0;
8478
+ }
8479
+ if (toPos === void 0) {
8480
+ toPos = this.displayValue.length;
8481
+ }
8482
+ const chunkTail = new ChunksTailDetails();
8483
+ if (fromPos === toPos) return chunkTail;
8484
+ this._forEachBlocksInRange(fromPos, toPos, (b, bi, bFromPos, bToPos) => {
8485
+ const blockChunk = b.extractTail(bFromPos, bToPos);
8486
+ blockChunk.stop = this._findStopBefore(bi);
8487
+ blockChunk.from = this._blockStartPos(bi);
8488
+ if (blockChunk instanceof ChunksTailDetails) blockChunk.blockIndex = bi;
8489
+ chunkTail.extend(blockChunk);
8490
+ });
8491
+ return chunkTail;
8492
+ }
8493
+ extractInput(fromPos, toPos, flags) {
8494
+ if (fromPos === void 0) {
8495
+ fromPos = 0;
8496
+ }
8497
+ if (toPos === void 0) {
8498
+ toPos = this.displayValue.length;
8499
+ }
8500
+ if (flags === void 0) {
8501
+ flags = {};
8502
+ }
8503
+ if (fromPos === toPos) return '';
8504
+ let input = '';
8505
+ this._forEachBlocksInRange(fromPos, toPos, (b, _, fromPos, toPos) => {
8506
+ input += b.extractInput(fromPos, toPos, flags);
8507
+ });
8508
+ return input;
8509
+ }
8510
+ _findStopBefore(blockIndex) {
8511
+ let stopBefore;
8512
+ for (let si = 0; si < this._stops.length; ++si) {
8513
+ const stop = this._stops[si];
8514
+ if (stop <= blockIndex) stopBefore = stop;else break;
8515
+ }
8516
+ return stopBefore;
8517
+ }
8518
+
8519
+ /** Appends placeholder depending on laziness */
8520
+ _appendPlaceholder(toBlockIndex) {
8521
+ const details = new ChangeDetails();
8522
+ if (this.lazy && toBlockIndex == null) return details;
8523
+ const startBlockIter = this._mapPosToBlock(this.displayValue.length);
8524
+ if (!startBlockIter) return details;
8525
+ const startBlockIndex = startBlockIter.index;
8526
+ const endBlockIndex = toBlockIndex != null ? toBlockIndex : this._blocks.length;
8527
+ this._blocks.slice(startBlockIndex, endBlockIndex).forEach(b => {
8528
+ if (!b.lazy || toBlockIndex != null) {
8529
+ var _blocks2;
8530
+ details.aggregate(b._appendPlaceholder((_blocks2 = b._blocks) == null ? void 0 : _blocks2.length));
8531
+ }
8532
+ });
8533
+ return details;
8534
+ }
8535
+
8536
+ /** Finds block in pos */
8537
+ _mapPosToBlock(pos) {
8538
+ let accVal = '';
8539
+ for (let bi = 0; bi < this._blocks.length; ++bi) {
8540
+ const block = this._blocks[bi];
8541
+ const blockStartPos = accVal.length;
8542
+ accVal += block.displayValue;
8543
+ if (pos <= accVal.length) {
8544
+ return {
8545
+ index: bi,
8546
+ offset: pos - blockStartPos
8547
+ };
8548
+ }
8549
+ }
8550
+ }
8551
+ _blockStartPos(blockIndex) {
8552
+ return this._blocks.slice(0, blockIndex).reduce((pos, b) => pos += b.displayValue.length, 0);
8553
+ }
8554
+ _forEachBlocksInRange(fromPos, toPos, fn) {
8555
+ if (toPos === void 0) {
8556
+ toPos = this.displayValue.length;
8557
+ }
8558
+ const fromBlockIter = this._mapPosToBlock(fromPos);
8559
+ if (fromBlockIter) {
8560
+ const toBlockIter = this._mapPosToBlock(toPos);
8561
+ // process first block
8562
+ const isSameBlock = toBlockIter && fromBlockIter.index === toBlockIter.index;
8563
+ const fromBlockStartPos = fromBlockIter.offset;
8564
+ const fromBlockEndPos = toBlockIter && isSameBlock ? toBlockIter.offset : this._blocks[fromBlockIter.index].displayValue.length;
8565
+ fn(this._blocks[fromBlockIter.index], fromBlockIter.index, fromBlockStartPos, fromBlockEndPos);
8566
+ if (toBlockIter && !isSameBlock) {
8567
+ // process intermediate blocks
8568
+ for (let bi = fromBlockIter.index + 1; bi < toBlockIter.index; ++bi) {
8569
+ fn(this._blocks[bi], bi, 0, this._blocks[bi].displayValue.length);
8570
+ }
8571
+
8572
+ // process last block
8573
+ fn(this._blocks[toBlockIter.index], toBlockIter.index, 0, toBlockIter.offset);
8574
+ }
8575
+ }
8576
+ }
8577
+ remove(fromPos, toPos) {
8578
+ if (fromPos === void 0) {
8579
+ fromPos = 0;
8580
+ }
8581
+ if (toPos === void 0) {
8582
+ toPos = this.displayValue.length;
8583
+ }
8584
+ const removeDetails = super.remove(fromPos, toPos);
8585
+ this._forEachBlocksInRange(fromPos, toPos, (b, _, bFromPos, bToPos) => {
8586
+ removeDetails.aggregate(b.remove(bFromPos, bToPos));
8587
+ });
8588
+ return removeDetails;
8589
+ }
8590
+ nearestInputPos(cursorPos, direction) {
8591
+ if (direction === void 0) {
8592
+ direction = DIRECTION.NONE;
8593
+ }
8594
+ if (!this._blocks.length) return 0;
8595
+ const cursor = new PatternCursor(this, cursorPos);
8596
+ if (direction === DIRECTION.NONE) {
8597
+ // -------------------------------------------------
8598
+ // NONE should only go out from fixed to the right!
8599
+ // -------------------------------------------------
8600
+ if (cursor.pushRightBeforeInput()) return cursor.pos;
8601
+ cursor.popState();
8602
+ if (cursor.pushLeftBeforeInput()) return cursor.pos;
8603
+ return this.displayValue.length;
8604
+ }
8605
+
8606
+ // FORCE is only about a|* otherwise is 0
8607
+ if (direction === DIRECTION.LEFT || direction === DIRECTION.FORCE_LEFT) {
8608
+ // try to break fast when *|a
8609
+ if (direction === DIRECTION.LEFT) {
8610
+ cursor.pushRightBeforeFilled();
8611
+ if (cursor.ok && cursor.pos === cursorPos) return cursorPos;
8612
+ cursor.popState();
8613
+ }
8614
+
8615
+ // forward flow
8616
+ cursor.pushLeftBeforeInput();
8617
+ cursor.pushLeftBeforeRequired();
8618
+ cursor.pushLeftBeforeFilled();
8619
+
8620
+ // backward flow
8621
+ if (direction === DIRECTION.LEFT) {
8622
+ cursor.pushRightBeforeInput();
8623
+ cursor.pushRightBeforeRequired();
8624
+ if (cursor.ok && cursor.pos <= cursorPos) return cursor.pos;
8625
+ cursor.popState();
8626
+ if (cursor.ok && cursor.pos <= cursorPos) return cursor.pos;
8627
+ cursor.popState();
8628
+ }
8629
+ if (cursor.ok) return cursor.pos;
8630
+ if (direction === DIRECTION.FORCE_LEFT) return 0;
8631
+ cursor.popState();
8632
+ if (cursor.ok) return cursor.pos;
8633
+ cursor.popState();
8634
+ if (cursor.ok) return cursor.pos;
8635
+ return 0;
8636
+ }
8637
+ if (direction === DIRECTION.RIGHT || direction === DIRECTION.FORCE_RIGHT) {
8638
+ // forward flow
8639
+ cursor.pushRightBeforeInput();
8640
+ cursor.pushRightBeforeRequired();
8641
+ if (cursor.pushRightBeforeFilled()) return cursor.pos;
8642
+ if (direction === DIRECTION.FORCE_RIGHT) return this.displayValue.length;
8643
+
8644
+ // backward flow
8645
+ cursor.popState();
8646
+ if (cursor.ok) return cursor.pos;
8647
+ cursor.popState();
8648
+ if (cursor.ok) return cursor.pos;
8649
+ return this.nearestInputPos(cursorPos, DIRECTION.LEFT);
8650
+ }
8651
+ return cursorPos;
8652
+ }
8653
+ totalInputPositions(fromPos, toPos) {
8654
+ if (fromPos === void 0) {
8655
+ fromPos = 0;
8656
+ }
8657
+ if (toPos === void 0) {
8658
+ toPos = this.displayValue.length;
8659
+ }
8660
+ let total = 0;
8661
+ this._forEachBlocksInRange(fromPos, toPos, (b, _, bFromPos, bToPos) => {
8662
+ total += b.totalInputPositions(bFromPos, bToPos);
8663
+ });
8664
+ return total;
8665
+ }
8666
+
8667
+ /** Get block by name */
8668
+ maskedBlock(name) {
8669
+ return this.maskedBlocks(name)[0];
8670
+ }
8671
+
8672
+ /** Get all blocks by name */
8673
+ maskedBlocks(name) {
8674
+ const indices = this._maskedBlocks[name];
8675
+ if (!indices) return [];
8676
+ return indices.map(gi => this._blocks[gi]);
8677
+ }
8678
+ pad(flags) {
8679
+ const details = new ChangeDetails();
8680
+ this._forEachBlocksInRange(0, this.displayValue.length, b => details.aggregate(b.pad(flags)));
8681
+ return details;
8682
+ }
8683
+ }
8684
+ MaskedPattern.DEFAULTS = {
8685
+ ...Masked.DEFAULTS,
8686
+ lazy: true,
8687
+ placeholderChar: '_'
8688
+ };
8689
+ MaskedPattern.STOP_CHAR = '`';
8690
+ MaskedPattern.ESCAPE_CHAR = '\\';
8691
+ MaskedPattern.InputDefinition = PatternInputDefinition;
8692
+ MaskedPattern.FixedDefinition = PatternFixedDefinition;
8693
+ IMask.MaskedPattern = MaskedPattern;
6244
8694
 
6245
- /*!
6246
- * maska v1.5.0
6247
- * (c) 2019-2021 Alexander Shabunevich
6248
- * Released under the MIT License.
6249
- */
6250
- var maska = maska$1.exports;
8695
+ /** Pattern which accepts ranges */
8696
+ class MaskedRange extends MaskedPattern {
8697
+ /**
8698
+ Optionally sets max length of pattern.
8699
+ Used when pattern length is longer then `to` param length. Pads zeros at start in this case.
8700
+ */
8701
+
8702
+ /** Min bound */
8703
+
8704
+ /** Max bound */
8705
+
8706
+ get _matchFrom() {
8707
+ return this.maxLength - String(this.from).length;
8708
+ }
8709
+ constructor(opts) {
8710
+ super(opts); // mask will be created in _update
8711
+ }
8712
+ updateOptions(opts) {
8713
+ super.updateOptions(opts);
8714
+ }
8715
+ _update(opts) {
8716
+ const {
8717
+ to = this.to || 0,
8718
+ from = this.from || 0,
8719
+ maxLength = this.maxLength || 0,
8720
+ autofix = this.autofix,
8721
+ ...patternOpts
8722
+ } = opts;
8723
+ this.to = to;
8724
+ this.from = from;
8725
+ this.maxLength = Math.max(String(to).length, maxLength);
8726
+ this.autofix = autofix;
8727
+ const fromStr = String(this.from).padStart(this.maxLength, '0');
8728
+ const toStr = String(this.to).padStart(this.maxLength, '0');
8729
+ let sameCharsCount = 0;
8730
+ while (sameCharsCount < toStr.length && toStr[sameCharsCount] === fromStr[sameCharsCount]) ++sameCharsCount;
8731
+ patternOpts.mask = toStr.slice(0, sameCharsCount).replace(/0/g, '\\0') + '0'.repeat(this.maxLength - sameCharsCount);
8732
+ super._update(patternOpts);
8733
+ }
8734
+ get isComplete() {
8735
+ return super.isComplete && Boolean(this.value);
8736
+ }
8737
+ boundaries(str) {
8738
+ let minstr = '';
8739
+ let maxstr = '';
8740
+ const [, placeholder, num] = str.match(/^(\D*)(\d*)(\D*)/) || [];
8741
+ if (num) {
8742
+ minstr = '0'.repeat(placeholder.length) + num;
8743
+ maxstr = '9'.repeat(placeholder.length) + num;
8744
+ }
8745
+ minstr = minstr.padEnd(this.maxLength, '0');
8746
+ maxstr = maxstr.padEnd(this.maxLength, '9');
8747
+ return [minstr, maxstr];
8748
+ }
8749
+ doPrepareChar(ch, flags) {
8750
+ if (flags === void 0) {
8751
+ flags = {};
8752
+ }
8753
+ let details;
8754
+ [ch, details] = super.doPrepareChar(ch.replace(/\D/g, ''), flags);
8755
+ if (!ch) details.skip = !this.isComplete;
8756
+ return [ch, details];
8757
+ }
8758
+ _appendCharRaw(ch, flags) {
8759
+ if (flags === void 0) {
8760
+ flags = {};
8761
+ }
8762
+ if (!this.autofix || this.value.length + 1 > this.maxLength) return super._appendCharRaw(ch, flags);
8763
+ const fromStr = String(this.from).padStart(this.maxLength, '0');
8764
+ const toStr = String(this.to).padStart(this.maxLength, '0');
8765
+ const [minstr, maxstr] = this.boundaries(this.value + ch);
8766
+ if (Number(maxstr) < this.from) return super._appendCharRaw(fromStr[this.value.length], flags);
8767
+ if (Number(minstr) > this.to) {
8768
+ if (!flags.tail && this.autofix === 'pad' && this.value.length + 1 < this.maxLength) {
8769
+ return super._appendCharRaw(fromStr[this.value.length], flags).aggregate(this._appendCharRaw(ch, flags));
8770
+ }
8771
+ return super._appendCharRaw(toStr[this.value.length], flags);
8772
+ }
8773
+ return super._appendCharRaw(ch, flags);
8774
+ }
8775
+ doValidate(flags) {
8776
+ const str = this.value;
8777
+ const firstNonZero = str.search(/[^0]/);
8778
+ if (firstNonZero === -1 && str.length <= this._matchFrom) return true;
8779
+ const [minstr, maxstr] = this.boundaries(str);
8780
+ return this.from <= Number(maxstr) && Number(minstr) <= this.to && super.doValidate(flags);
8781
+ }
8782
+ pad(flags) {
8783
+ const details = new ChangeDetails();
8784
+ if (this.value.length === this.maxLength) return details;
8785
+ const value = this.value;
8786
+ const padLength = this.maxLength - this.value.length;
8787
+ if (padLength) {
8788
+ this.reset();
8789
+ for (let i = 0; i < padLength; ++i) {
8790
+ details.aggregate(super._appendCharRaw('0', flags));
8791
+ }
8792
+
8793
+ // append tail
8794
+ value.split('').forEach(ch => this._appendCharRaw(ch));
8795
+ }
8796
+ return details;
8797
+ }
8798
+ }
8799
+ IMask.MaskedRange = MaskedRange;
8800
+
8801
+ const DefaultPattern = 'd{.}`m{.}`Y';
8802
+
8803
+ // Make format and parse required when pattern is provided
8804
+
8805
+ /** Date mask */
8806
+ class MaskedDate extends MaskedPattern {
8807
+ static extractPatternOptions(opts) {
8808
+ const {
8809
+ mask,
8810
+ pattern,
8811
+ ...patternOpts
8812
+ } = opts;
8813
+ return {
8814
+ ...patternOpts,
8815
+ mask: isString(mask) ? mask : pattern
8816
+ };
8817
+ }
8818
+
8819
+ /** Pattern mask for date according to {@link MaskedDate#format} */
8820
+
8821
+ /** Start date */
8822
+
8823
+ /** End date */
8824
+
8825
+ /** Format typed value to string */
8826
+
8827
+ /** Parse string to get typed value */
8828
+
8829
+ constructor(opts) {
8830
+ super(MaskedDate.extractPatternOptions({
8831
+ ...MaskedDate.DEFAULTS,
8832
+ ...opts
8833
+ }));
8834
+ }
8835
+ updateOptions(opts) {
8836
+ super.updateOptions(opts);
8837
+ }
8838
+ _update(opts) {
8839
+ const {
8840
+ mask,
8841
+ pattern,
8842
+ blocks,
8843
+ ...patternOpts
8844
+ } = {
8845
+ ...MaskedDate.DEFAULTS,
8846
+ ...opts
8847
+ };
8848
+ const patternBlocks = Object.assign({}, MaskedDate.GET_DEFAULT_BLOCKS());
8849
+ // adjust year block
8850
+ if (opts.min) patternBlocks.Y.from = opts.min.getFullYear();
8851
+ if (opts.max) patternBlocks.Y.to = opts.max.getFullYear();
8852
+ if (opts.min && opts.max && patternBlocks.Y.from === patternBlocks.Y.to) {
8853
+ patternBlocks.m.from = opts.min.getMonth() + 1;
8854
+ patternBlocks.m.to = opts.max.getMonth() + 1;
8855
+ if (patternBlocks.m.from === patternBlocks.m.to) {
8856
+ patternBlocks.d.from = opts.min.getDate();
8857
+ patternBlocks.d.to = opts.max.getDate();
8858
+ }
8859
+ }
8860
+ Object.assign(patternBlocks, this.blocks, blocks);
8861
+ super._update({
8862
+ ...patternOpts,
8863
+ mask: isString(mask) ? mask : pattern,
8864
+ blocks: patternBlocks
8865
+ });
8866
+ }
8867
+ doValidate(flags) {
8868
+ const date = this.date;
8869
+ return super.doValidate(flags) && (!this.isComplete || this.isDateExist(this.value) && date != null && (this.min == null || this.min <= date) && (this.max == null || date <= this.max));
8870
+ }
8871
+
8872
+ /** Checks if date is exists */
8873
+ isDateExist(str) {
8874
+ return this.format(this.parse(str, this), this).indexOf(str) >= 0;
8875
+ }
8876
+
8877
+ /** Parsed Date */
8878
+ get date() {
8879
+ return this.typedValue;
8880
+ }
8881
+ set date(date) {
8882
+ this.typedValue = date;
8883
+ }
8884
+ get typedValue() {
8885
+ return this.isComplete ? super.typedValue : null;
8886
+ }
8887
+ set typedValue(value) {
8888
+ super.typedValue = value;
8889
+ }
8890
+ maskEquals(mask) {
8891
+ return mask === Date || super.maskEquals(mask);
8892
+ }
8893
+ optionsIsChanged(opts) {
8894
+ return super.optionsIsChanged(MaskedDate.extractPatternOptions(opts));
8895
+ }
8896
+ }
8897
+ MaskedDate.GET_DEFAULT_BLOCKS = () => ({
8898
+ d: {
8899
+ mask: MaskedRange,
8900
+ from: 1,
8901
+ to: 31,
8902
+ maxLength: 2
8903
+ },
8904
+ m: {
8905
+ mask: MaskedRange,
8906
+ from: 1,
8907
+ to: 12,
8908
+ maxLength: 2
8909
+ },
8910
+ Y: {
8911
+ mask: MaskedRange,
8912
+ from: 1900,
8913
+ to: 9999
8914
+ }
8915
+ });
8916
+ MaskedDate.DEFAULTS = {
8917
+ ...MaskedPattern.DEFAULTS,
8918
+ mask: Date,
8919
+ pattern: DefaultPattern,
8920
+ format: (date, masked) => {
8921
+ if (!date) return '';
8922
+ const day = String(date.getDate()).padStart(2, '0');
8923
+ const month = String(date.getMonth() + 1).padStart(2, '0');
8924
+ const year = date.getFullYear();
8925
+ return [day, month, year].join('.');
8926
+ },
8927
+ parse: (str, masked) => {
8928
+ const [day, month, year] = str.split('.').map(Number);
8929
+ return new Date(year, month - 1, day);
8930
+ }
8931
+ };
8932
+ IMask.MaskedDate = MaskedDate;
8933
+
8934
+ /** Dynamic mask for choosing appropriate mask in run-time */
8935
+ class MaskedDynamic extends Masked {
8936
+ constructor(opts) {
8937
+ super({
8938
+ ...MaskedDynamic.DEFAULTS,
8939
+ ...opts
8940
+ });
8941
+ this.currentMask = undefined;
8942
+ }
8943
+ updateOptions(opts) {
8944
+ super.updateOptions(opts);
8945
+ }
8946
+ _update(opts) {
8947
+ super._update(opts);
8948
+ if ('mask' in opts) {
8949
+ this.exposeMask = undefined;
8950
+ // mask could be totally dynamic with only `dispatch` option
8951
+ this.compiledMasks = Array.isArray(opts.mask) ? opts.mask.map(m => {
8952
+ const {
8953
+ expose,
8954
+ ...maskOpts
8955
+ } = normalizeOpts(m);
8956
+ const masked = createMask({
8957
+ overwrite: this._overwrite,
8958
+ eager: this._eager,
8959
+ skipInvalid: this._skipInvalid,
8960
+ ...maskOpts
8961
+ });
8962
+ if (expose) this.exposeMask = masked;
8963
+ return masked;
8964
+ }) : [];
8965
+
8966
+ // this.currentMask = this.doDispatch(''); // probably not needed but lets see
8967
+ }
8968
+ }
8969
+ _appendCharRaw(ch, flags) {
8970
+ if (flags === void 0) {
8971
+ flags = {};
8972
+ }
8973
+ const details = this._applyDispatch(ch, flags);
8974
+ if (this.currentMask) {
8975
+ details.aggregate(this.currentMask._appendChar(ch, this.currentMaskFlags(flags)));
8976
+ }
8977
+ return details;
8978
+ }
8979
+ _applyDispatch(appended, flags, tail) {
8980
+ if (appended === void 0) {
8981
+ appended = '';
8982
+ }
8983
+ if (flags === void 0) {
8984
+ flags = {};
8985
+ }
8986
+ if (tail === void 0) {
8987
+ tail = '';
8988
+ }
8989
+ const prevValueBeforeTail = flags.tail && flags._beforeTailState != null ? flags._beforeTailState._value : this.value;
8990
+ const inputValue = this.rawInputValue;
8991
+ const insertValue = flags.tail && flags._beforeTailState != null ? flags._beforeTailState._rawInputValue : inputValue;
8992
+ const tailValue = inputValue.slice(insertValue.length);
8993
+ const prevMask = this.currentMask;
8994
+ const details = new ChangeDetails();
8995
+ const prevMaskState = prevMask == null ? void 0 : prevMask.state;
8996
+
8997
+ // clone flags to prevent overwriting `_beforeTailState`
8998
+ this.currentMask = this.doDispatch(appended, {
8999
+ ...flags
9000
+ }, tail);
9001
+
9002
+ // restore state after dispatch
9003
+ if (this.currentMask) {
9004
+ if (this.currentMask !== prevMask) {
9005
+ // if mask changed reapply input
9006
+ this.currentMask.reset();
9007
+ if (insertValue) {
9008
+ this.currentMask.append(insertValue, {
9009
+ raw: true
9010
+ });
9011
+ details.tailShift = this.currentMask.value.length - prevValueBeforeTail.length;
9012
+ }
9013
+ if (tailValue) {
9014
+ details.tailShift += this.currentMask.append(tailValue, {
9015
+ raw: true,
9016
+ tail: true
9017
+ }).tailShift;
9018
+ }
9019
+ } else if (prevMaskState) {
9020
+ // Dispatch can do something bad with state, so
9021
+ // restore prev mask state
9022
+ this.currentMask.state = prevMaskState;
9023
+ }
9024
+ }
9025
+ return details;
9026
+ }
9027
+ _appendPlaceholder() {
9028
+ const details = this._applyDispatch();
9029
+ if (this.currentMask) {
9030
+ details.aggregate(this.currentMask._appendPlaceholder());
9031
+ }
9032
+ return details;
9033
+ }
9034
+ _appendEager() {
9035
+ const details = this._applyDispatch();
9036
+ if (this.currentMask) {
9037
+ details.aggregate(this.currentMask._appendEager());
9038
+ }
9039
+ return details;
9040
+ }
9041
+ appendTail(tail) {
9042
+ const details = new ChangeDetails();
9043
+ if (tail) details.aggregate(this._applyDispatch('', {}, tail));
9044
+ return details.aggregate(this.currentMask ? this.currentMask.appendTail(tail) : super.appendTail(tail));
9045
+ }
9046
+ currentMaskFlags(flags) {
9047
+ var _flags$_beforeTailSta, _flags$_beforeTailSta2;
9048
+ return {
9049
+ ...flags,
9050
+ _beforeTailState: ((_flags$_beforeTailSta = flags._beforeTailState) == null ? void 0 : _flags$_beforeTailSta.currentMaskRef) === this.currentMask && ((_flags$_beforeTailSta2 = flags._beforeTailState) == null ? void 0 : _flags$_beforeTailSta2.currentMask) || flags._beforeTailState
9051
+ };
9052
+ }
9053
+ doDispatch(appended, flags, tail) {
9054
+ if (flags === void 0) {
9055
+ flags = {};
9056
+ }
9057
+ if (tail === void 0) {
9058
+ tail = '';
9059
+ }
9060
+ return this.dispatch(appended, this, flags, tail);
9061
+ }
9062
+ doValidate(flags) {
9063
+ return super.doValidate(flags) && (!this.currentMask || this.currentMask.doValidate(this.currentMaskFlags(flags)));
9064
+ }
9065
+ doPrepare(str, flags) {
9066
+ if (flags === void 0) {
9067
+ flags = {};
9068
+ }
9069
+ let [s, details] = super.doPrepare(str, flags);
9070
+ if (this.currentMask) {
9071
+ let currentDetails;
9072
+ [s, currentDetails] = super.doPrepare(s, this.currentMaskFlags(flags));
9073
+ details = details.aggregate(currentDetails);
9074
+ }
9075
+ return [s, details];
9076
+ }
9077
+ doPrepareChar(str, flags) {
9078
+ if (flags === void 0) {
9079
+ flags = {};
9080
+ }
9081
+ let [s, details] = super.doPrepareChar(str, flags);
9082
+ if (this.currentMask) {
9083
+ let currentDetails;
9084
+ [s, currentDetails] = super.doPrepareChar(s, this.currentMaskFlags(flags));
9085
+ details = details.aggregate(currentDetails);
9086
+ }
9087
+ return [s, details];
9088
+ }
9089
+ reset() {
9090
+ var _this$currentMask;
9091
+ (_this$currentMask = this.currentMask) == null || _this$currentMask.reset();
9092
+ this.compiledMasks.forEach(m => m.reset());
9093
+ }
9094
+ get value() {
9095
+ return this.exposeMask ? this.exposeMask.value : this.currentMask ? this.currentMask.value : '';
9096
+ }
9097
+ set value(value) {
9098
+ if (this.exposeMask) {
9099
+ this.exposeMask.value = value;
9100
+ this.currentMask = this.exposeMask;
9101
+ this._applyDispatch();
9102
+ } else super.value = value;
9103
+ }
9104
+ get unmaskedValue() {
9105
+ return this.exposeMask ? this.exposeMask.unmaskedValue : this.currentMask ? this.currentMask.unmaskedValue : '';
9106
+ }
9107
+ set unmaskedValue(unmaskedValue) {
9108
+ if (this.exposeMask) {
9109
+ this.exposeMask.unmaskedValue = unmaskedValue;
9110
+ this.currentMask = this.exposeMask;
9111
+ this._applyDispatch();
9112
+ } else super.unmaskedValue = unmaskedValue;
9113
+ }
9114
+ get typedValue() {
9115
+ return this.exposeMask ? this.exposeMask.typedValue : this.currentMask ? this.currentMask.typedValue : '';
9116
+ }
9117
+ set typedValue(typedValue) {
9118
+ if (this.exposeMask) {
9119
+ this.exposeMask.typedValue = typedValue;
9120
+ this.currentMask = this.exposeMask;
9121
+ this._applyDispatch();
9122
+ return;
9123
+ }
9124
+ let unmaskedValue = String(typedValue);
9125
+
9126
+ // double check it
9127
+ if (this.currentMask) {
9128
+ this.currentMask.typedValue = typedValue;
9129
+ unmaskedValue = this.currentMask.unmaskedValue;
9130
+ }
9131
+ this.unmaskedValue = unmaskedValue;
9132
+ }
9133
+ get displayValue() {
9134
+ return this.currentMask ? this.currentMask.displayValue : '';
9135
+ }
9136
+ get isComplete() {
9137
+ var _this$currentMask2;
9138
+ return Boolean((_this$currentMask2 = this.currentMask) == null ? void 0 : _this$currentMask2.isComplete);
9139
+ }
9140
+ get isFilled() {
9141
+ var _this$currentMask3;
9142
+ return Boolean((_this$currentMask3 = this.currentMask) == null ? void 0 : _this$currentMask3.isFilled);
9143
+ }
9144
+ remove(fromPos, toPos) {
9145
+ const details = new ChangeDetails();
9146
+ if (this.currentMask) {
9147
+ details.aggregate(this.currentMask.remove(fromPos, toPos))
9148
+ // update with dispatch
9149
+ .aggregate(this._applyDispatch());
9150
+ }
9151
+ return details;
9152
+ }
9153
+ get state() {
9154
+ var _this$currentMask4;
9155
+ return {
9156
+ ...super.state,
9157
+ _rawInputValue: this.rawInputValue,
9158
+ compiledMasks: this.compiledMasks.map(m => m.state),
9159
+ currentMaskRef: this.currentMask,
9160
+ currentMask: (_this$currentMask4 = this.currentMask) == null ? void 0 : _this$currentMask4.state
9161
+ };
9162
+ }
9163
+ set state(state) {
9164
+ const {
9165
+ compiledMasks,
9166
+ currentMaskRef,
9167
+ currentMask,
9168
+ ...maskedState
9169
+ } = state;
9170
+ if (compiledMasks) this.compiledMasks.forEach((m, mi) => m.state = compiledMasks[mi]);
9171
+ if (currentMaskRef != null) {
9172
+ this.currentMask = currentMaskRef;
9173
+ this.currentMask.state = currentMask;
9174
+ }
9175
+ super.state = maskedState;
9176
+ }
9177
+ extractInput(fromPos, toPos, flags) {
9178
+ return this.currentMask ? this.currentMask.extractInput(fromPos, toPos, flags) : '';
9179
+ }
9180
+ extractTail(fromPos, toPos) {
9181
+ return this.currentMask ? this.currentMask.extractTail(fromPos, toPos) : super.extractTail(fromPos, toPos);
9182
+ }
9183
+ doCommit() {
9184
+ if (this.currentMask) this.currentMask.doCommit();
9185
+ super.doCommit();
9186
+ }
9187
+ nearestInputPos(cursorPos, direction) {
9188
+ return this.currentMask ? this.currentMask.nearestInputPos(cursorPos, direction) : super.nearestInputPos(cursorPos, direction);
9189
+ }
9190
+ get overwrite() {
9191
+ return this.currentMask ? this.currentMask.overwrite : this._overwrite;
9192
+ }
9193
+ set overwrite(overwrite) {
9194
+ this._overwrite = overwrite;
9195
+ }
9196
+ get eager() {
9197
+ return this.currentMask ? this.currentMask.eager : this._eager;
9198
+ }
9199
+ set eager(eager) {
9200
+ this._eager = eager;
9201
+ }
9202
+ get skipInvalid() {
9203
+ return this.currentMask ? this.currentMask.skipInvalid : this._skipInvalid;
9204
+ }
9205
+ set skipInvalid(skipInvalid) {
9206
+ this._skipInvalid = skipInvalid;
9207
+ }
9208
+ get autofix() {
9209
+ return this.currentMask ? this.currentMask.autofix : this._autofix;
9210
+ }
9211
+ set autofix(autofix) {
9212
+ this._autofix = autofix;
9213
+ }
9214
+ maskEquals(mask) {
9215
+ return Array.isArray(mask) ? this.compiledMasks.every((m, mi) => {
9216
+ if (!mask[mi]) return;
9217
+ const {
9218
+ mask: oldMask,
9219
+ ...restOpts
9220
+ } = mask[mi];
9221
+ return objectIncludes(m, restOpts) && m.maskEquals(oldMask);
9222
+ }) : super.maskEquals(mask);
9223
+ }
9224
+ typedValueEquals(value) {
9225
+ var _this$currentMask5;
9226
+ return Boolean((_this$currentMask5 = this.currentMask) == null ? void 0 : _this$currentMask5.typedValueEquals(value));
9227
+ }
9228
+ }
9229
+ /** Currently chosen mask */
9230
+ /** Currently chosen mask */
9231
+ /** Compliled {@link Masked} options */
9232
+ /** Chooses {@link Masked} depending on input value */
9233
+ MaskedDynamic.DEFAULTS = {
9234
+ ...Masked.DEFAULTS,
9235
+ dispatch: (appended, masked, flags, tail) => {
9236
+ if (!masked.compiledMasks.length) return;
9237
+ const inputValue = masked.rawInputValue;
9238
+
9239
+ // simulate input
9240
+ const inputs = masked.compiledMasks.map((m, index) => {
9241
+ const isCurrent = masked.currentMask === m;
9242
+ const startInputPos = isCurrent ? m.displayValue.length : m.nearestInputPos(m.displayValue.length, DIRECTION.FORCE_LEFT);
9243
+ if (m.rawInputValue !== inputValue) {
9244
+ m.reset();
9245
+ m.append(inputValue, {
9246
+ raw: true
9247
+ });
9248
+ } else if (!isCurrent) {
9249
+ m.remove(startInputPos);
9250
+ }
9251
+ m.append(appended, masked.currentMaskFlags(flags));
9252
+ m.appendTail(tail);
9253
+ return {
9254
+ index,
9255
+ weight: m.rawInputValue.length,
9256
+ totalInputPositions: m.totalInputPositions(0, Math.max(startInputPos, m.nearestInputPos(m.displayValue.length, DIRECTION.FORCE_LEFT)))
9257
+ };
9258
+ });
9259
+
9260
+ // pop masks with longer values first
9261
+ inputs.sort((i1, i2) => i2.weight - i1.weight || i2.totalInputPositions - i1.totalInputPositions);
9262
+ return masked.compiledMasks[inputs[0].index];
9263
+ }
9264
+ };
9265
+ IMask.MaskedDynamic = MaskedDynamic;
9266
+
9267
+ /** Pattern which validates enum values */
9268
+ class MaskedEnum extends MaskedPattern {
9269
+ constructor(opts) {
9270
+ super({
9271
+ ...MaskedEnum.DEFAULTS,
9272
+ ...opts
9273
+ }); // mask will be created in _update
9274
+ }
9275
+ updateOptions(opts) {
9276
+ super.updateOptions(opts);
9277
+ }
9278
+ _update(opts) {
9279
+ const {
9280
+ enum: enum_,
9281
+ ...eopts
9282
+ } = opts;
9283
+ if (enum_) {
9284
+ const lengths = enum_.map(e => e.length);
9285
+ const requiredLength = Math.min(...lengths);
9286
+ const optionalLength = Math.max(...lengths) - requiredLength;
9287
+ eopts.mask = '*'.repeat(requiredLength);
9288
+ if (optionalLength) eopts.mask += '[' + '*'.repeat(optionalLength) + ']';
9289
+ this.enum = enum_;
9290
+ }
9291
+ super._update(eopts);
9292
+ }
9293
+ _appendCharRaw(ch, flags) {
9294
+ if (flags === void 0) {
9295
+ flags = {};
9296
+ }
9297
+ const matchFrom = Math.min(this.nearestInputPos(0, DIRECTION.FORCE_RIGHT), this.value.length);
9298
+ const matches = this.enum.filter(e => this.matchValue(e, this.unmaskedValue + ch, matchFrom));
9299
+ if (matches.length) {
9300
+ if (matches.length === 1) {
9301
+ this._forEachBlocksInRange(0, this.value.length, (b, bi) => {
9302
+ const mch = matches[0][bi];
9303
+ if (bi >= this.value.length || mch === b.value) return;
9304
+ b.reset();
9305
+ b._appendChar(mch, flags);
9306
+ });
9307
+ }
9308
+ const d = super._appendCharRaw(matches[0][this.value.length], flags);
9309
+ if (matches.length === 1) {
9310
+ matches[0].slice(this.unmaskedValue.length).split('').forEach(mch => d.aggregate(super._appendCharRaw(mch)));
9311
+ }
9312
+ return d;
9313
+ }
9314
+ return new ChangeDetails({
9315
+ skip: !this.isComplete
9316
+ });
9317
+ }
9318
+ extractTail(fromPos, toPos) {
9319
+ if (fromPos === void 0) {
9320
+ fromPos = 0;
9321
+ }
9322
+ // just drop tail
9323
+ return new ContinuousTailDetails('', fromPos);
9324
+ }
9325
+ remove(fromPos, toPos) {
9326
+ if (fromPos === void 0) {
9327
+ fromPos = 0;
9328
+ }
9329
+ if (toPos === void 0) {
9330
+ toPos = this.displayValue.length;
9331
+ }
9332
+ if (fromPos === toPos) return new ChangeDetails();
9333
+ const matchFrom = Math.min(super.nearestInputPos(0, DIRECTION.FORCE_RIGHT), this.value.length);
9334
+ let pos;
9335
+ for (pos = fromPos; pos >= 0; --pos) {
9336
+ const matches = this.enum.filter(e => this.matchValue(e, this.value.slice(matchFrom, pos), matchFrom));
9337
+ if (matches.length > 1) break;
9338
+ }
9339
+ const details = super.remove(pos, toPos);
9340
+ details.tailShift += pos - fromPos;
9341
+ return details;
9342
+ }
9343
+ get isComplete() {
9344
+ return this.enum.indexOf(this.value) >= 0;
9345
+ }
9346
+ }
9347
+ /** Match enum value */
9348
+ MaskedEnum.DEFAULTS = {
9349
+ ...MaskedPattern.DEFAULTS,
9350
+ matchValue: (estr, istr, matchFrom) => estr.indexOf(istr, matchFrom) === matchFrom
9351
+ };
9352
+ IMask.MaskedEnum = MaskedEnum;
9353
+
9354
+ /** Masking by custom Function */
9355
+ class MaskedFunction extends Masked {
9356
+ /** */
9357
+
9358
+ /** Enable characters overwriting */
9359
+
9360
+ /** */
9361
+
9362
+ /** */
9363
+
9364
+ /** */
9365
+
9366
+ updateOptions(opts) {
9367
+ super.updateOptions(opts);
9368
+ }
9369
+ _update(opts) {
9370
+ super._update({
9371
+ ...opts,
9372
+ validate: opts.mask
9373
+ });
9374
+ }
9375
+ }
9376
+ IMask.MaskedFunction = MaskedFunction;
9377
+
9378
+ var _MaskedNumber;
9379
+ /** Number mask */
9380
+ class MaskedNumber extends Masked {
9381
+ /** Single char */
9382
+
9383
+ /** Single char */
9384
+
9385
+ /** Array of single chars */
9386
+
9387
+ /** */
9388
+
9389
+ /** */
9390
+
9391
+ /** Digits after point */
9392
+
9393
+ /** Flag to remove leading and trailing zeros in the end of editing */
9394
+
9395
+ /** Flag to pad trailing zeros after point in the end of editing */
9396
+
9397
+ /** Enable characters overwriting */
9398
+
9399
+ /** */
9400
+
9401
+ /** */
9402
+
9403
+ /** */
9404
+
9405
+ /** Format typed value to string */
9406
+
9407
+ /** Parse string to get typed value */
9408
+
9409
+ constructor(opts) {
9410
+ super({
9411
+ ...MaskedNumber.DEFAULTS,
9412
+ ...opts
9413
+ });
9414
+ }
9415
+ updateOptions(opts) {
9416
+ super.updateOptions(opts);
9417
+ }
9418
+ _update(opts) {
9419
+ super._update(opts);
9420
+ this._updateRegExps();
9421
+ }
9422
+ _updateRegExps() {
9423
+ const start = '^' + (this.allowNegative ? '[+|\\-]?' : '');
9424
+ const mid = '\\d*';
9425
+ const end = (this.scale ? "(" + escapeRegExp(this.radix) + "\\d{0," + this.scale + "})?" : '') + '$';
9426
+ this._numberRegExp = new RegExp(start + mid + end);
9427
+ this._mapToRadixRegExp = new RegExp("[" + this.mapToRadix.map(escapeRegExp).join('') + "]", 'g');
9428
+ this._thousandsSeparatorRegExp = new RegExp(escapeRegExp(this.thousandsSeparator), 'g');
9429
+ }
9430
+ _removeThousandsSeparators(value) {
9431
+ return value.replace(this._thousandsSeparatorRegExp, '');
9432
+ }
9433
+ _insertThousandsSeparators(value) {
9434
+ // https://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as-thousands-separators-in-javascript
9435
+ const parts = value.split(this.radix);
9436
+ parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, this.thousandsSeparator);
9437
+ return parts.join(this.radix);
9438
+ }
9439
+ doPrepareChar(ch, flags) {
9440
+ if (flags === void 0) {
9441
+ flags = {};
9442
+ }
9443
+ const [prepCh, details] = super.doPrepareChar(this._removeThousandsSeparators(this.scale && this.mapToRadix.length && (
9444
+ /*
9445
+ radix should be mapped when
9446
+ 1) input is done from keyboard = flags.input && flags.raw
9447
+ 2) unmasked value is set = !flags.input && !flags.raw
9448
+ and should not be mapped when
9449
+ 1) value is set = flags.input && !flags.raw
9450
+ 2) raw value is set = !flags.input && flags.raw
9451
+ */
9452
+ flags.input && flags.raw || !flags.input && !flags.raw) ? ch.replace(this._mapToRadixRegExp, this.radix) : ch), flags);
9453
+ if (ch && !prepCh) details.skip = true;
9454
+ if (prepCh && !this.allowPositive && !this.value && prepCh !== '-') details.aggregate(this._appendChar('-'));
9455
+ return [prepCh, details];
9456
+ }
9457
+ _separatorsCount(to, extendOnSeparators) {
9458
+ if (extendOnSeparators === void 0) {
9459
+ extendOnSeparators = false;
9460
+ }
9461
+ let count = 0;
9462
+ for (let pos = 0; pos < to; ++pos) {
9463
+ if (this._value.indexOf(this.thousandsSeparator, pos) === pos) {
9464
+ ++count;
9465
+ if (extendOnSeparators) to += this.thousandsSeparator.length;
9466
+ }
9467
+ }
9468
+ return count;
9469
+ }
9470
+ _separatorsCountFromSlice(slice) {
9471
+ if (slice === void 0) {
9472
+ slice = this._value;
9473
+ }
9474
+ return this._separatorsCount(this._removeThousandsSeparators(slice).length, true);
9475
+ }
9476
+ extractInput(fromPos, toPos, flags) {
9477
+ if (fromPos === void 0) {
9478
+ fromPos = 0;
9479
+ }
9480
+ if (toPos === void 0) {
9481
+ toPos = this.displayValue.length;
9482
+ }
9483
+ [fromPos, toPos] = this._adjustRangeWithSeparators(fromPos, toPos);
9484
+ return this._removeThousandsSeparators(super.extractInput(fromPos, toPos, flags));
9485
+ }
9486
+ _appendCharRaw(ch, flags) {
9487
+ if (flags === void 0) {
9488
+ flags = {};
9489
+ }
9490
+ const prevBeforeTailValue = flags.tail && flags._beforeTailState ? flags._beforeTailState._value : this._value;
9491
+ const prevBeforeTailSeparatorsCount = this._separatorsCountFromSlice(prevBeforeTailValue);
9492
+ this._value = this._removeThousandsSeparators(this.value);
9493
+ const oldValue = this._value;
9494
+ this._value += ch;
9495
+ const num = this.number;
9496
+ let accepted = !isNaN(num);
9497
+ let skip = false;
9498
+ if (accepted) {
9499
+ let fixedNum;
9500
+ if (this.min != null && this.min < 0 && this.number < this.min) fixedNum = this.min;
9501
+ if (this.max != null && this.max > 0 && this.number > this.max) fixedNum = this.max;
9502
+ if (fixedNum != null) {
9503
+ if (this.autofix) {
9504
+ this._value = this.format(fixedNum, this).replace(MaskedNumber.UNMASKED_RADIX, this.radix);
9505
+ skip || (skip = oldValue === this._value && !flags.tail); // if not changed on tail it's still ok to proceed
9506
+ } else {
9507
+ accepted = false;
9508
+ }
9509
+ }
9510
+ accepted && (accepted = Boolean(this._value.match(this._numberRegExp)));
9511
+ }
9512
+ let appendDetails;
9513
+ if (!accepted) {
9514
+ this._value = oldValue;
9515
+ appendDetails = new ChangeDetails();
9516
+ } else {
9517
+ appendDetails = new ChangeDetails({
9518
+ inserted: this._value.slice(oldValue.length),
9519
+ rawInserted: skip ? '' : ch,
9520
+ skip
9521
+ });
9522
+ }
9523
+ this._value = this._insertThousandsSeparators(this._value);
9524
+ const beforeTailValue = flags.tail && flags._beforeTailState ? flags._beforeTailState._value : this._value;
9525
+ const beforeTailSeparatorsCount = this._separatorsCountFromSlice(beforeTailValue);
9526
+ appendDetails.tailShift += (beforeTailSeparatorsCount - prevBeforeTailSeparatorsCount) * this.thousandsSeparator.length;
9527
+ return appendDetails;
9528
+ }
9529
+ _findSeparatorAround(pos) {
9530
+ if (this.thousandsSeparator) {
9531
+ const searchFrom = pos - this.thousandsSeparator.length + 1;
9532
+ const separatorPos = this.value.indexOf(this.thousandsSeparator, searchFrom);
9533
+ if (separatorPos <= pos) return separatorPos;
9534
+ }
9535
+ return -1;
9536
+ }
9537
+ _adjustRangeWithSeparators(from, to) {
9538
+ const separatorAroundFromPos = this._findSeparatorAround(from);
9539
+ if (separatorAroundFromPos >= 0) from = separatorAroundFromPos;
9540
+ const separatorAroundToPos = this._findSeparatorAround(to);
9541
+ if (separatorAroundToPos >= 0) to = separatorAroundToPos + this.thousandsSeparator.length;
9542
+ return [from, to];
9543
+ }
9544
+ remove(fromPos, toPos) {
9545
+ if (fromPos === void 0) {
9546
+ fromPos = 0;
9547
+ }
9548
+ if (toPos === void 0) {
9549
+ toPos = this.displayValue.length;
9550
+ }
9551
+ [fromPos, toPos] = this._adjustRangeWithSeparators(fromPos, toPos);
9552
+ const valueBeforePos = this.value.slice(0, fromPos);
9553
+ const valueAfterPos = this.value.slice(toPos);
9554
+ const prevBeforeTailSeparatorsCount = this._separatorsCount(valueBeforePos.length);
9555
+ this._value = this._insertThousandsSeparators(this._removeThousandsSeparators(valueBeforePos + valueAfterPos));
9556
+ const beforeTailSeparatorsCount = this._separatorsCountFromSlice(valueBeforePos);
9557
+ return new ChangeDetails({
9558
+ tailShift: (beforeTailSeparatorsCount - prevBeforeTailSeparatorsCount) * this.thousandsSeparator.length
9559
+ });
9560
+ }
9561
+ nearestInputPos(cursorPos, direction) {
9562
+ if (!this.thousandsSeparator) return cursorPos;
9563
+ switch (direction) {
9564
+ case DIRECTION.NONE:
9565
+ case DIRECTION.LEFT:
9566
+ case DIRECTION.FORCE_LEFT:
9567
+ {
9568
+ const separatorAtLeftPos = this._findSeparatorAround(cursorPos - 1);
9569
+ if (separatorAtLeftPos >= 0) {
9570
+ const separatorAtLeftEndPos = separatorAtLeftPos + this.thousandsSeparator.length;
9571
+ if (cursorPos < separatorAtLeftEndPos || this.value.length <= separatorAtLeftEndPos || direction === DIRECTION.FORCE_LEFT) {
9572
+ return separatorAtLeftPos;
9573
+ }
9574
+ }
9575
+ break;
9576
+ }
9577
+ case DIRECTION.RIGHT:
9578
+ case DIRECTION.FORCE_RIGHT:
9579
+ {
9580
+ const separatorAtRightPos = this._findSeparatorAround(cursorPos);
9581
+ if (separatorAtRightPos >= 0) {
9582
+ return separatorAtRightPos + this.thousandsSeparator.length;
9583
+ }
9584
+ }
9585
+ }
9586
+ return cursorPos;
9587
+ }
9588
+ doCommit() {
9589
+ if (this.value) {
9590
+ const number = this.number;
9591
+ let validnum = number;
9592
+
9593
+ // check bounds
9594
+ if (this.min != null) validnum = Math.max(validnum, this.min);
9595
+ if (this.max != null) validnum = Math.min(validnum, this.max);
9596
+ if (validnum !== number) this.unmaskedValue = this.format(validnum, this);
9597
+ let formatted = this.value;
9598
+ if (this.normalizeZeros) formatted = this._normalizeZeros(formatted);
9599
+ if (this.padFractionalZeros && this.scale > 0) formatted = this._padFractionalZeros(formatted);
9600
+ this._value = formatted;
9601
+ }
9602
+ super.doCommit();
9603
+ }
9604
+ _normalizeZeros(value) {
9605
+ const parts = this._removeThousandsSeparators(value).split(this.radix);
9606
+
9607
+ // remove leading zeros
9608
+ parts[0] = parts[0].replace(/^(\D*)(0*)(\d*)/, (match, sign, zeros, num) => sign + num);
9609
+ // add leading zero
9610
+ if (value.length && !/\d$/.test(parts[0])) parts[0] = parts[0] + '0';
9611
+ if (parts.length > 1) {
9612
+ parts[1] = parts[1].replace(/0*$/, ''); // remove trailing zeros
9613
+ if (!parts[1].length) parts.length = 1; // remove fractional
9614
+ }
9615
+ return this._insertThousandsSeparators(parts.join(this.radix));
9616
+ }
9617
+ _padFractionalZeros(value) {
9618
+ if (!value) return value;
9619
+ const parts = value.split(this.radix);
9620
+ if (parts.length < 2) parts.push('');
9621
+ parts[1] = parts[1].padEnd(this.scale, '0');
9622
+ return parts.join(this.radix);
9623
+ }
9624
+ doSkipInvalid(ch, flags, checkTail) {
9625
+ if (flags === void 0) {
9626
+ flags = {};
9627
+ }
9628
+ const dropFractional = this.scale === 0 && ch !== this.thousandsSeparator && (ch === this.radix || ch === MaskedNumber.UNMASKED_RADIX || this.mapToRadix.includes(ch));
9629
+ return super.doSkipInvalid(ch, flags, checkTail) && !dropFractional;
9630
+ }
9631
+ get unmaskedValue() {
9632
+ return this._removeThousandsSeparators(this._normalizeZeros(this.value)).replace(this.radix, MaskedNumber.UNMASKED_RADIX);
9633
+ }
9634
+ set unmaskedValue(unmaskedValue) {
9635
+ super.unmaskedValue = unmaskedValue;
9636
+ }
9637
+ get typedValue() {
9638
+ return this.parse(this.unmaskedValue, this);
9639
+ }
9640
+ set typedValue(n) {
9641
+ this.rawInputValue = this.format(n, this).replace(MaskedNumber.UNMASKED_RADIX, this.radix);
9642
+ }
9643
+
9644
+ /** Parsed Number */
9645
+ get number() {
9646
+ return this.typedValue;
9647
+ }
9648
+ set number(number) {
9649
+ this.typedValue = number;
9650
+ }
9651
+ get allowNegative() {
9652
+ return this.min != null && this.min < 0 || this.max != null && this.max < 0;
9653
+ }
9654
+ get allowPositive() {
9655
+ return this.min != null && this.min > 0 || this.max != null && this.max > 0;
9656
+ }
9657
+ typedValueEquals(value) {
9658
+ // handle 0 -> '' case (typed = 0 even if value = '')
9659
+ // for details see https://github.com/uNmAnNeR/imaskjs/issues/134
9660
+ return (super.typedValueEquals(value) || MaskedNumber.EMPTY_VALUES.includes(value) && MaskedNumber.EMPTY_VALUES.includes(this.typedValue)) && !(value === 0 && this.value === '');
9661
+ }
9662
+ }
9663
+ _MaskedNumber = MaskedNumber;
9664
+ MaskedNumber.UNMASKED_RADIX = '.';
9665
+ MaskedNumber.EMPTY_VALUES = [...Masked.EMPTY_VALUES, 0];
9666
+ MaskedNumber.DEFAULTS = {
9667
+ ...Masked.DEFAULTS,
9668
+ mask: Number,
9669
+ radix: ',',
9670
+ thousandsSeparator: '',
9671
+ mapToRadix: [_MaskedNumber.UNMASKED_RADIX],
9672
+ min: Number.MIN_SAFE_INTEGER,
9673
+ max: Number.MAX_SAFE_INTEGER,
9674
+ scale: 2,
9675
+ normalizeZeros: true,
9676
+ padFractionalZeros: false,
9677
+ parse: Number,
9678
+ format: n => n.toLocaleString('en-US', {
9679
+ useGrouping: false,
9680
+ maximumFractionDigits: 20
9681
+ })
9682
+ };
9683
+ IMask.MaskedNumber = MaskedNumber;
6251
9684
 
6252
- var hasRequiredMaska;
9685
+ /** Mask pipe source and destination types */
9686
+ const PIPE_TYPE = {
9687
+ MASKED: 'value',
9688
+ UNMASKED: 'unmaskedValue',
9689
+ TYPED: 'typedValue'
9690
+ };
9691
+ /** Creates new pipe function depending on mask type, source and destination options */
9692
+ function createPipe(arg, from, to) {
9693
+ if (from === void 0) {
9694
+ from = PIPE_TYPE.MASKED;
9695
+ }
9696
+ if (to === void 0) {
9697
+ to = PIPE_TYPE.MASKED;
9698
+ }
9699
+ const masked = createMask(arg);
9700
+ return value => masked.runIsolated(m => {
9701
+ m[from] = value;
9702
+ return m[to];
9703
+ });
9704
+ }
6253
9705
 
6254
- function requireMaska () {
6255
- if (hasRequiredMaska) return maska$1.exports;
6256
- hasRequiredMaska = 1;
6257
- (function (module, exports) {
6258
- !function(e,t){t(exports);}(maska,(function(e){var t={"#":{pattern:/[0-9]/},X:{pattern:/[0-9a-zA-Z]/},S:{pattern:/[a-zA-Z]/},A:{pattern:/[a-zA-Z]/,uppercase:true},a:{pattern:/[a-zA-Z]/,lowercase:true},"!":{escape:true},"*":{repeat:true}};function n(e,n,r=t,o=true){return s(n).length>1?function(e){const t=s(e).sort(((e,t)=>e.length-t.length));return function(e,s,r,o=true){const i=t.map((t=>a(e,t,r,false))).pop();for(const s in t)if(n(i,t[s],r))return a(e,t[s],r,o);return ""};function n(e,t,n){for(const e in n)n[e].escape&&(t=t.replace(new RegExp(e+".{1}","g"),""));return t.split("").filter((e=>n[e]&&n[e].pattern)).length>=e.length}}(n)(e,n,r,o):a(e,n,r,o)}function s(e){try{return JSON.parse(e)}catch{return [e]}}function a(e,t,n,s=true){let a=0,o=0,i="",u="";for(;a<t.length&&o<e.length;){let u=t[a];const p=e[o],l=n[u];if(l&&l.pattern)l.pattern.test(p)&&(i+=r(p,l),a++,s&&t[a]&&(n[t[a]]?n[t[a]]&&n[t[a]].escape&&(i+=t[a+1],a+=2):(i+=t[a],a++))),o++;else if(l&&l.repeat){const e=n[t[a-1]];e&&!e.pattern.test(p)?a++:a--;}else l&&l.escape&&(a++,u=t[a]),s&&(i+=u),p===u&&o++,a++;}for(;s&&a<t.length;){const e=t[a];if(n[e]){u="";break}u+=e,a++;}return i+u}function r(e,t){return t.transform&&(e=t.transform(e)),t.uppercase?e.toLocaleUpperCase():t.lowercase?e.toLocaleLowerCase():e}function o(e){return e instanceof HTMLInputElement?e:e.querySelector("input")||e}function i(e){return "[object String]"===Object.prototype.toString.call(e)}class u{constructor(e,n={}){if(!e)throw new Error("Maska: no element for mask");if(null!=n.preprocessor&&"function"!=typeof n.preprocessor)throw new Error("Maska: preprocessor must be a function");if(n.tokens)for(const e in n.tokens)n.tokens[e]={...n.tokens[e]},n.tokens[e].pattern&&i(n.tokens[e].pattern)&&(n.tokens[e].pattern=new RegExp(n.tokens[e].pattern));this._opts={mask:n.mask,tokens:{...t,...n.tokens},preprocessor:n.preprocessor},this._el=i(e)?document.querySelectorAll(e):e.length?e:[e],this.inputEvent=e=>this.updateValue(e.target,e),this.init();}init(){for(let e=0;e<this._el.length;e++){const t=o(this._el[e]);!this._opts.mask||t.dataset.mask&&t.dataset.mask===this._opts.mask||(t.dataset.mask=this._opts.mask),setTimeout((()=>this.updateValue(t)),0),t.dataset.maskInited||(t.dataset.maskInited=true,t.addEventListener("input",this.inputEvent),t.addEventListener("beforeinput",this.beforeInput));}}destroy(){for(let e=0;e<this._el.length;e++){const t=o(this._el[e]);t.removeEventListener("input",this.inputEvent),t.removeEventListener("beforeinput",this.beforeInput),delete t.dataset.mask,delete t.dataset.maskInited;}}updateValue(e,t){if(!e||!e.type)return;const s=e.type.match(/^number$/i)&&e.validity.badInput;if(!e.value&&!s||!e.dataset.mask)return e.dataset.maskRawValue="",void this.dispatch("maska",e,t);let a=e.selectionEnd;const r=e.value,o=r[a-1];e.dataset.maskRawValue=n(e.value,e.dataset.mask,this._opts.tokens,false);let i=e.value;this._opts.preprocessor&&(i=this._opts.preprocessor(i)),e.value=n(i,e.dataset.mask,this._opts.tokens),t&&"insertText"===t.inputType&&a===r.length&&(a=e.value.length),function(e,t,n){for(;t&&t<e.value.length&&e.value.charAt(t-1)!==n;)t++;(e.type?e.type.match(/^(text|search|password|tel|url)$/i):!e.type)&&e===document.activeElement&&(e.setSelectionRange(t,t),setTimeout((function(){e.setSelectionRange(t,t);}),0));}(e,a,o),this.dispatch("maska",e,t),e.value!==r&&this.dispatch("input",e,t);}beforeInput(e){e&&e.target&&e.target.type&&e.target.type.match(/^number$/i)&&e.data&&isNaN(e.target.value+e.data)&&e.preventDefault();}dispatch(e,t,n){t.dispatchEvent(function(e,t=null){const n=document.createEvent("Event");return n.initEvent(e,true,true),t&&(n.inputType=t),n}(e,n&&n.inputType||null));}}var p=(()=>{const e=new WeakMap;return (t,n)=>{n.value&&(e.has(t)&&!function(e){return !(i(e.value)&&e.value===e.oldValue||Array.isArray(e.value)&&JSON.stringify(e.value)===JSON.stringify(e.oldValue)||e.value&&e.value.mask&&e.oldValue&&e.oldValue.mask&&e.value.mask===e.oldValue.mask)}(n)||e.set(t,new u(t,function(e){const t={};return e.mask?(t.mask=Array.isArray(e.mask)?JSON.stringify(e.mask):e.mask,t.tokens=e.tokens?{...e.tokens}:{},t.preprocessor=e.preprocessor):t.mask=Array.isArray(e)?JSON.stringify(e):e,t}(n.value))));}})();function l(e){e.directive("maska",p);}"undefined"!=typeof window&&window.Vue&&window.Vue.use&&window.Vue.use(l),e.create=function(e,t){return new u(e,t)},e.default=l,e.install=l,e.mask=n,e.maska=p,e.tokens=t,Object.defineProperty(e,"__esModule",{value:true});}));
6259
- } (maska$1, maska$1.exports));
6260
- return maska$1.exports;
9706
+ /** Pipes value through mask depending on mask type, source and destination options */
9707
+ function pipe(value, mask, from, to) {
9708
+ return createPipe(mask, from, to)(value);
9709
+ }
9710
+ IMask.PIPE_TYPE = PIPE_TYPE;
9711
+ IMask.createPipe = createPipe;
9712
+ IMask.pipe = pipe;
9713
+
9714
+ /** Pattern mask */
9715
+ class RepeatBlock extends MaskedPattern {
9716
+ get repeatFrom() {
9717
+ var _ref;
9718
+ return (_ref = Array.isArray(this.repeat) ? this.repeat[0] : this.repeat === Infinity ? 0 : this.repeat) != null ? _ref : 0;
9719
+ }
9720
+ get repeatTo() {
9721
+ var _ref2;
9722
+ return (_ref2 = Array.isArray(this.repeat) ? this.repeat[1] : this.repeat) != null ? _ref2 : Infinity;
9723
+ }
9724
+ constructor(opts) {
9725
+ super(opts);
9726
+ }
9727
+ updateOptions(opts) {
9728
+ super.updateOptions(opts);
9729
+ }
9730
+ _update(opts) {
9731
+ var _ref3, _ref4, _this$_blocks;
9732
+ const {
9733
+ repeat,
9734
+ ...blockOpts
9735
+ } = normalizeOpts(opts); // TODO type
9736
+ this._blockOpts = Object.assign({}, this._blockOpts, blockOpts);
9737
+ const block = createMask(this._blockOpts);
9738
+ this.repeat = (_ref3 = (_ref4 = repeat != null ? repeat : block.repeat) != null ? _ref4 : this.repeat) != null ? _ref3 : Infinity; // TODO type
9739
+
9740
+ super._update({
9741
+ mask: 'm'.repeat(Math.max(this.repeatTo === Infinity && ((_this$_blocks = this._blocks) == null ? void 0 : _this$_blocks.length) || 0, this.repeatFrom)),
9742
+ blocks: {
9743
+ m: block
9744
+ },
9745
+ eager: block.eager,
9746
+ overwrite: block.overwrite,
9747
+ skipInvalid: block.skipInvalid,
9748
+ lazy: block.lazy,
9749
+ placeholderChar: block.placeholderChar,
9750
+ displayChar: block.displayChar
9751
+ });
9752
+ }
9753
+ _allocateBlock(bi) {
9754
+ if (bi < this._blocks.length) return this._blocks[bi];
9755
+ if (this.repeatTo === Infinity || this._blocks.length < this.repeatTo) {
9756
+ this._blocks.push(createMask(this._blockOpts));
9757
+ this.mask += 'm';
9758
+ return this._blocks[this._blocks.length - 1];
9759
+ }
9760
+ }
9761
+ _appendCharRaw(ch, flags) {
9762
+ if (flags === void 0) {
9763
+ flags = {};
9764
+ }
9765
+ const details = new ChangeDetails();
9766
+ for (let bi = (_this$_mapPosToBlock$ = (_this$_mapPosToBlock = this._mapPosToBlock(this.displayValue.length)) == null ? void 0 : _this$_mapPosToBlock.index) != null ? _this$_mapPosToBlock$ : Math.max(this._blocks.length - 1, 0), block, allocated;
9767
+ // try to get a block or
9768
+ // try to allocate a new block if not allocated already
9769
+ block = (_this$_blocks$bi = this._blocks[bi]) != null ? _this$_blocks$bi : allocated = !allocated && this._allocateBlock(bi); ++bi) {
9770
+ var _this$_mapPosToBlock$, _this$_mapPosToBlock, _this$_blocks$bi, _flags$_beforeTailSta;
9771
+ const blockDetails = block._appendChar(ch, {
9772
+ ...flags,
9773
+ _beforeTailState: (_flags$_beforeTailSta = flags._beforeTailState) == null || (_flags$_beforeTailSta = _flags$_beforeTailSta._blocks) == null ? void 0 : _flags$_beforeTailSta[bi]
9774
+ });
9775
+ if (blockDetails.skip && allocated) {
9776
+ // remove the last allocated block and break
9777
+ this._blocks.pop();
9778
+ this.mask = this.mask.slice(1);
9779
+ break;
9780
+ }
9781
+ details.aggregate(blockDetails);
9782
+ if (blockDetails.consumed) break; // go next char
9783
+ }
9784
+ return details;
9785
+ }
9786
+ _trimEmptyTail(fromPos, toPos) {
9787
+ var _this$_mapPosToBlock2, _this$_mapPosToBlock3;
9788
+ if (fromPos === void 0) {
9789
+ fromPos = 0;
9790
+ }
9791
+ const firstBlockIndex = Math.max(((_this$_mapPosToBlock2 = this._mapPosToBlock(fromPos)) == null ? void 0 : _this$_mapPosToBlock2.index) || 0, this.repeatFrom, 0);
9792
+ let lastBlockIndex;
9793
+ if (toPos != null) lastBlockIndex = (_this$_mapPosToBlock3 = this._mapPosToBlock(toPos)) == null ? void 0 : _this$_mapPosToBlock3.index;
9794
+ if (lastBlockIndex == null) lastBlockIndex = this._blocks.length - 1;
9795
+ let removeCount = 0;
9796
+ for (let blockIndex = lastBlockIndex; firstBlockIndex <= blockIndex; --blockIndex, ++removeCount) {
9797
+ if (this._blocks[blockIndex].unmaskedValue) break;
9798
+ }
9799
+ if (removeCount) {
9800
+ this._blocks.splice(lastBlockIndex - removeCount + 1, removeCount);
9801
+ this.mask = this.mask.slice(removeCount);
9802
+ }
9803
+ }
9804
+ reset() {
9805
+ super.reset();
9806
+ this._trimEmptyTail();
9807
+ }
9808
+ remove(fromPos, toPos) {
9809
+ if (fromPos === void 0) {
9810
+ fromPos = 0;
9811
+ }
9812
+ if (toPos === void 0) {
9813
+ toPos = this.displayValue.length;
9814
+ }
9815
+ const removeDetails = super.remove(fromPos, toPos);
9816
+ this._trimEmptyTail(fromPos, toPos);
9817
+ return removeDetails;
9818
+ }
9819
+ totalInputPositions(fromPos, toPos) {
9820
+ if (fromPos === void 0) {
9821
+ fromPos = 0;
9822
+ }
9823
+ if (toPos == null && this.repeatTo === Infinity) return Infinity;
9824
+ return super.totalInputPositions(fromPos, toPos);
9825
+ }
9826
+ get state() {
9827
+ return super.state;
9828
+ }
9829
+ set state(state) {
9830
+ this._blocks.length = state._blocks.length;
9831
+ this.mask = this.mask.slice(0, this._blocks.length);
9832
+ super.state = state;
9833
+ }
6261
9834
  }
9835
+ IMask.RepeatBlock = RepeatBlock;
6262
9836
 
6263
- var maskaExports = requireMaska();
9837
+ try {
9838
+ globalThis.IMask = IMask;
9839
+ } catch {}
6264
9840
 
9841
+ exports.IMask = IMask;
6265
9842
  exports.format = format;
6266
9843
  exports.isValid = isValid;
6267
- exports.maskaExports = maskaExports;
6268
9844
  exports.parse = parse;