Skip to content

Commit

Permalink
fix #924
Browse files Browse the repository at this point in the history
  • Loading branch information
uNmAnNeR committed Dec 17, 2023
1 parent 276833d commit 39a4c2b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 27 deletions.
19 changes: 0 additions & 19 deletions packages/imask/example.html
Expand Up @@ -17,25 +17,6 @@ <h1>IMask Core Demo</h1>
lazy: false,
overwrite: true,
autofix: 'pad',
blocks: {
d: {
mask: IMask.MaskedRange,
from: 1,
to: 31,
maxLength: 2,
},
m: {
mask: IMask.MaskedRange,
from: 1,
to: 12,
maxLength: 2,
},
Y: {
mask: IMask.MaskedRange,
from: 2023,
to: 2023,
},
},
};

const input = document.getElementById('input');
Expand Down
12 changes: 8 additions & 4 deletions packages/imask/src/masked/base.ts
Expand Up @@ -231,20 +231,24 @@ abstract class Masked<Value=any> {
const beforeTailState = this.state;
if (this.overwrite === true) {
consistentTail = checkTail.state;
checkTail.unshift(this.displayValue.length - details.tailShift);
for (let i=0; i < details.rawInserted.length; ++i) {
checkTail.unshift(this.displayValue.length - details.tailShift);
}
}

let tailDetails = this.appendTail(checkTail);
appended = tailDetails.rawInserted === checkTail.toString();
appended = tailDetails.rawInserted.length === checkTail.toString().length;

// not ok, try shift
if (!(appended && tailDetails.inserted) && this.overwrite === 'shift') {
this.state = beforeTailState;
consistentTail = checkTail.state;
checkTail.shift();
for (let i=0; i < details.rawInserted.length; ++i) {
checkTail.shift();
}

tailDetails = this.appendTail(checkTail);
appended = tailDetails.rawInserted === checkTail.toString();
appended = tailDetails.rawInserted.length === checkTail.toString().length;
}

// if ok, rollback state after tail
Expand Down
5 changes: 1 addition & 4 deletions packages/imask/src/masked/pattern.ts
Expand Up @@ -305,10 +305,7 @@ class MaskedPattern<Value=string> extends Masked<Value> {
const details = new ChangeDetails();
if (!blockIter) return details;

for (let bi=blockIter.index; ; ++bi) {
const block = this._blocks[bi];
if (!block) break;

for (let bi=blockIter.index, block; (block = this._blocks[bi]); ++bi) {
const blockDetails = block._appendChar(ch, { ...flags, _beforeTailState: flags._beforeTailState?._blocks?.[bi] });

const skip = blockDetails.skip;
Expand Down
19 changes: 19 additions & 0 deletions packages/imask/test/masked/base.ts
Expand Up @@ -5,6 +5,7 @@ import MaskedPattern from '../../src/masked/pattern';
import MaskedDate from '../../src/masked/date';
import MaskedNumber from '../../src/masked/number';
import { DIRECTION } from '../../src/core/utils';
import { MaskedRange } from '../../src';


describe('Masked', function () {
Expand Down Expand Up @@ -147,6 +148,24 @@ describe('Masked', function () {
masked.splice(4, 0, '0', DIRECTION.NONE, { input: true, raw: true });
assert.strictEqual(masked.value, '$1,230.45');
});

it('should work with autofix', function () {
const masked = new MaskedRange({
from: 1,
to: 31,
autofix: 'pad',
overwrite: true,
});

masked.value = '12';
assert.strictEqual(masked.value, '12');

masked.splice(0, 0, '3', DIRECTION.NONE, { input: true, raw: true });
assert.strictEqual(masked.value, '31');

masked.splice(0, 0, '4', DIRECTION.NONE, { input: true, raw: true });
assert.strictEqual(masked.value, '04');
});
});

describe('#splice', function () {
Expand Down

0 comments on commit 39a4c2b

Please sign in to comment.